Debian Codegraph @

texmf-dist/source/latex/thmtools/unique.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2005-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
15 %<*code>
16 %\fi
17 % Two macros are provided: |\setuniqmark| takes a single parameter,
18 % the name, which should be a string of letters. |\ifuniq| takes
19 % three parameters: a name, a true-part and a false-part. The true
20 % part is executed if and only if there was exactly one call to
21 % |\setuniqmark| with the given name during the previous \LaTeX\ run.
22 %
23 % Example application: legal documents are often very strongly numbered.
24 % However, if a section has only a single paragraph, this paragraph is
25 % not numbered separately, this only occurs from two paragraphs onwards.
26 %
27 % It's also possible to not-number the single theorem in your paper, but
28 % fall back to numbering when you add another one.
29 %
30 % \StopEventually{}
31 %
32 % \begin{macrocode}
33
34 \DeclareOption{unq}{%
35 \newwrite\uniq@channel
36 \InputIfFileExists{\jobname.unq}{}{}%
37 \immediate\openout\uniq@channel=\jobname.unq
38 \AtEndDocument{%
39 \immediate\closeout\uniq@channel%
40 }
41 }
42 \DeclareOption{aux}{%
43 \let\uniq@channel\@auxout
44 }
45
46 % \end{macrocode}
47 %
48 %
49 % \iffalse $Id: unique.dtx,v 1.4 2010/04/25 19:37:46 ulmi Exp $\fi
50 % \begin{macro}{\setuniqmark}
51 % Call this with a name to set the corresponding uniqmark. The name must
52 % be suitable for |\csname|-constructs, i.e. fully expansible to a
53 % string of characters. If you use some counter values to generate this,
54 % it might be a good idea to try and use hyperref's |\theH...| macros,
55 % which have similar restrictions. You can check whether a particular
56 % |\setuniqmark| was called more than once during \emph{the last run}
57 % with |\ifuniq|.
58 % \begin{macrocode}
59 \newcommand\setuniqmark[1]{%
60 \expandafter\ifx\csname uniq@now@#1\endcsname\relax
61 \global\@namedef{uniq@now@#1}{\uniq@ONE}%
62 \else
63 \expandafter\ifx\csname uniq@now@#1\endcsname\uniq@MANY
64 \else
65 \immediate\write\uniq@channel{%
66 \string\uniq@setmany{#1}%
67 }%
68 \ifuniq{#1}{%
69 \uniq@warnnotunique{#1}%
70 }{}%
71 \fi
72 \global\@namedef{uniq@now@#1}{\uniq@MANY}%
73 \fi
74 }
75 % \end{macrocode}
76 % \end{macro}
77 %
78 % \begin{macro}{\ifuniq}
79 % Companion to |\setuniqmark|: if the uniqmark given in the first
80 % argument was called more than once, execute the second argument,
81 % otherwise execute the third argument. Note that no call to
82 % |\setuniqmark| for a particular uniqmark at all means that this
83 % uniqmark is unique.
84 %
85 % This is a lazy version: we could always say false if we already had two
86 % calls to |\setuniqmark| this run, but we have to rerun for any |\ifuniq|
87 % prior to the first setuniqmark anyway, so why bother?
88 % \begin{macrocode}
89 \newcommand\ifuniq[1]{%
90 \expandafter\ifx\csname uniq@last@#1\endcsname\uniq@MANY
91 \expandafter\@secondoftwo
92 \else
93 \expandafter\@firstoftwo
94 \fi
95 }
96 % \end{macrocode}
97 % \end{macro}
98 %
99 % Two quarks to signal if we have seen an uniqmark more than once.
100 % \begin{macrocode}
101 \def\uniq@ONE{\uniq@ONE}
102 \def\uniq@MANY{\uniq@MANY}
103 % \end{macrocode}
104 % Flag: suggest a rerun?
105 % \begin{macrocode}
106 \newif\if@uniq@rerun
107 % \end{macrocode}
108 %
109 % Helper macro: a call to this is written to the .aux file when we see
110 % an uniqmark for the second time. This sets the right information for
111 % the next run. It also checks on subsequent runs if the number of
112 % uniqmarks drops to less than two, so that we'll need a rerun.
113 % \begin{macrocode}
114 \def\uniq@setmany#1{%
115 \global\@namedef{uniq@last@#1}{\uniq@MANY}%
116 \AtEndDocument{%
117 \uniq@warnifunique{#1}%
118 }%
119 }
120 % \end{macrocode}
121 %
122 % Warning if something is unique now. This always warns if the
123 % setting for this run is not ``many'', because it was generated
124 % by a setmany from the last run.
125 % \begin{macrocode}
126 \def\uniq@warnifunique#1{%
127 \expandafter\ifx\csname uniq@now@#1\endcsname\uniq@MANY\else
128 \PackageWarningNoLine{uniq}{%
129 `#1' is unique now.\MessageBreak
130 Rerun LaTeX to pick up the change%
131 }%
132 \@uniq@reruntrue
133 \fi
134 }
135 % \end{macrocode}
136 %
137 % Warning if we have a second uniqmark this run around. Since this is
138 % checked immediately, we could give the line of the second
139 % occurence, but we do not do so for symmetry.
140 % \begin{macrocode}
141 \def\uniq@warnnotunique#1{%
142 \PackageWarningNoLine{uniq}{%
143 `#1' is not unique anymore.\MessageBreak
144 Rerun LaTeX to pick up the change%
145 }%
146 \@uniq@reruntrue
147 }
148 % \end{macrocode}
149 %
150 % Maybe advise a rerun (duh!). This is executed at the end of the
151 % second reading of the aux-file. If you manage to set uniqmarks
152 % after that (though I cannot imagine why), you might need reruns
153 % without being warned, so don't to that.
154 % \begin{macrocode}
155 \def\uniq@maybesuggestrerun{%
156 \if@uniq@rerun
157 \PackageWarningNoLine{uniq}{%
158 Uniquenesses have changed. \MessageBreak
159 Rerun LaTeX to pick up the change%
160 }%
161 \fi
162 }
163 % \end{macrocode}
164 %
165 % Make sure the check for rerun is pretty late in processing, so it
166 % can catch all of the uniqmarks (hopefully).
167 % \begin{macrocode}
168 \AtEndDocument{%
169 \immediate\write\@auxout{\string\uniq@maybesuggestrerun}%
170 }
171 \ExecuteOptions{aux}
172 \ProcessOptions\relax
173 % \end{macrocode}
174 %\iffalse
175 %</code>
176 %\fi