summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/thmtools/source/unique.dtx
blob: f2fff48a07e3c19ad51c6f46827b11445c74a71e (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
% \iffalse meta-comment
%
% Copyright (C) 2005-2014 by Ulrich M. Schwarz
% Copyright (C) 2019      by Frank Mittelbach
% Copyright (C) 2020-     by Yukai Chou
%
% This file may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, version 1.3c.
% The license can be obtained from
% http://www.latex-project.org/lppl/lppl-1.3c.txt
%
% \fi
%
%\iffalse
%<*code>
%\fi
%    Two macros are provided: |\setuniqmark| takes a single parameter,
%    the name, which should be a string of letters. |\ifuniq| takes
%    three parameters: a name, a true-part and a false-part. The true
%    part is executed if and only if there was exactly one call to
%    |\setuniqmark| with the given name during the previous \LaTeX\ run.
%
%    Example application: legal documents are often very strongly numbered.
%    However, if a section has only a single paragraph, this paragraph is
%    not numbered separately, this only occurs from two paragraphs onwards.
%
%    It's also possible to not-number the single theorem in your paper, but
%    fall back to numbering when you add another one.
%
%     \StopEventually{}
%
%    \begin{macrocode}

\DeclareOption{unq}{%
  \newwrite\uniq@channel
  \InputIfFileExists{\jobname.unq}{}{}%
  \immediate\openout\uniq@channel=\jobname.unq
  \AtEndDocument{%
    \immediate\closeout\uniq@channel%
  }
}
\DeclareOption{aux}{%
  \let\uniq@channel\@auxout
}

%    \end{macrocode}
%
%
%    \iffalse $Id: unique.dtx,v 1.4 2010/04/25 19:37:46 ulmi Exp $\fi
%    \begin{macro}{\setuniqmark}
%    Call this with a name to set the corresponding uniqmark. The name must
%    be suitable for |\csname|-constructs, i.e. fully expansible to a
%    string of characters. If you use some counter values to generate this,
%    it might be a good idea to try and use hyperref's |\theH...| macros,
%    which have similar restrictions. You can check whether a particular
%    |\setuniqmark| was called more than once during \emph{the last run}
%    with |\ifuniq|.
%    \begin{macrocode}
\newcommand\setuniqmark[1]{%
  \expandafter\ifx\csname uniq@now@#1\endcsname\relax
    \global\@namedef{uniq@now@#1}{\uniq@ONE}%
  \else
    \expandafter\ifx\csname uniq@now@#1\endcsname\uniq@MANY
    \else
      \immediate\write\uniq@channel{%
        \string\uniq@setmany{#1}%
      }%
      \ifuniq{#1}{%
        \uniq@warnnotunique{#1}%
      }{}%
    \fi
    \global\@namedef{uniq@now@#1}{\uniq@MANY}%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%    
%    \begin{macro}{\ifuniq}
%    Companion to |\setuniqmark|: if the uniqmark given in the first
%    argument was called more than once, execute the second argument,
%    otherwise execute the third argument. Note that no call to
%    |\setuniqmark| for a particular uniqmark at all means that this
%    uniqmark is unique.
%    
%    This is a lazy version: we could always say false if we already had two
%    calls to |\setuniqmark| this run, but we have to rerun for any |\ifuniq|
%    prior to the first setuniqmark anyway, so why bother?
%    \begin{macrocode}
\newcommand\ifuniq[1]{%
  \expandafter\ifx\csname uniq@last@#1\endcsname\uniq@MANY
    \expandafter\@secondoftwo
  \else
    \expandafter\@firstoftwo
  \fi
}
%    \end{macrocode}
%    \end{macro}
%    
%    Two quarks to signal if we have seen an uniqmark more than once.
%    \begin{macrocode}
\def\uniq@ONE{\uniq@ONE}
\def\uniq@MANY{\uniq@MANY}
%    \end{macrocode}
%    Flag: suggest a rerun?
%    \begin{macrocode}
\newif\if@uniq@rerun
%    \end{macrocode}
%    
%    Helper macro: a call to this is written to the .aux file when we see
%    an uniqmark for the second time. This sets the right information for
%    the next run. It also checks on subsequent runs if the number of
%    uniqmarks drops to less than two, so that we'll need a rerun.
%    \begin{macrocode}
\def\uniq@setmany#1{%
  \global\@namedef{uniq@last@#1}{\uniq@MANY}%
  \AtEndDocument{%
    \uniq@warnifunique{#1}%
  }%
}
%    \end{macrocode}
%    
%    Warning if something is unique now. This always warns if the
%    setting for this run is not ``many'', because it was generated
%    by a setmany from the last run.
%    \begin{macrocode}
\def\uniq@warnifunique#1{%
  \expandafter\ifx\csname uniq@now@#1\endcsname\uniq@MANY\else
    \PackageWarningNoLine{uniq}{%
      `#1' is unique now.\MessageBreak
      Rerun LaTeX to pick up the change%
    }%
    \@uniq@reruntrue
  \fi
}
%    \end{macrocode}
%    
%    Warning if we have a second uniqmark this run around. Since this is
%    checked immediately, we could give the line of the second
%    occurence, but we do not do so for symmetry.
%    \begin{macrocode}
\def\uniq@warnnotunique#1{%
  \PackageWarningNoLine{uniq}{%
    `#1' is not unique anymore.\MessageBreak
    Rerun LaTeX to pick up the change%
  }%
  \@uniq@reruntrue
}
%    \end{macrocode}
%    
%    Maybe advise a rerun (duh!). This is executed at the end of the
%    second reading of the aux-file. If you manage to set uniqmarks
%    after that (though I cannot imagine why), you might need reruns
%    without being warned, so don't to that.
%    \begin{macrocode}
\def\uniq@maybesuggestrerun{%
  \if@uniq@rerun
    \PackageWarningNoLine{uniq}{%
      Uniquenesses have changed. \MessageBreak
      Rerun LaTeX to pick up the change%
    }%
  \fi
}
%    \end{macrocode}
%    
%    Make sure the check for rerun is pretty late in processing, so it
%    can catch all of the uniqmarks (hopefully).
%    \begin{macrocode}
\AtEndDocument{%
  \immediate\write\@auxout{\string\uniq@maybesuggestrerun}%
}
\ExecuteOptions{aux}
\ProcessOptions\relax
%    \end{macrocode}
%\iffalse
%</code>
%\fi