muzimuzhi / thmtools

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

incompatibility with `ntheorem` using `\declaretheorem` #36

Closed GTM52 closed 1 year ago

GTM52 commented 1 year ago

thmtools seems to be incompatible with the package ntheorem, here is a minimal example:

\documentclass{article}
\usepackage{ntheorem, thmtools}
\declaretheorem[style = plain]{theorem}
\begin{document}
\begin{theorem}
    This is a theorem.
\end{theorem}
\end{document}

the error message is Argument of \MakeUppercase has an extra }. This can be solved by setting title = {title_name} in the command, but it could be uncomfortable when declaring many theorems at the same time.

Is there a more elegant way to solve this problem? Thanks for your help.

muzimuzhi commented 1 year ago

The root cause is a long-existing ntheorem issue, see muzimuzhi/ltx-pkg-ntheorem-issues#1.

Since LaTeX2e 2022-11-01, the inner implementation of \MakeUppercase (as well as \MakeLowercase and \MakeTitlecase) is changed, see latex2e@7447e931.

The remaining story is similar to muzimuzhi/ltx-pkg-ntheorem-issues#1: at some step ntheorem's \@begintheorem will expand to sth containing

\item [... \MakeUppercase    []{t}heorem ...]
% which should be
\item[{... \MakeUppercase    []{t}heorem ...}]

A partial (because there're other similar \item uses in ntheorem) patch for ntheorem can be found in muzimuzhi/ltx-pkg-ntheorem-issues#1. Below is a possible patch from thmtools' side (the patch makes use of the knowledge that \thmt@thmname will be fully expanded by \protected@edef, hence tricky):

diff --git a/source/thm-kv.dtx b/source/thm-kv.dtx
index 9fb16c4..01c5f79 100644
--- a/source/thm-kv.dtx
+++ b/source/thm-kv.dtx
@@ -373,7 +333,7 @@
   \thmt@isnumberedtrue
   \thmt@isunlessuniquefalse
   \def\thmt@envname{#1}%
-  \thmt@setthmname{\thmt@modifycase #1}%
+  \thmt@setthmname{\protect\thmt@modifycase #1}%
   % use true code in \thmt@trytwice{<true>}{<false>}
   \@thmt@firstkeysettrue
   % parse options
\documentclass{article}
\usepackage{ntheorem, thmtools}

\usepackage{xpatch}
\makeatletter
\xpatchcmd\declaretheorem@ii
  {\thmt@setthmname{\thmt@modifycase #1}}
  {\thmt@setthmname{\protect\thmt@modifycase #1}}
  {}{\PatchFailed}
\makeatother

\declaretheorem[style=plain]{theorem}\relax

\begin{document}
\begin{theorem}
    This is a theorem.
\end{theorem}
\end{document}
muzimuzhi commented 1 year ago

I pushed a fix in another way. Feedbacks welcome.

muzimuzhi commented 1 year ago

A revised fix is just pushed and I'm going to release a new version.

\thmt@thmname has been fully expanded inside \protected@edef since the first CTAN release of thmtools in 2008, see b89ec88. Hence I decide to not modify this behavior, but only suppress the expansion of \MakeUppercase/\MakeLowercase if it is auto prepended to \thmt@thmname.

GTM52 commented 1 year ago

It works perfectly, thanks for your help!