muzimuzhi / thmtools

New home for LaTeX package bundle thmtools
LaTeX Project Public License v1.3c
16 stars 3 forks source link

prefoot and postfoot hooks called twice with restate key #54

Open mbertucci47 opened 8 months ago

mbertucci47 commented 8 months ago

When a theorem is called with the restate key, the prefoot and postfoot hooks are called twice. It only occurs with the key version, not the restatable environment.

\documentclass{article}
\usepackage{amsthm,thmtools}

\declaretheorem{theorem}

\addtotheorempreheadhook{PREHEAD}
\addtotheorempostheadhook{POSTHEAD}
\addtotheoremprefoothook{PREFOOT}
\addtotheorempostfoothook{POSTFOOT}

\begin{document}

\begin{theorem}
body text
\end{theorem}

\begin{theorem}[restate=foo]
body text
\end{theorem}

\begin{restatable}{theorem}{blub}
body text
\end{restatable}

\end{document}

thmtestcode-1

The hooks are also not duplicated in the restated theorem, so here \foo*.

As a side observation, the restatable environment adds an extra space between the posthead and body.

mbertucci47 commented 7 months ago

I think I see the issue but I don't know how to fix it. In the definition of the restate key there is

  \protected@edef\tmp@a{%
    \@nx\thmt@thisistheonetrue
    \@nx\def\@nx\@currenvir{\thmt@envname}%
    \@nx\@xa\@nx\thmt@restatable\@nx\@xa[\@nx\thmt@storedoptargs]%
      {\thmt@envname}{\thmt@storename}%
  }%
  \@xa\g@addto@macro\@xa\thmt@local@postheadhook\@xa{%
    \tmp@a
  }%

so I believe the prefoot and postfoot hooks are being collected along with the theorem body, then since \endtheorem is defined with

  \@xa\def\csname end#1\endcsname{%
    % these need to be in opposite order of headhooks.
    \csname thmt@generic@prefoothook\endcsname
    \csname thmt@#1@prefoothook\endcsname
    \csname thmt@local@prefoothook\endcsname
    \csname thmt@original@end#1\endcsname
    \csname thmt@generic@postfoothook\endcsname
    \csname thmt@#1@postfoothook\endcsname
    \csname thmt@local@postfoothook\endcsname
  }%

in thm-patch.sty, the prefoot and postfoot hooks are executed once again. This can perhaps be fixed with a redefinition of \endtheorem in the restate key, but I don't yet see how to stop \thmt@collect@body from collecting the hooks.