# How to include label in environment arguments?

I wrote my own environment "theorem" with the package environ for key arguments and was recently stuck at two problems: How to insert citation as an argument and how to do it with a label?

The situation is as folows: When referencing to the theorem, I didn't just want to get the number, but the text "theorem 1.1" (where 1.1 is the correct number of the theorem). I did not like it, that in other environments you first write a label as BODY, so I wanted to include this as an argument. This works fine when I use the name of the label as argument (\mytheorem@label) and write this inside the code for the environment as \label{\mytheorem@label}. But this has the disadvantage that when writing a document, I don't get the list of labels when tiping \ref{}. Therefore I wanted to replace the code snippet in the environment by giving [label=\label{randomStuff}] as argument.

Problem is that just won't work. It gives back the error, that something in the label was never defined. I recently had the same problem with citation (the result should look like theorem 1.1 (see [1]), where [1] is the first citation) and after trying \expandafterand \protect, which I just always try because they seem important, I found that a simple \begin{mytheorem}[{cite=\cite{firstBook}}] does the trick. So I tried something similiar to this, but it didn't work.

Do you have any ideas? If there is a way to loose the parantheses around the cite, I'd also love to hear about it. So here is an example of what I'd like to have.

\documentclass{scrartcl]
\usepackage{Environ}
\newcounter{\theoremcounter}
\define@key{mytheorem}{cite}{\def\mytheorem@cite{#1}}
\define@key{mytheorem}{label}{\def\mytheorem@label{#1}}
\NewEnviron{mytheorem}[1][]{
theorem \thesection.\theoremcounter\ \protect\theorem@cite\\
\def\@currentlabel{theorem~\thesection.\theoremcounter}{{\lemma@label}}
\BODY
}
\section{first sec}
\begin{document}
\begin{mytheorem}[label=\label{th1},cite=(see \cite{author1})]
good idea
\end{mytheorem}

As stated in \ref{th1} it is ...
\end{document}


which should give the output

1. first sec

theorem 1.1 (see [1])
good idea
As stated in theorem 1.1


Sorry, if something is unclear. I'll try to explain whatever you need. I had to make my code much smaller and I hope from this you understand what I mean.

• Welcome to TeX.SE. It would be great if you could provide a complete working example of the work you have done. Your example doesn't compile. It needs a document-class which is provided by the \documentclass{article} command which is typically the first command in LaTeX documents. Adding any content that is supposed to be printed after \end{document} doesn't make any sense. Everything after that command is ignored. If you add \section{<name>} before \begin{document} it will also produce an error. The material that is supposed to be printed shall be inside the document environment. Jun 10 at 13:16

I wouldn't reinvent the wheel: there are several subtle points when defining a theorem-like environment and I'm afraid your code doesn't address them. With amsthm you have a very good way to manage them. And you can use cleveref for adding the tag.

I use expl3 syntax for the key-value system, because it's powerful and can be combined with other features of the language, such as the check whether a key has been set or not.

One can also imagine an abstraction layer to allow for a \NewTheorem command that does a similar job to \newtheorem and applies the ideas outlined below.

\documentclass{article}
\usepackage{amsthm}
\usepackage{cleveref}

\newtheorem{generalthm}{Theorem}[section]
\crefname{generalthm}{theorem}{theorems}
\Crefname{generalthm}{Theorem}{Theorems}

\ExplSyntaxOn
\NewDocumentEnvironment{theorem}{O{}}
{
\keys_set:nn { hannes/theorem } { #1 }
\tl_if_empty:VTF \l__hannes_theorem_cite_tl
{ \generalthm }
{ \generalthm[\l__hannes_theorem_cite_tl] }
\tl_if_empty:VF \l__hannes_theorem_label_tl { \l__hannes_theorem_label_tl }
}
{
\endgeneralthm
}

\keys_define:nn { hannes/theorem }
{
cite  .tl_set:N = \l__hannes_theorem_cite_tl,
label .tl_set:N = \l__hannes_theorem_label_tl,
}

\ExplSyntaxOff

\begin{document}

\section{Main results}

\begin{theorem}[label=\label{th1},cite=see \cite{author1}]
good idea
\end{theorem}

\begin{theorem}[label=\label{th2},cite=see \cite[page~42]{author1}]
good idea
\end{theorem}

\begin{theorem}[label=\label{th3}]
good idea
\end{theorem}

We see that \cref{th1} is important. But \cref{th2} is more important.
Not to mention \cref{th3}.

\begin{thebibliography}{1}

\bibitem{author1} A. Uthor, \emph{Nice paper}, Journal (\textbf{1}), 2022

\end{thebibliography}

\end{document}


I'd not use such a key-value syntax, as the standard \label is easy to input:

\begin{theorem}[see \cite{author1}]\label{th1}
good idea
\end{theorem}


is even easier to type.

By the way, I see no reason for using \NewEnviron.

Here's the abstraction layer.

\documentclass{article}
\usepackage{amsthm}
\usepackage{cleveref}

\ExplSyntaxOn

\NewDocumentCommand{\NewTheorem}{momo}
{
% define the inner theorem-like environment
\IfNoValueTF { #4 }
{% we possibly have the first optional argument
\IfNoValueTF { #2 }
{% no parent counter
\newtheorem{#1@inner}{#3}
}
{
\newtheorem{#1@inner}[#2@inner]{#3}
}
}
{% second optional argument
\newtheorem{#1@inner}{#3}[#4]
}
% take care of cleveref
\use:e
{
\exp_not:N \crefname{#1@inner}{\text_lowercase:n { #3 }}{\text_lowercase:n { #3 }s}
\exp_not:N \Crefname{#1@inner}{\text_titlecase:n { #3 }}{\text_titlecase:n { #3 }s}
}
% define the user level environment
\NewDocumentEnvironment{#1}{O{}}
{
\keys_set:nn { hannes/theorem } { ##1 }
\tl_if_empty:VTF \l__hannes_theorem_cite_tl
{ \begin{ #1@inner } }
{ \begin{ #1@inner } [\l__hannes_theorem_cite_tl] }
\tl_if_empty:VF \l__hannes_theorem_label_tl { \l__hannes_theorem_label_tl }
}
{
\end{#1@inner}
}
}
\keys_define:nn { hannes/theorem }
{
cite  .tl_set:N = \l__hannes_theorem_cite_tl,
label .tl_set:N = \l__hannes_theorem_label_tl,
}

\ExplSyntaxOff

\NewTheorem{theorem}{Theorem}[section]
\NewTheorem{definition}[theorem]{Definition}

\begin{document}

\section{Main results}

\begin{theorem}[label=\label{th1},cite=see \cite{author1}]
good idea
\end{theorem}

\begin{theorem}[label=\label{th2},cite=see \cite[page~42]{author1}]
good idea
\end{theorem}

\begin{definition}[label=\label{def1}]
A definition
\end{definition}

\begin{theorem}[label=\label{th3}]
good idea
\end{theorem}

We see that \cref{th1} is important. But \cref{th2} is more important.
Not to mention \cref{th3}. And we have \cref{th1,th2,th3}.
Also \cref{def1} is interesting.

\begin{thebibliography}{1}

\bibitem{author1} A. Uthor, \emph{Nice paper}, Journal (\textbf{1}), 2022

\end{thebibliography}

\end{document}


Note how \cref renders multiple cross-references.

• Thank you for the input. My main problem with amsthm is that I'm to stupid to do the style the way I have to. That's why I started doing it myself, because honestly this was way faster. I think, I like your way, I just do not know any of the commands, but it seems to work. Might be possible that I have to ask later though. Jun 11 at 18:01

A slightly different approach:

\documentclass{article}

\usepackage{ntheorem}
\usepackage{cleveref}
\theoremstyle{break}
\newtheorem{thm}{theorem}[section]

\usepackage{keyval}
\makeatletter
\define@key{mytheorem}{cite}{\def\mytheorem@cite{#1}}
\define@key{mytheorem}{label}{\def\mytheorem@label{#1}}
\newenvironment{mytheorem}[1][]{%
\setkeys{mytheorem}{#1}%
\begin{thm}[see \mytheorem@cite] \mytheorem@label
}{%
\end{thm}
}
\makeatother

\begin{document}
\section{first sec}
\begin{mytheorem}[label=\label{th1},cite=\cite{book}]
good idea
\end{mytheorem}

As stated in \cref{th1} it is ...

\begin{thebibliography}{9}
\bibitem{book} Donald E. Knuth (1986) \emph{The \TeX{} Book}, Addison-Wesley Professional.
\end{thebibliography}
\end{document}
`

• Hi and thanks for the advice. I changed it a bit and am still trying out. Jun 11 at 17:54