-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefstate.dtx
296 lines (293 loc) · 7.93 KB
/
refstate.dtx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
% \iffalse meta-comment
%
%% File: refstate.dtx
%% Copyright (C) 2021--2024 by Maximilian Teegen
%%
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3c of this license or (at your option) any later
%% version. The latest version of this license is in:
%%
%% http://www.latex-project.org/lppl.txt
%%
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%
%<*batch>
%<*gobble>
\ifx\jobname\relax\let\documentclass\undefined\fi
\ifx\documentclass\undefined
\csname fi\endcsname
%</gobble>
\input l3docstrip.tex
\keepsilent
\preamble
\endpreamble
\generate{\file{refstate.sty}{\from{refstate.dtx}{package}}}
\endbatchfile
%</batch>
%<*gobble>
\fi
\expandafter\ifx\csname @currname\endcsname\empty
\csname fi\endcsname
%</gobble>
% \fi
%
%<*driver>
\ProvidesFile{refstate.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{refstate}
%<*package>
[2024/10/10 v0.2 ]
%</package>
%
%<*driver>
\documentclass{l3doc}
\usepackage{lmodern}
\usepackage{refstate}
\usepackage{hyperref}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\theoremstyle{plain}
\newtheorem{THM}{Theorem}[section]
\begin{document}
\DocInput{refstate.dtx}
\PrintChanges
\PrintIndex
\end{document}
%</driver>
% \fi
%
% \CharacterTable
% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
% Digits \0\1\2\3\4\5\6\7\8\9
% Exclamation \! Double quote \" Hash (number) \#
% Dollar \$ Percent \% Ampersand \&
% Acute accent \' Left paren \( Right paren \)
% Asterisk \* Plus \+ Comma \,
% Minus \- Point \. Solidus \/
% Colon \: Semicolon \; Less than \<
% Equals \= Greater than \> Question mark \?
% Commercial at \@ Left bracket \[ Backslash \\
% Right bracket \] Circumflex \^ Underscore \_
% Grave accent \` Left brace \{ Vertical bar \|
% Right brace \} Tilde \~}
%
%
% \changes{v0.2}{2024/10/10}{Converted to DTX file}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \providecommand*{\url}{\texttt}
% \GetFileInfo{refstate.dtx}
% \title{The \pkg{refstate} package}
% \author{Maximilian Teegen}
% \date{\fileversion~from \filedate}
%
% \maketitle
% \begin{documentation}
% \section{Introduction}
%
% The \pkg{refstate} package provides a mechanism to pre-state and re-state theorems in a \LaTeX\ document.
% It relies on the implementation details of the \pkg{amsthm} package and \textbf{may break if those details change}.
%
% \section{Usage}
%
% To use the \pkg{refstate} package, include it in your document preamble with:
% \begin{verbatim}
% \usepackage{refstate}
% \end{verbatim}
% Declare your theorem environments like normal.
% \begin{verbatim}
% \theoremstyle{plain}
% \newtheorem{THM}{Theorem}[section]
% \end{verbatim}
%
% \begin{function}{store}
% You can then use the \env{store} environment to define a theorem like this.
%
% \begin{verbatim}
% \begin{store}{THM}[Blue sky theorem]\label{thmmain}
% The sky is blue.
% \end{store}
% \end{verbatim}
% \hrule
% \begin{store}{THM}[Blue sky theorem]\label{thmmain}
% The sky is blue.
% \end{store}
% \hrule
% \end{function}
%
% \begin{function}{\refstate}
% Elsewhere in the document (before or after), use the \tn{refstate} command to reference it.
%
% \begin{verbatim}
% \refstate{thmmain}
% \end{verbatim}
% \hrule
% \refstate{thmmain}
% \hrule
% \end{function}
%
% \end{documentation}
%
% \StopEventually{}
%
% \section{Implementation}
% \begin{implementation}
%
% \iffalse
%<*package>
% \fi
%
% \begin{macrocode}
\ProvidesPackage{refstate}
\RequirePackage{amsthm,xparse}
\RequirePackage{marginnote}
\ExplSyntaxOn
% \end{macrocode}
% Redefine \tn{newtheorem} to capture theorem styles and names and save them to macros.
% \begin{macrocode}
\let\rfst@newtheorem@original=\newtheorem
\RenewDocumentCommand{\newtheorem}{s m o m o}{
\tl_if_in:nnTF {#2} {phfthm@} {
\tl_set:Nx \l_tmpa_tl
{\tl_range:nnn{#2}{8}{-1}}
\cs_if_exist:NTF \cmdKV@phfmkthm@thmstyle {
\if\relax\detokenize\expandafter{\cmdKV@phfmkthm@thmstyle}\relax % ... why?
\tl_const:cx {rfst@style@\tl_use:N\l_tmpa_tl} {\the\thm@style}
\else
\tl_const:cx {rfst@style@\tl_use:N\l_tmpa_tl} {\cmdKV@phfmkthm@thmstyle}
\fi
}{
\tl_const:cx {rfst@style@\tl_use:N\l_tmpa_tl} {\the\thm@style}
}
\tl_const:cx {rfst@style@#2} {plain}
\tl_const:cx {rfst@name@\tl_use:N\l_tmpa_tl}
{#4}
}{
\tl_const:cx {rfst@style@#2} {\the\thm@style}
\tl_const:cx {rfst@name@#2} {#4}
}
\IfBooleanTF{#1}{
\IfValueTF{#3}{
\IfValueTF{#5}{
\rfst@newtheorem@original*{#2}[#3]{#4}[#5]
}{
\rfst@newtheorem@original*{#2}[#3]{#4}
}
}{
\IfValueTF{#5}{
\rfst@newtheorem@original*{#2}{#4}[#5]
}{
\rfst@newtheorem@original*{#2}{#4}
}
}
}{
\IfValueTF{#3}{
\IfValueTF{#5}{
\rfst@newtheorem@original{#2}[#3]{#4}[#5]
}{
\rfst@newtheorem@original{#2}[#3]{#4}
}
}{
\IfValueTF{#5}{
\rfst@newtheorem@original{#2}{#4}[#5]
}{
\rfst@newtheorem@original{#2}{#4}
}
}
}
}
% \end{macrocode}
% Define a theorem-environment lookalike command for the restated theorem.
% We gobble the \tn{label} command for the body so that there is no labels.
% \begin{macrocode}
\NewExpandableDocumentCommand{\rfst@gobble@label}{o m}{}
\NewDocumentCommand{\rfst@faketheorem}{m m m m +m O{}}{
% 1=Label 2=Style 3=Type 4=TypeName 5=Body 6=Head
\begingroup
\let\label\rfst@gobble@label
% \let\index\@gobble \let\glossary\@gobble
% Not all of these may be desirable...
\ifhmode\unskip\unskip\par\fi
\normalfont
\trivlist
\let\thmheadnl\relax
\let\thm@swap\@gobble
\thm@notefont{\fontseries\mddefault\upshape}%
\thm@headpunct{.}% add period after heading
\thm@headsep 5\p@ plus\p@ minus\p@\relax
\thm@space@setup
\csname th@#2\endcsname
\@topsep \thm@preskip % used by thm head
\@topsepadd \thm@postskip % used by \@endparenv
\@begintheorem{#4}{\ref{#1}}[#6]
\marginnote{\textit{\footnotesize$\leadsto$\,p.\,\pageref*{#1}}}
\ignorespaces
#5
\@endtheorem
\endgroup
}
% \end{macrocode}
% \begin{macro}{\refstate}
% \begin{macrocode}
\newcommand{\refstate}[1]{%
\ifcsname rfst@storedthm@#1\endcsname
\@nameuse{rfst@storedthm@#1}
\else
% \end{macrocode}
% We need an other run of latex. Put a placeholder.
% \begin{macrocode}
\ifhmode\unskip\unskip\par\fi
\thm@space@setup
\vskip\thm@preskip
\noindent
\textbf{Restate}~\texttt{\detokenize{#1}}
\par
\vskip\thm@postskip
\fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
\NewExpandableDocumentCommand{\rfst@thm@detok}{o m m m}{
\detokenize{{#2}{#3}{#4}}
}
\NewDocumentEnvironment{store}{ m o +b } {%
\IfValueTF{#2}{\begin{#1}[#2]}{\begin{#1}} % Begin wrapped theorem
\newtoks\rfst@labelval % Capture the first label.
\let\rfst@oldlabel\label
\newcommand{\rfst@label}[1]{\rfst@labelval{##1}\let\label\rfst@oldlabel\label{##1}}
\let\label\rfst@label
#3 % Display the actual theorem contents
% \end{macrocode}
% Store all the info needed to refstate the theorem into the aux file
% \begin{macrocode}
\begingroup
\let\@thm=\rfst@thm@detok
\protected@write\@auxout{}{%
\string\global\string\long\string\@namedef{rfst@storedthm@\the\rfst@labelval}{%
\string\rfst@faketheorem%
{\the\rfst@labelval}%
{\tl_use:c{rfst@style@#1}}{#1}{\tl_use:c{rfst@name@#1}}
{\detokenize{#3}}%
\IfValueT{#2}{[#2]}%
}
}
\endgroup
\end{#1} % End wrapped theorem
} {}
% \end{macrocode}
% \end{implementation}
%
% \iffalse
%</package>
% \fi
%
% \Finale
\endinput