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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{sphinxpackagefootnote}%
[2021/02/04 v1.1d footnotehyper adapted to sphinx (Sphinx team)]
% Provides support for this output mark-up from Sphinx latex writer:
% - footnote environment
% - savenotes environment (table templates)
% - \sphinxfootnotemark
%
%%
%% Package: sphinxpackagefootnote
%% Version: based on footnotehyper.sty 2021/02/04 v1.1d
%% as available at https://www.ctan.org/pkg/footnotehyper
%% License: the one applying to Sphinx
%%
%% Refer to the PDF documentation at https://www.ctan.org/pkg/footnotehyper for
%% the code comments.
%%
%% Differences:
%% 1. a partial tabulary compatibility layer added (enough for Sphinx mark-up),
%% 2. use of \spx@opt@BeforeFootnote from sphinx.sty,
%% 3. use of \sphinxunactivateextrasandspace from sphinx.sty,
%% 4. macro definition \sphinxfootnotemark,
%% 5. macro definition \sphinxlongtablepatch
%% 6. replaced some \undefined by \@undefined
\newif\iffootnotehyperparse\footnotehyperparsetrue
\DeclareOption*{\PackageWarning{sphinxpackagefootnote}{Option `\CurrentOption' is unknown}}%
\ProcessOptions\relax
\newbox\FNH@notes
\newtoks\FNH@toks % 1.1c
\newdimen\FNH@width
\let\FNH@colwidth\columnwidth
\newif\ifFNH@savingnotes
\AtBeginDocument {%
\let\FNH@latex@footnote \footnote
\let\FNH@latex@footnotetext\footnotetext
\let\FNH@H@@footnotetext \@footnotetext
\let\FNH@H@@mpfootnotetext \@mpfootnotetext
\newenvironment{savenotes}
{\FNH@savenotes\ignorespaces}{\FNH@spewnotes\ignorespacesafterend}%
\let\spewnotes \FNH@spewnotes
\let\footnote \FNH@footnote
\let\footnotetext \FNH@footnotetext
\let\endfootnote \FNH@endfntext
\let\endfootnotetext\FNH@endfntext
\@ifpackageloaded{hyperref}
{\ifHy@hyperfootnotes
\let\FNH@H@@footnotetext\H@@footnotetext
\let\FNH@H@@mpfootnotetext\H@@mpfootnotetext
\else
\let\FNH@hyper@fntext\FNH@nohyp@fntext
\fi}%
{\let\FNH@hyper@fntext\FNH@nohyp@fntext}%
}%
\def\FNH@hyper@fntext{\FNH@fntext\FNH@hyper@fntext@i}%
\def\FNH@nohyp@fntext{\FNH@fntext\FNH@nohyp@fntext@i}%
\def\FNH@fntext #1{%
\ifx\ifmeasuring@\@undefined
\expandafter\@secondoftwo\else\expandafter\@firstofone\fi
% these two lines modified for Sphinx (tabulary compatibility):
{\ifmeasuring@\expandafter\@gobbletwo\else\expandafter\@firstofone\fi}%
{\ifx\equation$\expandafter\@gobbletwo\fi #1}%$
}%
\long\def\FNH@hyper@fntext@i#1{%
\global\setbox\FNH@notes\vbox
{\unvbox\FNH@notes
\FNH@startnote
\@makefntext
{\rule\z@\footnotesep\ignorespaces
\ifHy@nesting\expandafter\ltx@firstoftwo
\else\expandafter\ltx@secondoftwo
\fi
{\expandafter\hyper@@anchor\expandafter{\Hy@footnote@currentHref}{#1}}%
{\Hy@raisedlink
{\expandafter\hyper@@anchor\expandafter{\Hy@footnote@currentHref}%
{\relax}}%
\let\@currentHref\Hy@footnote@currentHref
\let\@currentlabelname\@empty
#1}%
\@finalstrut\strutbox
}%
\FNH@endnote
}%
}%
\long\def\FNH@nohyp@fntext@i#1{%
\global\setbox\FNH@notes\vbox
{\unvbox\FNH@notes
\FNH@startnote
\@makefntext{\rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
\FNH@endnote
}%
}%
\def\FNH@startnote{%
\hsize\FNH@colwidth
\interlinepenalty\interfootnotelinepenalty
\reset@font\footnotesize
\floatingpenalty\@MM
\@parboxrestore
\protected@edef\@currentlabel{\csname p@\@mpfn\endcsname\@thefnmark}%
\color@begingroup
}%
\def\FNH@endnote{\color@endgroup}%
\def\FNH@savenotes{%
\begingroup
\ifFNH@savingnotes\else
\FNH@savingnotestrue
\let\@footnotetext \FNH@hyper@fntext
\let\@mpfootnotetext \FNH@hyper@fntext
\let\H@@mpfootnotetext\FNH@nohyp@fntext
\FNH@width\columnwidth
\let\FNH@colwidth\FNH@width
\global\setbox\FNH@notes\box\voidb@x
\let\FNH@thempfn\thempfn
\let\FNH@mpfn\@mpfn
\ifx\@minipagerestore\relax\let\@minipagerestore\@empty\fi
\expandafter\def\expandafter\@minipagerestore\expandafter{%
\@minipagerestore
\let\thempfn\FNH@thempfn
\let\@mpfn\FNH@mpfn
}%
\fi
}%
\def\FNH@spewnotes {%
\if@endpe\ifx\par\@@par\FNH@toks{}\else
\FNH@toks\expandafter{\expandafter
\def\expandafter\par\expandafter{\par}\@endpetrue}%
\expandafter\expandafter\expandafter
\FNH@toks
\expandafter\expandafter\expandafter
{\expandafter\the\expandafter\FNH@toks
\expandafter\def\expandafter\@par\expandafter{\@par}}%
\expandafter\expandafter\expandafter
\FNH@toks
\expandafter\expandafter\expandafter
{\expandafter\the\expandafter\FNH@toks
\expandafter\everypar\expandafter{\the\everypar}}\fi
\else\FNH@toks{}\fi
\expandafter
\endgroup\the\FNH@toks
\ifFNH@savingnotes\else
\ifvoid\FNH@notes\else
\begingroup
\let\@makefntext\@empty
\let\@finalstrut\@gobble
\let\rule\@gobbletwo
\ifx\@footnotetext\@mpfootnotetext
\expandafter\FNH@H@@mpfootnotetext
\else
\expandafter\FNH@H@@footnotetext
\fi{\unvbox\FNH@notes}%
\endgroup
\fi
\fi
}%
\def\FNH@footnote@envname {footnote}%
\def\FNH@footnotetext@envname{footnotetext}%
\def\FNH@footnote{%
% this line added for Sphinx:
\spx@opt@BeforeFootnote
\ifx\@currenvir\FNH@footnote@envname
\expandafter\FNH@footnoteenv
\else
\expandafter\FNH@latex@footnote
\fi
}%
\def\FNH@footnoteenv{%
% this line added for Sphinx (footnotes in parsed literal blocks):
\catcode13=5 \sphinxunactivateextrasandspace
\@ifnextchar[%
\FNH@footnoteenv@i %]
{\stepcounter\@mpfn
\protected@xdef\@thefnmark{\thempfn}%
\@footnotemark
\def\FNH@endfntext@fntext{\@footnotetext}%
\FNH@startfntext}%
}%
\def\FNH@footnoteenv@i[#1]{%
\begingroup
\csname c@\@mpfn\endcsname #1\relax
\unrestored@protected@xdef\@thefnmark{\thempfn}%
\endgroup
\@footnotemark
\def\FNH@endfntext@fntext{\@footnotetext}%
\FNH@startfntext
}%
\def\FNH@footnotetext{%
\ifx\@currenvir\FNH@footnotetext@envname
\expandafter\FNH@footnotetextenv
\else
\expandafter\FNH@latex@footnotetext
\fi
}%
\def\FNH@footnotetextenv{%
\@ifnextchar[%
\FNH@footnotetextenv@i %]
{\protected@xdef\@thefnmark{\thempfn}%
\def\FNH@endfntext@fntext{\@footnotetext}%
\FNH@startfntext}%
}%
\def\FNH@footnotetextenv@i[#1]{%
\begingroup
\csname c@\@mpfn\endcsname #1\relax
\unrestored@protected@xdef\@thefnmark{\thempfn}%
\endgroup
\ifFNH@savingnotes
\def\FNH@endfntext@fntext{\FNH@nohyp@fntext}%
\else
\def\FNH@endfntext@fntext{\FNH@H@@footnotetext}%
\fi
\FNH@startfntext
}%
\def\FNH@startfntext{%
\setbox\z@\vbox\bgroup
\FNH@startnote
\FNH@prefntext
\rule\z@\footnotesep\ignorespaces
}%
\def\FNH@endfntext {%
\@finalstrut\strutbox
\FNH@postfntext
\FNH@endnote
\egroup
\begingroup
\let\@makefntext\@empty\let\@finalstrut\@gobble\let\rule\@gobbletwo
\FNH@endfntext@fntext {\unvbox\z@}%
\endgroup
}%
\let\FNH@prefntext\@empty
\let\FNH@postfntext\@empty
\AtBeginDocument{\iffootnotehyperparse\expandafter\FNH@check\fi}%
\def\FNH@safeif#1{%
\iftrue\csname if#1\endcsname\csname fi\endcsname\expandafter\@firstoftwo
\else\csname fi\endcsname\expandafter\@secondoftwo
\fi
}%
\def\FNH@check{%
\ifx\@makefntextFB\@undefined\expandafter\FNH@check@
\else\expandafter\FNH@frenchb@
\fi
}%
\def\FNH@frenchb@{%
\def\FNH@prefntext{%
\localleftbox{}%
\let\FBeverypar@save\FBeverypar@quote
\let\FBeverypar@quote\relax
\FNH@safeif{FB@koma}%
{\FNH@safeif{FBFrenchFootnotes}%
{\ifx\footnote\thanks
\let\@@makefnmark\@@makefnmarkTH
\@makefntextTH{} % space as in french.ldf
\else
\let\@@makefnmark\@@makefnmarkFB
\@makefntextFB{} % space as in french.ldf
\fi
}{\let\@@makefnmark\@@makefnmarkORI
\@makefntextORI{}% no space as in french.ldf
}%
}%
{\FNH@safeif{FBFrenchFootnotes}%
{\@makefntextFB{}}%
{\@makefntextORI{}}%
}%
}%
\def\FNH@postfntext{%
\let\FBeverypar@quote\FBeverypar@save
\localleftbox{\FBeveryline@quote}%
}%
}%
\def\FNH@check@{%
\expandafter\FNH@check@a\@makefntext{1.2!3?4,}%
\FNH@@@1.2!3?4,\FNH@@@\relax
}%
\long\def\FNH@check@a #11.2!3?4,#2\FNH@@@#3{%
\ifx\relax#3\expandafter\FNH@checkagain@
\else
\def\FNH@prefntext{#1}\def\FNH@postfntext{#2}%
\expandafter\FNH@check@b
\fi
}%
\def\FNH@checkagain@{%
\expandafter\FNH@checkagain@a
\detokenize\expandafter{\@makefntext{1.2!3?4,}}\relax\FNH@@@
}%
\edef\FNH@temp{\noexpand\FNH@checkagain@a ##1\string{1.2!3?4,\string}}%
\expandafter\def\FNH@temp#2#3\FNH@@@{%
\ifx\relax#2%
\def\FNH@prefntext{\@makefntext{}}%
\else\FNH@bad@makefntext@alert
\fi
}%
\def\FNH@check@b #1\relax{%
\expandafter\expandafter\expandafter\FNH@check@c
\expandafter\meaning\expandafter\FNH@prefntext
\meaning\FNH@postfntext1.2!3?4,\FNH@check@c\relax
}%
\def\FNH@check@c #11.2!3?4,#2#3\relax{%
\ifx\FNH@check@c#2\else\FNH@bad@makefntext@alert\fi
}%
% slight reformulation for Sphinx
\def\FNH@bad@makefntext@alert{%
\PackageWarningNoLine{sphinxpackagefootnote}%
{Footnotes will be sub-optimal, sorry. This is due to the document class or^^J
some package modifying macro \string\@makefntext.^^J
You can try to report this incompatibility at^^J
https://github.com/sphinx-doc/sphinx with this info:}%
\typeout{\meaning\@makefntext}%
\let\FNH@prefntext\@empty\let\FNH@postfntext\@empty
}%
% this macro from original footnote.sty is not used anymore by Sphinx
% but for simplicity sake let's just keep it as is
\def\makesavenoteenv{\@ifnextchar[\FNH@msne@ii\FNH@msne@i}%]
\def\FNH@msne@i #1{%
\expandafter\let\csname FNH$#1\expandafter\endcsname %$
\csname #1\endcsname
\expandafter\let\csname endFNH$#1\expandafter\endcsname %$
\csname end#1\endcsname
\FNH@msne@ii[#1]{FNH$#1}%$
}%
\def\FNH@msne@ii[#1]#2{%
\expandafter\edef\csname#1\endcsname{%
\noexpand\savenotes
\expandafter\noexpand\csname#2\endcsname
}%
\expandafter\edef\csname end#1\endcsname{%
\expandafter\noexpand\csname end#2\endcsname
\noexpand\expandafter
\noexpand\spewnotes
\noexpand\if@endpe\noexpand\@endpetrue\noexpand\fi
}%
}%
%
% some extras for Sphinx :
% \sphinxfootnotemark: usable in section titles and silently removed from TOCs.
\def\sphinxfootnotemark [#1]%
{\ifx\thepage\relax\else\sphinxfootref{#1}\fi}%
% \sphinxfootref:
% - \spx@opt@BeforeFootnote is from BeforeFootnote sphinxsetup option
% - \ref:
% the latex.py writer inserts a \phantomsection\label{<scope>.<num>}
% whenever
% - the footnote was explicitly numbered in sources,
% - or it was in restrained context and is rendered using footnotetext
%
% These are the two types of footnotes that \sphinxfootnotemark must
% handle. But for explicitly numbered footnotes the same number
% can be found in document. So a secondary part in <scope> is updated
% at each novel such footnote to know what is the target from then on
% for \sphinxfootnotemark and already encountered [1], or [2],...
%
% LaTeX package varioref is not supported by hyperref (from its doc: "There
% are too many problems with varioref. Nobody has time to sort them out.
% Therefore this package is now unsupported.") So we will simply use our own
% macros to access the page number of footnote text and decide whether to print
% it. \pagename is internationalized by latex-babel.
\def\spx@thefnmark#1#2{%
% #1=label for reference, #2=page where footnote was printed
\ifx\spx@tempa\spx@tempb
% same page
#1%
\else
\sphinxthefootnotemark{#1}{#2}%
\fi
}%
\def\sphinxfootref@get #1#2#3#4#5\relax{%
\def\sphinxfootref@label{#1}%
\def\sphinxfootref@page {#2}%
\def\sphinxfootref@Href {#4}%
}%
\protected\def\sphinxfootref#1{% #1 always explicit number in Sphinx usage
\spx@opt@BeforeFootnote
\ltx@ifundefined{r@\thesphinxscope.#1}%
{\gdef\@thefnmark{?}\H@@footnotemark}%
{\expandafter\expandafter\expandafter\sphinxfootref@get
\csname r@\thesphinxscope.#1\endcsname\relax
\edef\spx@tempa{\thepage}\edef\spx@tempb{\sphinxfootref@page}%
\protected@xdef\@thefnmark{\spx@thefnmark{\sphinxfootref@label}{\sphinxfootref@page}}%
\let\spx@@makefnmark\@makefnmark
\def\@makefnmark{%
\hyper@linkstart{link}{\sphinxfootref@Href}%
\spx@@makefnmark
\hyper@linkend
}%
\H@@footnotemark
\let\@makefnmark\spx@@makefnmark
}%
}%
\AtBeginDocument{%
% let hyperref less complain
\pdfstringdefDisableCommands{\def\sphinxfootnotemark [#1]{}}%
% to obtain hyperlinked footnotes in longtable environment we must replace
% hyperref's patch of longtable's patch of \@footnotetext by our own
\let\LT@p@ftntext\FNH@hyper@fntext
% this *requires* longtable to be used always wrapped in savenotes environment
}%
\endinput
%%
%% End of file `sphinxpackagefootnote.sty'.
|