# Setting an “attribute” for Theorem to be accessed later

I am writing a math paper with a large number of theorems. I'm looking to have a table summarizing my results, and I'd love it if the table could be generated automatically from my paper. I' looking to be able to do something like this

\begin{thm}
\shortTheorem{$x + y = y + x$}
Let $x,y$ be integers. Then $x+y=y+x$.
\end{thm}


Note that shortTheorem is just the name I made up for whatever tag I put my desired text into.

Then, later on, I want to have a table which, for each theorem I define, prints its "shortTheorem" contents. i.e.

• x + y = y + x

Are there existing packages to do this? If not, how would I go about writing my own macro to do so?

(Note: I couldn't figure out if there was a way to actually render a LaTeX table in my question, feel free to edit if you know how.)

-
This looks like something the endnotes package could do (or you could save the code directly) To show a latex image just use the image upload button to upload a cropped screenshot. It always helps if you post a complete document in your question not just a fragment. Also in this case what other information do you want in your final list, you just show a bullet but you also want the theorem number? –  David Carlisle May 6 at 21:09
My entire document is 30 pages spread out over 6 source files, so I don't think I'll be posting it in its entirety. As for information, the Theorem number would be nice, but all I really want is the text from \shortTheorem{} or some other tag to be put in the table. –  jmite May 6 at 21:16
I didn't mean post your orignal document I meant that example codes should have \documentclass and needed \usepackage to save people thinking about helping adding them. –  David Carlisle May 6 at 21:20
add comment

## 2 Answers

This makes a table at the end, which is I think what you wanted:

\documentclass{article}

\makeatletter
\def\shortthms{}
\def\shorttheorem#1{\g@addto@macro\shortthms{#1\\}}
\makeatother

\newtheorem{thm}{Theorem}

\begin{document}

\begin{thm}
\shorttheorem{$x + y = y + x$}
Let $x,y$ be integers. Then $x+y=y+x$.
\end{thm}

\begin{thm}
\shorttheorem{$x + x = x + x$}
Let $x,y$ be integers. Then $x+x=x+x$.
\end{thm}

blab blah

\begin{tabular}{p{5cm}}
\shortthms
\end{tabular}

\end{document}


Plan b, using a file takes two latex runs to pull in the data:

\documentclass{article}

\makeatletter
%\def\shortthms{}
%\def\shorttheorem#1{\g@addto@macro\shortthms{#1\\}}

\def\shorttheorem#1{\immediate\write\shortthmout{\unexpanded{#1\\}}}

\def\shortthms{%
\begin{tabular}{p{5cm}}\@input{\jobname.thm}\end{tabular}%
\newwrite\shortthmout
\immediate\openout\shortthmout=\jobname.thm}

\makeatother

\newtheorem{thm}{Theorem}

\begin{document}

\shortthms

\begin{thm}
\shorttheorem{$x + y = y + x$}
Let $x,y$ be integers. Then $x+y=y+x$.
\end{thm}

\begin{thm}
\shorttheorem{$x + x = x + x$}
Let $x,y$ be integers. Then $x+x=x+x$.
\end{thm}

blab blah

\end{document}

-
This is exactly what I want. The only thing is, is there a way to render the table earlier in the document than the theorems themselves? Ideally I'd have "summary of my results" followed by "detailed statement/proof for each theorem". –  jmite May 6 at 21:40
@jmite If you need that you need a different implementation that writes the text to a file. I dodn't do that as you said "...Then, later on, ..." which rather implied "not earlier":-) –  David Carlisle May 6 at 21:53
I should have been more clear. Thanks anyways, it's quite a nice implementation. –  jmite May 6 at 22:05
@jmite version with table first added –  David Carlisle May 6 at 22:27
add comment

If you want also to print the theorem number, here's a schematic solution:

\documentclass{article}
\usepackage{lipsum}% just for the example

\newcommand{\shorttheorem}[1]{%
\xdef\shorttheoremlist{%
\unexpanded\expandafter{\shorttheoremlist}%
\noexpand\item[Theorem \thethm:]
\unexpanded{#1}}}

\newcommand\shorttheoremlist{}
\newcommand\printshorttheorems{%
\begin{description}
\shorttheoremlist
\end{description}}

\newtheorem{thm}{Theorem}

\begin{document}

\section{A section}

\lipsum[2]

\begin{thm}\shorttheorem{$x+y=y+x$}
Let $x$ and $y$ be integers. Then $x+y=y+x$.
\end{thm}

\lipsum[3]

\begin{thm}\shorttheorem{$\sqrt{x^2}=|x|$}
Let $x$ be an integer. Then $\sqrt{x^2}=|x|$.
\end{thm}

\section{The final list}

\printshorttheorems

\end{document}


## Extended version

If you want to name the theorems in different ways and have them in the final list, then cleveref can come to the rescue. It's important to load also amsthm or ntheorem (I prefer the former) and to define the theorem-like environments after loading cleveref. Also, be sure to add the \shorttheorem bit at the start of the environment.

\documentclass{article}
\usepackage{lipsum}% just for the example

\usepackage{amsthm}
\usepackage{cleveref}

\newcommand{\shorttheorem}[1]{%
\stepcounter{shortcount}%
\label{shortcount\theshortcount}%
\xdef\shorttheoremlist{%
\unexpanded\expandafter{\shorttheoremlist}%
\noexpand\item[\noexpand\Cref{shortcount\theshortcount}:]
\unexpanded{#1}}}
\newcounter{shortcount}
\newcommand\shorttheoremlist{}
\newcommand\printshorttheorems{%
\begin{description}
\shorttheoremlist
\end{description}}

\newtheorem{thm}{Theorem}
\newtheorem{lem}[thm]{Lemma}

\begin{document}

\section{A section}

\lipsum[2]

\begin{lem}\shorttheorem{$x+y=y+x$}
Let $x$ and $y$ be integers. Then $x+y=y+x$.
\end{lem}

\lipsum[3]

\begin{thm}\shorttheorem{$\sqrt{x^2}=|x|$}
Let $x$ be an integer. Then $\sqrt{x^2}=|x|$.
\end{thm}

\section{The final list}

\printshorttheorems

\end{document}


## Extended version with arbitrary placement in the document

\documentclass{article}
\usepackage{lipsum}% just for the example

\usepackage{amsthm}
\usepackage{cleveref}

\makeatletter
\newwrite\shorttheoremfile
\newcounter{shortcount}
\newcommand{\shorttheorem}[1]{%
\stepcounter{shortcount}%
\label{shortcount\theshortcount}%
\xdef\shorttheoremlist{%
\unexpanded\expandafter{\shorttheoremlist}%
\string\item[\string\Cref{shortcount\theshortcount}:]
\unexpanded{#1}^^J}%
}
\newcommand{\shorttheoremlist}{}
\AtEndDocument{
\immediate\openout\shorttheoremfile=\jobname.thm
\immediate\write\shorttheoremfile{\unexpanded\expandafter{\shorttheoremlist}}
\immediate\closeout\shorttheoremfile
}

\newcommand\printshorttheorems{%
\begin{description}
\def\@noitemerr{\@latex@warning{Empty short theorem list}}%
\@input{\jobname.thm}
\end{description}
}
\makeatother

\newtheorem{thm}{Theorem}
\newtheorem{lem}[thm]{Lemma}

\begin{document}

\section{Short theorems}

\printshorttheorems

\section{A section}

\lipsum[2]

\begin{lem}\shorttheorem{$x+y=y+x$}
Let $x$ and $y$ be integers. Then $x+y=y+x$.
\end{lem}

\lipsum[3]

\begin{thm}\shorttheorem{$\sqrt{x^2}=|x|$}
Let $x$ be an integer. Then $\sqrt{x^2}=|x|$.
\end{thm}

\end{document}

-
This is quite nice. Is there any way to get the label of the theorem? I have mine labelled thm:foo and lem:bar so that I can decide whether to print Theorem, Lemma etc. –  jmite May 6 at 21:48
@jmite I was afraid you were to ask this. ;-) Next step, be patient. –  egreg May 6 at 21:50
@jmite Here is a version that prints the different labels and allows arbitrary placement in the document (requires two runs to stabilize, as usual in these cases). –  egreg May 6 at 23:22
add comment