summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/parnotes/parnotes.sty
blob: 65ed47046102076a01262d0e70cf2148a5131b8f (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
% The parnotes package
% Notes after every paragraph, or elsewhere
% Copyright (c) 2016 Chelsea Hughes
%
% This work is distributed under the LaTeX Project Public License,
% version 1.3 or later, available at
%     http://www.latex-project.org/lppl.txt
% I currently maintain this project (comprising parnotes.sty, parnotes.tex,
% README.md, and the derived file parnotes.pdf) and will receive error reports at
%     https://github.com/chelh/LaTeX-parnotes
%
% This package was started due to a question at the TeX Stack Exchange:
%     http://tex.stackexchange.com/questions/34746/
%
% Thanks to Stack Exchange users Ahmed Musa, Bruno Le Floch, and David Carlisle.
%
% -----------------------------
%
% The package is currently unmainted, i.e. the above address exists but is not monitored
%
% Small extensions in 2019 by Frank Mittelbach:
%
% option reset (alias for restart to match other packages)
% option alph  (for alph style notes instead of arabic)
% option symbol (for fnsymbol style notes)

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{parnotes}[2019/07/23 rev. 3b Notes after every paragraph, or elsewhere]

%
% These are the user-modifiable format commands
%

% Format for parnotes - \PN@notes@shape is blank (\relax) by default
\providecommand{\parnotefmt}[1]{\footnotesize%
                \PN@notes@shape\PN@narrower@optional\noindent #1}
% Number format (arabic, roman, etc.) for the parnote mark counter
\providecommand{\theparnotemark}{\arabic{parnotemark}}
% Minimum vertical space before and after parnotes
\providecommand{\parnotevskip}{\medskipamount}
% Full command used between parnotes
\providecommand{\parnoteintercmd}{\hspace{1em plus 0.3em minus 0.2em}}
% Basic formatting for parnote marks
\providecommand{\parnotecusmarkfmt}[1]{\textsuperscript{#1}}

%
% Internal variables below this line
%


% This holds all the parnote text
\global\def\PN@text{}
% Counter for parnote marks
\newcounter{parnotemark}
% autopn environment name
\def\@PN@autopn{autopn}
% True if currently setting parnotes
\newif\ifPN@inparnotes
% Commands used to disable indentation for the next paragraph
\def\PN@disable@indent{\@afterindentfalse\@afterheading}

%
% Optionally reset the counter within \parnotes
%
\let\PN@reset@optional\relax
\DeclareOption{restart}{\def\PN@reset@optional{\parnotereset}}
\DeclareOption{reset}{\def\PN@reset@optional{\parnotereset}}      %FMi alternative name
%
% Parnote marks are arabic by default, but an option changes them to roman
%
\DeclareOption{roman}{\renewcommand\theparnotemark{\roman{parnotemark}}}
%%FMi
\DeclareOption{alph}{\renewcommand\theparnotemark{\alph{parnotemark}}}
\DeclareOption{symbol}{\renewcommand\theparnotemark{\fnsymbol{parnotemark}}}
%%FMi -- end
% breakwithin just changes \parnoteintercmd
\DeclareOption{breakwithin}{\renewcommand{\parnoteintercmd}{\\}}
% And indentafter just clears \PN@disable@indent
\DeclareOption{indentafter}{\let\PN@disable@indent\relax}
%
% Set parnotes using \narrower if that option is specified
%
\let\PN@narrower@optional\relax
\DeclareOption{narrower}{\let\PN@narrower@optional\narrower}
% Allow the user to disable separators between multiple parnote marks
\DeclareOption{nomultiple}{\def\FN@mf@prepare{\relax}\def\FN@mf@check{\relax}}
%
% Font-changing options
%
\let\PN@notes@shape\relax
\DeclareOption{notesrm}{\let\PN@notes@shape\rmfamily}
\DeclareOption{notessf}{\let\PN@notes@shape\sffamily}
\DeclareOption{notesit}{\let\PN@notes@shape\itshape}
% Finally process the options
\ProcessOptions\relax

%
% Provide a null implementation of \phantomsection in case hyperref
% isn't loaded
%
\providecommand\phantomsection{}

% Later, we'll check to see if this command was redefined
\let\parnotemarkfmt\@gobble

%
% A wrapper for \PN@parnote@real. Takes an optional (manually-specified)
% mark and mandatory note text, and transforms them into the two mandatory
% arguments (mark and note text) for \PN@parnote@real. If no mark is
% specified, it will increment the counter parnotemark and pass its FULLY
% EXPANDED text value to \PN@parnote@real. That's two cases:
%
% 1: \parnote[MARK]{TEXT} -> \PN@parnote@real{MARK}{TEXT}
% 2: \parnote{TEXT}       -> \PN@parnote@real{EXPANDED COUNTER}{TEXT}
%
% \parnote supports behavior like the stable option of the package
% footmisc, by Robin Fairbairns: If encountered in the Table of Contents,
% it will do nothing and expand nothing. That adds two more cases:
%
% 3: \parnote[MARK]{TEXT} -> {}
% 4: \parnote{TEXT}       -> {}
%

\long\def\parnote{%
\ifx\@gobble\parnotemarkfmt\else
    % Crash if the user has defined \parnotemarkfmt, as this is
    % no longer supported
    \PackageError{parnotes}{%
        Cannot use custom definition of \protect\parnotemarkfmt\MessageBreak
        in parnotes rev. 2 or later. Use
        \protect\theparnotemark\MessageBreak
        and \protect\parnotecusmarkfmt\space instead%
    }{%
        You've used an option that is no longer supported.
        Downgrade back\MessageBreak
        to parnotes rev. 1, or redefine \protect\theparnotemark\space
        and \protect\parnotecusmarkfmt\MessageBreak
        instead of \protect\parnotemarkfmt. See section
        2.2 of the parnotes manual for\MessageBreak
        details.
    }%
\fi
% This incorporates changes suggested by David Carlisle
\relax\ifx\@footnotetext\TX@trial@ftn\else
    \ifx\protect\@typeset@protect
        \expandafter\expandafter\expandafter\PN@check@args
    \else
        \expandafter\expandafter\expandafter\@gobble
    \fi
\fi
}
\def\PN@check@args{%
        \@ifnextchar [
                \PN@parnote@twoargs% Case 1
                \PN@parnote@onearg% Case 2
}
\DeclareRobustCommand\PN@gobble@opt{%
    \@ifnextchar [
        \PN@gobble@opt@@% Case 3
        \@gobble% Case 4
}
\def\PN@parnote@twoargs[#1]#2{\PN@parnote@real{#1}{#2}}
\def\PN@parnote@onearg#1{%
    % Fully expand the new counter value, as text
    \begingroup\protected@edef\@x{\endgroup\noexpand\PN@parnote@real%
                                  {\theparnotemark}}\@x{#1}%
    \global\advance\c@parnotemark\@ne
}
\def\PN@gobble@opt@@[#1]#2{}

%
% Collects tokens into \PN@text, to be set when \parnotes is called.
% Everything added to \PN@text is expanded first. Takes two MANDATORY
% arguments, a mark and some note text.
%
\long\def\PN@parnote@real#1#2{%
    \parnotemark{#1}%
    % Unless this is the first parnote in \PN@text, add a separator first
    \unless\ifx\PN@text\@empty\g@addto@macro\PN@text{\parnoteintercmd}\fi
    % Redefine \@currentlabel to the parnote label, so \label works
    \g@addto@macro\PN@text{\phantomsection\def\@currentlabel{#1}}%
    \g@addto@macro\PN@text{\parnotemark{#1}\nolinebreak\thinspace#2}%
}

%
% Definitions to get multiple consecutive parnotes/footnotes working, as
% in footmisc and manyfoot; this is cribbed directly from footmisc
%
\providecommand*{\multiplefootnotemarker}{3sp}
\providecommand*{\multfootsep}{,}
\providecommand\FN@mf@prepare{%
    \kern-\multiplefootnotemarker
    \kern\multiplefootnotemarker\relax
}
\providecommand\FN@mf@check{%
    \ifdim\lastkern=\multiplefootnotemarker\relax
        \edef\@x@sf{\the\spacefactor}%
        \unkern
        \textsuperscript{\multfootsep}%
        \spacefactor\@x@sf\relax
    \fi
}

%
% Sets the parnote mark (included as a MANDATORY argument)
%
\newcommand\parnotemark[1]{%
    \leavevmode
    \ifhmode
        % Save the spacefactor, like \footnote
        \edef\@x@sf{\the\spacefactor}%
        \FN@mf@check
        \nobreak
    \fi
    \parnotecusmarkfmt{#1}%
    \FN@mf@prepare
    \ifhmode\spacefactor\@x@sf\fi
    \relax
}

%
% Calls \PN@parnotes@real, if there are parnotes to set.
%
\newcommand\parnotes{%
    \unless\ifx\PN@text\@empty
        \expandafter\PN@parnotes@real
    \fi
}

\def\PN@parnotes@real{%
    % We call \par later, so this avoids recursion with \PN@parnotes@auto
    \PN@inparnotestrue
    \unless\ifvmode\par\fi
    % Avoid page breaks between a paragraph and its parnotes
    \nopagebreak\addvspace{\parnotevskip}%
    {\parnotefmt{\PN@text}\par}%
    \global\def\PN@text{}%
    \addvspace{\parnotevskip}%
    %
    % These can be enabled or disabled by package options
    %
    \PN@disable@indent
    \PN@reset@optional
    \PN@inparnotesfalse
}

\newenvironment{autopn}{%
    %
    % Insert \PN@parnotes@auto into \par and \@par
    % We can't just do \@setpar{\PN@parnotes@auto\@@par} since autopn
    % could be nested within a list
    %
    \let\PN@par\par
    \def\par{\PN@parnotes@auto\PN@par}%
    \def\@par{\PN@parnotes@auto\@@par}%
}
{\PN@parnotes@auto} % At the environment's end, set any notes we've missed

%
% If autopn calls parnotes, check for nested environments, and that this
% isn't a call to \par from within \PN@parnotes@real, AND that there are
% parnotes to set
%
\def\PN@parnotes@auto{%
    \ifx\@currenvir\@PN@autopn
        \unless\ifPN@inparnotes
            \unless\ifx\PN@text\@empty
                \expandafter\PN@parnotes@real
            \fi
        \fi
    \fi
}

%
% Set parnote counter to 1
%
\newcommand\parnotereset{\setcounter{parnotemark}{1}}
% A variant also clears \PN@text
\newcommand\parnoteclear{%
    \gdef\PN@text{}%
    \parnotereset
}

%
% A wrapper around \ref to provide formatted parnote references
%
\newcommand\parnoteref[1]{\parnotemark{\ref{#1}}}

% Set parnote mark counter to 1 initially
\parnotereset