Thinking about this question, I'd like to use thmtools' restatable environment to store theorems with a number in the csname. That way one can loop through them at the end of the document to restate each one. My issue is that the way thmtools stores this csname seems to prevent expansion of commands within.
Here's an example that works. \somecmd steps through a counter and stores its argument in cmd-1, cmd-2, etc.
\documentclass{article}
\newcounter{foo}
\newcommand{\somecmd}[1]{%
\stepcounter{foo}%
\expandafter\def\csname cmd-\thefoo\endcsname{#1}%
}
\begin{document}
\somecmd{A}
\somecmd{B}
\somecmd{C}
\UseName{cmd-1}
\UseName{cmd-2}
\UseName{cmd-3}
\ExpandArgs{c}\ShowCommand{cmd-1}
\ExpandArgs{c}\ShowCommand{cmd-2}
\end{document}
The output is A B C and the log shows
> \cmd-1=macro:
->A.
<argument> \cmd-1
l.19 \ExpandArgs{c}\ShowCommand{cmd-1}
> \cmd-2=macro:
->B.
<argument> \cmd-2
l.20 \ExpandArgs{c}\ShowCommand{cmd-2}
as expected.
Now here's a try with restatable where \declaretheoremx works like \declaretheorem but also defines an environment saved<thmname> that stores its contents in a command thm-<num>. Since the environment grabs its contents we need to use the +b argspec.
\documentclass{article}
\usepackage{kantlipsum,amsthm,thmtools}
\newcounter{savedthmcount}
\newcommand{\declaretheoremx}[2][]{
\declaretheorem[#1]{#2}
\NewDocumentEnvironment{saved#2}{+b}{%
\stepcounter{savedthmcount}%
\begin{restatable}{#2}{thm-\thesavedthmcount}
##1
\end{restatable}
}{}
}
\declaretheoremx{theorem}
\begin{document}
\begin{savedtheorem}
\kant[2][1]
\end{savedtheorem}
\begin{savedtheorem}
\kant[3][1]
\end{savedtheorem}
\UseName{thm-1}*
\UseName{thm-2}*
\ExpandArgs{c}\ShowCommand{thm-1}
\ExpandArgs{c}\ShowCommand{thm-2}
\end{document}
Everything works as expected except that each command thm-<num> is just the last theorem called. In the log we get
> \thm-1=macro:
->\@ifstar {\thmt@thisistheonefalse \csname thmt@stored@thm-\thesavedthmcount \
endcsname }{\thmt@thisistheonetrue \csname thmt@stored@thm-\thesavedthmcount \e
ndcsname }.
<argument> \thm-1
l.31 \ExpandArgs{c}\ShowCommand{thm-1}
> \thm-2=macro:
->@ifstar {\thmt@thisistheonefalse \csname thmt@stored@thm-\thesavedthmcount
endcsname }{\thmt@thisistheonetrue \csname thmt@stored@thm-\thesavedthmcount \e
ndcsname }.
<argument> \thm-2
l.32 \ExpandArgs{c}\ShowCommand{thm-2}
which makes it seem like the stored csname is the literal string thmt@stored@thm-\thesavedthmcount instead of thmt@stored@thm-thm-1, thmt@stored@thm-thm-2, etc.
What is going wrong here? The definition of the restatable environment in thm-restate.sty is somewhat complicated but I don't see anything that would "protect" the argument from expansion.
Edit: This seems to work but I'd still like to know specifically what's going wrong without this trick.


thm-restate. – John Kormylo Sep 08 '23 at 20:40thm-restateis automatically loaded bythmtools– mbert Sep 08 '23 at 20:48\@ifstarand\thesavedthmcountand what this commands do will depend on their meaning when you use the command.\ExpandArgs{nnx}\begin{restatable}{#2}{thm-\thesavedthmcount}should work. – Ulrike Fischer Sep 08 '23 at 22:40\begin{restatable}[##1]{#2}{thm-\thesavedthmcount}, what would be the argument of\ExpandArgs? For example\ExpandArgs{nxnx}doesn't seem to work – mbert Sep 09 '23 at 20:44