Add argument to theorem environment

environmentstheorems

I want to have a theorem environment with an additional required argument:

\begin{mylemma}{text1, text2}
    It holds that True.
\end{mylemma}
\begin{mylemma}{text1, text2}[Name]
    It holds that True.
\end{mylemma}

The output should look like this:

Lemma 1.1 [text1, text2]. It holds that True.

Lemma 1.2 (Name) [text1, text2]. It holds that True.

I tried using the answers provided here, here, here and here, but got none to work. The closest I got was the following:

\documentclass{book}

\usepackage{amsmath, amsthm}

\usepackage{thmtools}
\declaretheoremstyle[%
within=chapter,%
notefont=\normalfont\itshape,%
notebraces={}{},%
]{mystyle}
\declaretheorem[style=mystyle,name=Lemma]{mydef}
\newenvironment{mylemma}[2]
    {\begin{mydef}[{(}#2{)\ [}#1{]}]}
    {\end{mydef}}
    
\begin{document}
\begin{mylemma}{text1, text2}{Name}
It holds that True.
\end{mylemma}
\end{document}

This allows me to use \begin{mylemma}{text1, text2}{Name}\end{mylemma}, but the argument I want to be optional is now required. Also, it would be nice to be able to write the following instead of defining new environments so verbosely:

\newtheoremstyle{mystyle}{}...{}
\theoremstyle{mystyle}
\newtheorem{mylemma}[theorem]{Lemma}
\newtheorem{mytheorem}[theorem]{Theorem}
\newtheorem{mycorollary}[theorem]{Corollary}
\newtheorem{myproposition}[theorem]{Proposition}

and then being able to use each environment like specified above. I tried to adapt the answer provided here for that, but apart from then having two optional arguments, I got an error possibly related to other packages I use (Package ntheorem Error: Theorem style plain already defined. ...rfont ##1\ ##2\ (##3)\theorem@separator}}). Edit: a MWE for that modification:

\documentclass{book}

\usepackage{amsmath, amsthm}

\usepackage{mathtools, xparse}
\usepackage{ntheorem}
\makeatletter
\newtheoremstyle{mystyle}
{\isastyleaux{##1}{##2}}
{\isastyleaux{##1}{##2}[##3]}
\NewDocumentCommand\isastyleaux{mmou\ignorespaces o}
{\item[\theorem@headerfont\hskip\labelsep
    #1\ #2%
    \IfValueT{#3}{\normalfont\itshape (#3)}%
    \IfValueT{#5}{\normalfont\itshape [#5]}%
    .\theorem@separator]#4\ignorespaces}
\makeatother
\theoremstyle{mystyle}
\newtheorem{mylemma}{Lemma}
    
\begin{document}
\begin{mylemma}[text1, text2][Name]
It holds that True.
\end{mylemma}
\end{document}

Best Answer

Define an inner theorem and a new environment.

\documentclass{article}

% define it to your liking
\newtheorem{mylemmainner}{Lemma}[section]

\NewDocumentEnvironment{mylemma}{mo}
 {%
  \IfNoValueTF{#2}{\mylemmainner\textup{[#1]}}{\mylemmainner[#2]\textup{[#1]}} \ignorespaces
 }
 {\endmylemmainner}

\begin{document}

\section{Test}

\begin{mylemma}{text1, text2}
This is the text of the statement.
\end{mylemma}

\begin{mylemma}{text1, text2}[Name]
This is the text of the statement.
\end{mylemma}

\end{document}

enter image description here

Here's how to move the period after the part in square brackets, with amsthm (but not ntheorem and you shouldn't load both).

\documentclass{article}
\usepackage{amsthm}

\newtheoremstyle{witharg}
  {} % ABOVESPACE
  {} % BELOWSPACE
  {\itshape} % BODYFONT
  {0pt} % INDENT (empty value is the same as 0pt)
  {\bfseries} % HEADFONT
  {} % HEADPUNCT
  {5pt plus 1pt minus 1pt} % HEADSPACE
  % CUSTOM-HEAD-SPEC
  {\thmname{#1} \thmnumber{#2}\thmnote{ (#3)} \textnormal{[\theoremarg].}}
\newcommand{\theoremarg}{}
  
\theoremstyle{witharg}
\newtheorem{mylemmainner}{Lemma}[section]

\NewDocumentEnvironment{mylemma}{mo}
 {%
  \renewcommand{\theoremarg}{#1}%
  \IfNoValueTF{#2}{\mylemmainner}{\mylemmainner[#2]}\ignorespaces
 }
 {\endmylemmainner}

\begin{document}

\section{Test}

\begin{mylemma}{text1, text2}
This is the text of the statement.
\end{mylemma}

\begin{mylemma}{text1, text2}[Name]
This is the text of the statement.
\end{mylemma}

\end{document}

enter image description here

Related Question