| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% Copyright (C) 2008-2014 by Ulrich M. Schwarz |
| 4 |
% Copyright (C) 2019 by Frank Mittelbach |
| 5 |
% Copyright (C) 2020- by Yukai Chou |
| 6 |
% |
| 7 |
% This file may be distributed and/or modified under the conditions of |
| 8 |
% the LaTeX Project Public License, version 1.3c. |
| 9 |
% The license can be obtained from |
| 10 |
% http://www.latex-project.org/lppl/lppl-1.3c.txt |
| 11 |
% |
| 12 |
% \fi |
| 13 |
% |
| 14 |
%\iffalse (hide this from DocInput) |
| 15 |
%<*patch> |
| 16 |
%\fi |
| 17 |
% |
| 18 |
% This package is maybe not very suitable for the end user. It redefines |
| 19 |
% |\newtheorem| in a way that lets other packages (or the user) add code |
| 20 |
% to the newly-defined theorems, in a reasonably cross-compatible (with |
| 21 |
% the kernel, \pkg{theorem} and \pkg{amsthm}) way. |
| 22 |
% |
| 23 |
% \textbf{Warning:} the new |\newtheorem| is a superset of the allowed |
| 24 |
% syntax. For example, you can give a star and both optional arguments, |
| 25 |
% even though you cannot have an unnumbered theorem that shares a counter |
| 26 |
% and yet has a different reset-regimen. At some point, your command is |
| 27 |
% re-assembled and passed on to the original |\newtheorem|. This might |
| 28 |
% complain, or give you the usual ``Missing |\begin{document}|'' that |
| 29 |
% marks too many arguments in the preamble. |
| 30 |
% |
| 31 |
% \DescribeMacro{\addtotheorem(pre/post)(head/foot)hook} |
| 32 |
% A call to |\addtotheorempreheadhook|\oarg{kind}\marg{code} will |
| 33 |
% insert the code to be executed whenever a kind theorem is opened, before |
| 34 |
% the actual call takes place. (I.e., before the header ``Kind 1.3 (Foo)'' |
| 35 |
% is typeset.) There are also posthooks that are executed after this header, |
| 36 |
% and the same for the end of the environment, even though nothing |
| 37 |
% interesting ever happens there. These are useful to put |
| 38 |
% |\begin{shaded}|\dots |\end{shaded}| around your theorems. |
| 39 |
% Note that foothooks are executed LIFO (last addition first) and headhooks |
| 40 |
% are executed FIFO (first addition first). There is a special kind called |
| 41 |
% generic that is called for all theorems. This is the default if no kind |
| 42 |
% is given. |
| 43 |
% |
| 44 |
% The added code may examine |\thmt@thmname| to get the title, |
| 45 |
% |\thmt@envname| to get the environment's name, and |\thmt@optarg| to get |
| 46 |
% the extra optional title, if any. |
| 47 |
% |
| 48 |
%\StopEventually{} |
| 49 |
% \begin{macrocode} |
| 50 |
\RequirePackage{parseargs} |
| 51 |
|
| 52 |
\newif\ifthmt@isstarred |
| 53 |
\newif\ifthmt@hassibling |
| 54 |
\newif\ifthmt@hasparent |
| 55 |
|
| 56 |
\def\thmt@parsetheoremargs#1{% |
| 57 |
\parse{% |
| 58 |
{\parseOpt[]{\def\thmt@optarg{##1}}{% |
| 59 |
\let\thmt@shortoptarg\@empty |
| 60 |
\let\thmt@optarg\@empty}}% |
| 61 |
{% |
| 62 |
\def\thmt@local@preheadhook{}% |
| 63 |
\def\thmt@local@postheadhook{}% |
| 64 |
\def\thmt@local@prefoothook{}% |
| 65 |
\def\thmt@local@postfoothook{}% |
| 66 |
\thmt@local@preheadhook |
| 67 |
\csname thmt@#1@preheadhook\endcsname |
| 68 |
\thmt@generic@preheadhook |
| 69 |
% change following to \@xa-orgy at some point? |
| 70 |
% forex, might have keyvals involving commands. |
| 71 |
%\protected@edef\tmp@args{% |
| 72 |
% \ifx\@empty\thmt@optarg\else [{\thmt@optarg}]\fi |
| 73 |
%}% |
| 74 |
\ifx\@empty\thmt@optarg |
| 75 |
\def\tmp@args{}% |
| 76 |
\else |
| 77 |
\@xa\def\@xa\tmp@args\@xa{\@xa[\@xa{\thmt@optarg}]}% |
| 78 |
\fi |
| 79 |
\csname thmt@original@#1\@xa\endcsname\tmp@args |
| 80 |
%%moved down: \thmt@local@postheadhook |
| 81 |
%% (give postheadhooks a chance to re-set nameref data) |
| 82 |
\csname thmt@#1@postheadhook\endcsname |
| 83 |
\thmt@generic@postheadhook |
| 84 |
\thmt@local@postheadhook |
| 85 |
%FMi 2019-07-31 |
| 86 |
% \let\@parsecmd\@empty |
| 87 |
\let\@parsecmd\ignorespaces |
| 88 |
%FMi --- |
| 89 |
}% |
| 90 |
}% |
| 91 |
}% |
| 92 |
|
| 93 |
\let\thmt@original@newtheorem\newtheorem |
| 94 |
\let\thmt@theoremdefiner\thmt@original@newtheorem |
| 95 |
|
| 96 |
\def\newtheorem{% |
| 97 |
\thmt@isstarredfalse |
| 98 |
\thmt@hassiblingfalse |
| 99 |
\thmt@hasparentfalse |
| 100 |
\parse{% |
| 101 |
{\parseFlag*{\thmt@isstarredtrue}{}}% |
| 102 |
{\parseMand{\def\thmt@envname{##1}}}% |
| 103 |
{\parseOpt[]{\thmt@hassiblingtrue\def\thmt@sibling{##1}}{}}% |
| 104 |
{\parseMand{\def\thmt@thmname{##1}}}% |
| 105 |
{\parseOpt[]{\thmt@hasparenttrue\def\thmt@parent{##1}}{}}% |
| 106 |
{\let\@parsecmd\thmt@newtheoremiv}% |
| 107 |
}% |
| 108 |
} |
| 109 |
|
| 110 |
\newcommand\thmt@newtheoremiv{% |
| 111 |
\thmt@newtheorem@predefinition |
| 112 |
% whee, now reassemble the whole shebang. |
| 113 |
\protected@edef\thmt@args{% |
| 114 |
\@nx\thmt@theoremdefiner% |
| 115 |
\ifthmt@isstarred *\fi |
| 116 |
{\thmt@envname}% |
| 117 |
\ifthmt@hassibling [\thmt@sibling]\fi |
| 118 |
{\thmt@thmname}% |
| 119 |
\ifthmt@hasparent [\thmt@parent]\fi |
| 120 |
} |
| 121 |
\thmt@args |
| 122 |
\thmt@newtheorem@postdefinition |
| 123 |
} |
| 124 |
|
| 125 |
\newcommand\thmt@newtheorem@predefinition{} |
| 126 |
\newcommand\thmt@newtheorem@postdefinition{% |
| 127 |
\let\thmt@theoremdefiner\thmt@original@newtheorem |
| 128 |
} |
| 129 |
|
| 130 |
\g@addto@macro\thmt@newtheorem@predefinition{% |
| 131 |
\@xa\thmt@providetheoremhooks\@xa{\thmt@envname}% |
| 132 |
} |
| 133 |
\g@addto@macro\thmt@newtheorem@postdefinition{% |
| 134 |
\@xa\thmt@addtheoremhook\@xa{\thmt@envname}% |
| 135 |
\ifthmt@isstarred\@namedef{the\thmt@envname}{}\fi |
| 136 |
\protected@edef\thmt@tmp{% |
| 137 |
\def\@nx\thmt@envname{\thmt@envname}% |
| 138 |
\def\@nx\thmt@thmname{\thmt@thmname}% |
| 139 |
}% |
| 140 |
\@xa\addtotheorempreheadhook\@xa[\@xa\thmt@envname\@xa]\@xa{% |
| 141 |
\thmt@tmp |
| 142 |
}% |
| 143 |
} |
| 144 |
\newcommand\thmt@providetheoremhooks[1]{% |
| 145 |
\@namedef{thmt@#1@preheadhook}{}% |
| 146 |
\@namedef{thmt@#1@postheadhook}{}% |
| 147 |
\@namedef{thmt@#1@prefoothook}{}% |
| 148 |
\@namedef{thmt@#1@postfoothook}{}% |
| 149 |
\def\thmt@local@preheadhook{}% |
| 150 |
\def\thmt@local@postheadhook{}% |
| 151 |
\def\thmt@local@prefoothook{}% |
| 152 |
\def\thmt@local@postfoothook{}% |
| 153 |
} |
| 154 |
\newcommand\thmt@addtheoremhook[1]{% |
| 155 |
% this adds two command calls to the newly-defined theorem. |
| 156 |
\@xa\let\csname thmt@original@#1\@xa\endcsname |
| 157 |
\csname#1\endcsname |
| 158 |
\@xa\renewcommand\csname #1\endcsname{% |
| 159 |
\thmt@parsetheoremargs{#1}% |
| 160 |
}% |
| 161 |
\@xa\let\csname thmt@original@end#1\@xa\endcsname\csname end#1\endcsname |
| 162 |
\@xa\def\csname end#1\endcsname{% |
| 163 |
% these need to be in opposite order of headhooks. |
| 164 |
\csname thmt@generic@prefoothook\endcsname |
| 165 |
\csname thmt@#1@prefoothook\endcsname |
| 166 |
\csname thmt@local@prefoothook\endcsname |
| 167 |
\csname thmt@original@end#1\endcsname |
| 168 |
\csname thmt@generic@postfoothook\endcsname |
| 169 |
\csname thmt@#1@postfoothook\endcsname |
| 170 |
\csname thmt@local@postfoothook\endcsname |
| 171 |
}% |
| 172 |
} |
| 173 |
\newcommand\thmt@generic@preheadhook{\refstepcounter{thmt@dummyctr}} |
| 174 |
\newcommand\thmt@generic@postheadhook{} |
| 175 |
\newcommand\thmt@generic@prefoothook{} |
| 176 |
\newcommand\thmt@generic@postfoothook{} |
| 177 |
|
| 178 |
\def\thmt@local@preheadhook{} |
| 179 |
\def\thmt@local@postheadhook{} |
| 180 |
\def\thmt@local@prefoothook{} |
| 181 |
\def\thmt@local@postfoothook{} |
| 182 |
|
| 183 |
|
| 184 |
\providecommand\g@prependto@macro[2]{% |
| 185 |
\begingroup |
| 186 |
\toks@\@xa{\@xa{#1}{#2}}% |
| 187 |
\def\tmp@a##1##2{##2##1}% |
| 188 |
\@xa\@xa\@xa\gdef\@xa\@xa\@xa#1\@xa\@xa\@xa{\@xa\tmp@a\the\toks@}% |
| 189 |
\endgroup |
| 190 |
} |
| 191 |
|
| 192 |
\newcommand\addtotheorempreheadhook[1][generic]{% |
| 193 |
\expandafter\g@addto@macro\csname thmt@#1@preheadhook\endcsname% |
| 194 |
} |
| 195 |
\newcommand\addtotheorempostheadhook[1][generic]{% |
| 196 |
\expandafter\g@addto@macro\csname thmt@#1@postheadhook\endcsname% |
| 197 |
} |
| 198 |
|
| 199 |
\newcommand\addtotheoremprefoothook[1][generic]{% |
| 200 |
\expandafter\g@prependto@macro\csname thmt@#1@prefoothook\endcsname% |
| 201 |
} |
| 202 |
\newcommand\addtotheorempostfoothook[1][generic]{% |
| 203 |
\expandafter\g@prependto@macro\csname thmt@#1@postfoothook\endcsname% |
| 204 |
} |
| 205 |
|
| 206 |
% \end{macrocode} |
| 207 |
% Since rev1.16, we add hooks to the proof environment as well, if it exists. |
| 208 |
% If it doesn't exist at this point, we're probably using ntheorem as |
| 209 |
% backend, where it goes through the regular theorem mechanism anyway. |
| 210 |
% \begin{macrocode} |
| 211 |
\ifx\proof\endproof\else% yup, that's a quaint way of doing it :) |
| 212 |
% FIXME: this assumes proof has the syntax of theorems, which |
| 213 |
% usually happens to be true (optarg overrides "Proof" string). |
| 214 |
% FIXME: refactor into thmt@addtheoremhook, but we really don't want to |
| 215 |
% call the generic-hook... |
| 216 |
\let\thmt@original@proof=\proof |
| 217 |
\renewcommand\proof{% |
| 218 |
\thmt@parseproofargs% |
| 219 |
}% |
| 220 |
\def\thmt@parseproofargs{% |
| 221 |
\parse{% |
| 222 |
{\parseOpt[]{\def\thmt@optarg{##1}}{\let\thmt@optarg\@empty}}% |
| 223 |
{% |
| 224 |
\thmt@proof@preheadhook |
| 225 |
%\thmt@generic@preheadhook |
| 226 |
\protected@edef\tmp@args{% |
| 227 |
\ifx\@empty\thmt@optarg\else [\thmt@optarg]\fi |
| 228 |
}% |
| 229 |
\csname thmt@original@proof\@xa\endcsname\tmp@args |
| 230 |
\thmt@proof@postheadhook |
| 231 |
%\thmt@generic@postheadhook |
| 232 |
\let\@parsecmd\@empty |
| 233 |
}% |
| 234 |
}% |
| 235 |
}% |
| 236 |
|
| 237 |
\let\thmt@original@endproof=\endproof |
| 238 |
\def\endproof{% |
| 239 |
% these need to be in opposite order of headhooks. |
| 240 |
%\csname thmtgeneric@prefoothook\endcsname |
| 241 |
\thmt@proof@prefoothook |
| 242 |
\thmt@original@endproof |
| 243 |
%\csname thmt@generic@postfoothook\endcsname |
| 244 |
\thmt@proof@postfoothook |
| 245 |
}% |
| 246 |
\@namedef{thmt@proof@preheadhook}{}% |
| 247 |
\@namedef{thmt@proof@postheadhook}{}% |
| 248 |
\@namedef{thmt@proof@prefoothook}{}% |
| 249 |
\@namedef{thmt@proof@postfoothook}{}% |
| 250 |
\fi |
| 251 |
% \end{macrocode} |
| 252 |
%\iffalse |
| 253 |
%</patch> |
| 254 |
%\fi |