summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/proof-at-the-end/proof-at-the-end.sty
blob: 66a27077b5e1b08eba117e59cc0aa893497e0baa (plain)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
%%
%% This is file `proof-at-the-end.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% proof-at-the-end.dtx  (with options: `proof-at-the-end.sty,package')
%% 
%%  proof-at-the-end.dtx
%%  Copyright 2019 Léo Colisson
%% 
%%  This work may be distributed and/or modified under the
%%  conditions of the LaTeX Project Public License, either version 1.3
%%  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.
%% 
%%  This work has the LPPL maintenance status `maintained'.
%% 
%%  The Current Maintainer of this work is Léo Colisson.
%% 
%%  This work consists of the files proof-at-the-end.dtx and proof-at-the-end.ins and the derived file proof-at-the-end.sty.
%% 
%% \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         \~}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{proof-at-the-end}[2019/05/21 A package to move proofs in appendix]

\RequirePackage{etoolbox}
\RequirePackage{hyperref}
\RequirePackage{thmtools}
\RequirePackage{thm-restate}
\RequirePackage{catchfile}
\RequirePackage{pgfkeys}
\RequirePackage{xparse}

\RequirePackage{kvoptions}

%% https://tex.stackexchange.com/questions/109747/put-all-package-options-into-one-command
%% Forward the options list to the command \pratendOptdefconf
%% in order to use:
%% \usepackage[conf={normal}]{proof-at-the-end}
\SetupKeyvalOptions{
  family=pratendOpt,
  prefix=pratendOpt,
}
\DeclareStringOption{conf}
\DeclareLocalOptions{conf}% \pratendOptdefconf contains the proof
\ProcessKeyvalOptions*


\newwrite\appendwrite

\NewDocumentCommand\appendtofile{m+m}{%
  \begingroup
  \IfFileExists{#1}%
  {\CatchFileDef{\filecontent}{#1}{\catcode`\\=12 \endlinechar=`^^J\catcode\endlinechar=12\relax}}% keep existing end-of-lines
  {\let\filecontent\empty}%
  \immediate\openout\appendwrite=#1\relax
  \immediate\write\appendwrite{\detokenize\expandafter{\filecontent}#2}%
  \immediate\closeout\appendwrite
  \endgroup
}

%% This functions takes one input: the category (without .tex),
%% If it's the first time we write in this
%% category file, it "blanks" it.
\def\prefixPrAtEndFiles{pratend}
\newcommand*\eraseIfNeeded[1]{%
  % A macro 'pratendmacrocat{category}' is created to check
  % if it's the first time we write in this category file.
  \protected@edef\macroname{pratendmacrocat#1}%
  \ifcsdef{\macroname}{% The macro exists, nothing to do
  }{ % The macro does not exists, create it, and empty the file
    \global\expandafter\def\csname \macroname\endcsname{true}%
    \immediate\openout\appendwrite=\prefixPrAtEndFiles#1.tex%
    \immediate\write\appendwrite{}%
    \immediate\closeout\appendwrite%
  }%
}

\newif\ifproofhere
\newif\ifproofend
\newif\ifrestatethm
\newif\iflinktoproof
\newif\ifboth
\newif\ifallattheend
\newif\ifrestatedbefore
\pgfkeys{
  /prAtEnd/.cd, %% Proof at end will be the main path
  %% Category of the proof (if you want to put proofs
  %% at several places), can be anything
  category/.initial=defaultcategory,
  category/.store in=\category,
  category/.get=\category,
  %% Display the proof in the main part
  proof here/.is if=proofhere,
  no proof here/.style={proof here=false}, % alias
  %% Display the proof when using \printProofs
  proof end/.is if=proofend,
  no proof end/.style={proof end=false},
  %% Restate the theorem when using \printProofs
  restate/.is if=restatethm,
  no restate/.style={restate=false},
  %% Put a link to the proof after the theorem
  link to proof/.is if=linktoproof,
  no link to proof/.style={link to proof=false},
  %% Put the theorem and proof only in appendix
  opt all end/.is if=allattheend,
  no opt all end/.style={opt all end=false},
  %% Text of link
  text link/.code={\def\pratendtextlink{#1}},
  %% Text of proof. Make sure also to "\renewcommand*{\proofname}{Name of the proof}"
  %% to make sure the proof for normal theorems are changed
  text proof/.code={\def\pratendtextproof{#1}},
  %% Custom restate command
  restate command/.code={\protected@edef\pratendcustomrestate{#1}},
  %% (Re)stated before
  %% If the theorems has been stated before, then we just need to put the restate command in
  %% place of the argument, and we set this value to true:
  restated before/.is if=restatedbefore,
  no restated before/.style={restated before=false},
  %% In star version, we don't want
  %% Put the text (defined only for \textInAppendix) in both the
  %% current location and in appendix
  both/.is if=both,
  no both/.style={both=false},
  %%%% Alias and styles
  normal/.style={
    proof here,
    no opt all end,
    no proof end,
    no link to proof,
    no restate,
    no both,
  },
  proof at the end/.style={
    no proof here,
    no opt all end,
    proof end,
    no both,
  },
  end/.style={
    proof at the end,
    link to proof,
  },
  all end/.style={
    end,
    opt all end,
  },
  debug/.style={
    no opt all end,
    proof here
  },
  no link to theorem/.style={ % Remove the link to the theorem
    text proof={\proofname},
  },
  stared/.style={ % Remove
    text proof={\string\mbox{\string\hyperref[thm:prAtEnd\pratendcountercurrent]{\proofname}}},
  },
  no number/.style={
    stared
  },
  see full proof/.style={
    text link={See \hyperref[proof:prAtEnd\pratendcountercurrent]{full proof} on page~\pageref{proof:prAtEnd\pratendcountercurrent}.}
  },
  one big link/.style={
    text link={\hyperref[proof:prAtEnd\pratendcountercurrent] {#1}}
  },
  one big link/.default={%
    See proof on page~\pageref*{proof:prAtEnd\pratendcountercurrent}.
  },
  one big link translated/.style={
    one big link={#1~\pageref*{proof:prAtEnd\pratendcountercurrent}.}
  },
  text link section/.style={%
      text link={See \hyperref[proof:prAtEnd\pratendcountercurrent]{proof} in \autoref{proofsection:prAtEnd\pratendcountercurrent}.}
  },
  text link section full proof/.style={%
      text link={See \hyperref[proof:prAtEnd\pratendcountercurrent]{full proof} in \autoref{proofsection:prAtEnd\pratendcountercurrent}.}
  },
  default text link/.style={
    text link={See \hyperref[proof:prAtEnd\pratendcountercurrent]{proof} on page~\pageref{proof:prAtEnd\pratendcountercurrent}.},
  },
  text proof translated/.style={
    text proof={#1 \string\autoref{thm:prAtEnd\pratendcountercurrent}},
  },
  default text proof/.style={
    text proof={Proof of \string\autoref{thm:prAtEnd\pratendcountercurrent}},
  },
  %%%% Defaults
  bare defaults/.style={
    end,
    link to proof,
    no restate,
    category=defaultcategory,
    default text link,
    default text proof,
    restate command=pratenddummymacro,
  },
  configuration options/.style/.expand once={
    % This styles will contain the configuration
    % given as options of the package like:
    % \usepackage[conf={normal, no link to proof}]{proof-at-the-end}
    % The package options does not accept macros and valued keys
    % due to some fundamental issues:
    % https://tex.stackexchange.com/questions/489564/use-unexpanded-macro-in-package-options/489570#489570
    % so if you need to write macro/valued key, edit instead the
    % style "/prAtEnd/global custom defaults" or
    % "local /prAtEnd/custom defaults" for local changes instead.
    \pratendOptconf%
  },
  global custom defaults/.style={
    %% you can put in this style any global defaults
    %% that should overwrite the usual defaults.
  },
  local custom defaults/.style={
    %% you can put in this style any overwrite of the defaults
    %% that should be "local" and changed over the file, like
    % the category for a given section.
  },
  all defaults/.style={
    %% Load all the style that sets the default values
    bare defaults,
    configuration options,
    global custom defaults,
    local custom defaults,
  },
}

\newcounter{counterAllProofEnd}
\stepcounter{counterAllProofEnd}

\NewDocumentEnvironment{theoremEndRestateBefore}{mO{}m+b}{
  \stepcounter{counterAllProofEnd}%
  \protected@edef\currcounterval{\roman{counterAllProofEnd}}
  \protected@edef\temprest{\noexpand\begin{restatable*}[#2]{#1}{prAtEndRestate\currcounterval}\noexpand\label{thm:prAtEnd\currcounterval}}%
  \expandafter\protected@xdef\csname #3\endcsname{\currcounterval}%
  \temprest%
    #4%
  \end{restatable*}%
}{}

\NewDocumentEnvironment{theoremEnd}{O{}mO{}+b}{
  % The first facultative argument will be the options: type of proof you want, the file to which you want to write...
  % The first mandatory option is the type of the theorem (thm,lemma,...)
  % The second facultative argument will be the title
  % the second mandatory option is the theorem (will \label inside eventually)
  % the last mandatory option is the proof
  \global\def\pratendlastoptions{#1}%
  \pgfkeys{%
    /prAtEnd/.cd,
    all defaults,
    #1
  }%
  \stepcounter{counterAllProofEnd}%
  %% Pre-expand the restatable environment. Need protected
  %% otherwise can't have $\mathtt{G}$ in the title
  \protected@edef\temprest{\noexpand\begin{restatable}[#3]{#2}{prAtEndRestate\roman{counterAllProofEnd}}}%
  %% Create the file if it's the first time
  \eraseIfNeeded{\category}%
  %% If the theorem must be written here:
  \unless\ifallattheend%
    %% Restate the theorem if it was stated before:
    \ifrestatedbefore%
      \protected@xdef\pratendcountercurrent{\csname #4\endcsname} % Store the current (alpha value of the) counter
      \csname prAtEndRestate\pratendcountercurrent\endcsname % Restate the theorem
    \fi%
    %%  Otherwise just state the theorem in a restatable environment
    \unless\ifrestatedbefore%
      \temprest%
        \label{thm:prAtEnd\roman{counterAllProofEnd}}%
        #4%
      \end{restatable}%
      %% Store the current (alpha value of the) counter
      %% in \pratendcountercurrent
      \protected@xdef\pratendcountercurrent{\roman{counterAllProofEnd}} %
    \fi%
    %% Create a custom alias to restate the theorem
    \expandafter\protected@xdef\csname \pratendcustomrestate\endcsname{\noexpand\csname prAtEndRestate\pratendcountercurrent\endcsname}%
    %% Restate the theorem if needed in appendix
    \ifrestatethm
      \appendtofile{\prefixPrAtEndFiles\category.tex}{\string\prAtEndRestate\pratendcountercurrent*}
    \fi%
  \fi%
  %% If the theorem is not stated in the main text,
  %% write it at the end
  \ifallattheend%
    %% Store the current (alpha value of the) counter
    %% in \pratendcountercurrent
    \protected@xdef\pratendcountercurrent{\roman{counterAllProofEnd}}
    %% Create a custom alias to restate the theorem
    \expandafter\protected@xdef\csname \pratendcustomrestate\endcsname{\noexpand\csname prAtEndRestate\pratendcountercurrent\endcsname}%
    \appendtofile{\prefixPrAtEndFiles\category.tex}{\string\begin{restatable}[#3]{#2}{prAtEndRestate\pratendcountercurrent}\string\label{thm:prAtEnd\pratendcountercurrent}\detokenize{#4}\string\end{restatable}}%
  \fi%
}{}

\NewDocumentEnvironment{proofEnd}{O{}+b}{
  \pgfkeys{%
    /prAtEnd/.cd,
    all defaults,
    prAtEndTmpStyle/.style/.expand once={\pratendlastoptions},
    prAtEndTmpStyle,
    #1
  }%
  \unless\ifallattheend
    %% Write eventually a link to the proof
    \iflinktoproof%
      \pratendtextlink{}%
    \fi%
    %% And eventually the proof
    \ifproofhere%
      \begin{proof}%
        #2%
      \end{proof}%
    \fi%
  \fi%
  %% Write the proof at the end
  \ifproofend%
    \appendtofile{\prefixPrAtEndFiles\category.tex}{\string\label{proofsection:prAtEnd\pratendcountercurrent}\string\begin{proof}[\pratendtextproof]\string\phantomsection\string\label{proof:prAtEnd\pratendcountercurrent}\detokenize{#2}\string\end{proof}}%
  \fi%
}{}

%%%%% Text in appendix

\NewDocumentEnvironment{textAtEnd}{O{}+b}{
  % Use it to put normal text in Appendix.
  \pgfkeys{
    /prAtEnd/.cd,
    all defaults,
    #1
  }%
  \ifboth%
  #2%
  \fi%
  \eraseIfNeeded{\category}%
  \appendtofile{\prefixPrAtEndFiles\category.tex}{\detokenize{#2}}%
}{}

\NewDocumentCommand\textEnd{O{}+m}{%
  \begin{textAtEnd}[#1]%
    #2%
  \end{textAtEnd}%
}

\NewDocumentCommand\printProofs{O{defaultcategory}}{
  \input{\prefixPrAtEndFiles#1.tex}
}

%%% You can easily modify the defaults:
%%% Or create new styles to apply:

\endinput
%%
%% End of file `proof-at-the-end.sty'.