I am writing a math paper consisting of several Definitions, Lemmas, Theorems, Corollaries, etc. Every such part can have a title and/or a reference. In the text, I would like to have both the title and the reference appear. In the table of contents, I would like for only the title to appear. I have been able to come up with the following MWE. However, I would like to ask whether there is a more idiomatic way of achieving this. In particular, I'm unsure whether my handling of the keyword arguments using the keyval
package could be made simpler, i.e. more idiomatic. In particular, I would like to avoid all the csname
usage.
MWE:
\documentclass[border={10pt 10pt 10pt 10pt}]{standalone}
\usepackage{keyval}
\usepackage{etoolbox}
\usepackage{amsthm}
\usepackage{thmtools}
\makeatletter
\newcommand{\HelperDeclareTheorem}[4]{
\define@key{#1@keys}{title}{\expandafter\def\csname #1@title\endcsname{##1}}
\define@key{#1@keys}{reference}{\expandafter\def\csname #1@reference\endcsname{##1}}
\declaretheorem[
numberlike=subsubsection,
name=#3,
refname={#1, #2},
Refname={#3, #4},
style=definition
]{#1aux}
\newenvironment{#1}[1][]{
\setkeys{#1@keys}{##1}
\@ifundefined{#1@title}{
\@ifundefined{#1@reference}{
\begin{#1aux}
}{
\def\reference{\csname #1@reference\endcsname}
\begin{#1aux}[\reference]
}
\addcontentsline{toc}{subsubsection}{\protect\numberline{\thesubsubsection} #3}
}{
\def\title{\csname #1@title\endcsname}
\@ifundefined{#1@reference}{
\begin{#1aux}[\title]
}{
\def\reference{\csname #1@reference\endcsname}
\begin{#1aux}[\title \ (\reference)]
}
\addcontentsline{toc}{subsubsection}{\protect\numberline{\thesubsubsection} #3 (\title)}
}
}{
\end{#1aux}
}
}
\makeatother
\HelperDeclareTheorem{definition}{definitions}{Definition}{Definitions}
\HelperDeclareTheorem{lemma}{lemmas}{Lemma}{Lemmas}
\HelperDeclareTheorem{theorem}{theorems}{Theorem}{Theorems}
\HelperDeclareTheorem{corollary}{corollaries}{Corollary}{Corollaries}
\begin{document}
\begin{minipage}{\textwidth}
\tableofcontents
\section{Section 1}
\subsection{Subsection 1.1}
\begin{definition}[]
This Definition has neither a title nor a reference.
\end{definition}
\begin{lemma}[title={Lemma A}]
This Lemma has a title but no reference.
\end{lemma}
\begin{theorem}[reference={Reference B}]
This Theorem has no title but a reference.
\end{theorem}
\begin{corollary}[title={Corollary C},reference={Reference D}]
This Corollary has both a title and a reference.
\end{corollary}
\end{minipage}
\end{document}
\csname...\endcsname
is just how you do this in classic tex. You can use expl3 (also l3keys) for nicer constructs if you want to be modern#1@title
, you could just define\smiley@title
locally for each. Also probably not a good idea to use\title
as a variable\title
? Isn't it just a local variable which shadows the global variable?\title
is unlikely to appear inside a theorem it shouldn't matter here