如果文本相同,是否可以告诉 LaTeX, \footnote{text} 使用相同的数字?
答案1
这是解决方案。(它有很多检查,因为它也接受自定义数字,并且如果已经给出了特定的脚注文本,则更新旧数字):
\documentclass{article}
\usepackage{lipsum}
\usepackage{pgffor}
\usepackage{xcolor}
\newcounter{myfootnote}
\let\oldfootnote=\footnote
\makeatletter
\def\footnote{\let\curfootnum\undefined\@ifnextchar[%
{\@ReadOptional}{\@ReadMandatory}}
\def\@ReadOptional[#1]{\def\curfootnum{#1}\@ReadMandatory}%
\def\@ReadMandatory#1{\xdef\curfoottext{#1}%
\ifnum\themyfootnote>0%
\xdef\Found{0}%
\foreach \n in {1,...,\themyfootnote}%
{\expandafter\ifx\csname FootText\n\endcsname\curfoottext%
\xdef\temppage{\csname FootLastPage\n\endcsname}%
\xdef\tempnum{\n}%\ifx\curfootnum\undefined\n\else\curfootnum\fi
\xdef\Found{1}%
\fi%
}%
\ifnum\Found=0%
\let\nextavail\undefined%
\foreach \f in {1,...,\themyfootnote}%
{\xdef\FoundNum{0}%
\foreach \l in {1,...,\themyfootnote}%
{\expandafter\ifnum\csname FootLastNumber\l\endcsname=\f%
\xdef\FoundNum{1}%
\breakforeach%
\fi}%
\ifnum\FoundNum=0%
\xdef\nextavail{\f}%
\fi%
}%
\refstepcounter{myfootnote}%
\ifx\nextavail\undefined%
\xdef\nextavail{\themyfootnote}%
\fi%
\xdef\tempn{\ifx\curfootnum\undefined\nextavail\else\curfootnum\fi}%
\global\expandafter\let\csname FootLastNumber\themyfootnote\endcsname\tempn%
\xdef\myp{\thepage}%
\global\expandafter\let\csname FootLastPage\themyfootnote\endcsname\myp%
\global\expandafter\let\csname FootText\themyfootnote\endcsname\curfoottext
\oldfootnote[\csname FootLastNumber\themyfootnote\endcsname]{\curfoottext}%
\else%
\ifnum\thepage=\temppage%
\ifx\curfootnum\undefined%
\footnotemark[\csname FootLastNumber\tempnum\endcsname]%
\else%
\xdef\myp{\thepage}%
\global\expandafter\let\csname FootLastPage\tempnum\endcsname\myp%
\global\expandafter\let\csname FootLastNumber\tempnum\endcsname\curfootnum%
\refstepcounter{myfootnote}%
\global\expandafter\let\csname FootLastNumber\themyfootnote\endcsname\tempnum%
\oldfootnote[\csname FootLastNumber\tempnum\endcsname]{\curfoottext}%
\fi%
\else%
\ifx\curfootnum\undefined%
\oldfootnote[\csname FootLastNumber\tempnum\endcsname]{\curfoottext}%
\else%
\global\expandafter\let\csname FootLastNumber\tempnum\endcsname\curfootnum%
\oldfootnote[\csname FootLastNumber\tempnum\endcsname]{\curfoottext}%
\refstepcounter{myfootnote}%
\global\expandafter\let\csname FootLastNumber\themyfootnote\endcsname\tempnum%
\fi%
\xdef\myp{\thepage}%
\global\expandafter\let\csname FootLastPage\tempnum\endcsname\myp%
\fi%
\fi%
\else%
\refstepcounter{myfootnote}\global\expandafter\let\csname FootText\themyfootnote\endcsname\curfoottext%
\xdef\temp{\thepage}%
\global\expandafter\let\csname FootLastPage\themyfootnote\endcsname\temp%
\xdef\temp{\ifx\curfootnum\undefined\themyfootnote\else\curfootnum\fi}%
\global\expandafter\let\csname FootLastNumber\themyfootnote\endcsname\temp%
\oldfootnote[\csname FootLastNumber\themyfootnote\endcsname]{\curfoottext}%
\fi}%
\makeatother
\begin{document}
\lipsum[1]
{\color{red}test1\footnote{test}}
\lipsum[1]
{\color{red}test2 same as 1\footnote{test}}
\lipsum[2]
{\color{red}test3 different\footnote{test2}}
\lipsum[1-3]
{\color{red}test4 same as 1 different page\footnote{test}}
\lipsum[1-2]
\lipsum[1-3]
{\color{red}test5 same as 2 different page\footnote{test2}}
\lipsum[1-3]
{\color{red}custom numbered\footnote[5]{custom numbered 5}}
\lipsum[1]
{\color{red} repeat custon numbered same page\footnote{custom numbered 5}}
\lipsum[1-5]
{\color{red} repeat custon numbered other page\footnote{custom numbered 5}}
\lipsum[1-3]
{\color{red}Again second\footnote{test2}}
\lipsum[1]{\color{red}A new one\footnote{test new}}
{\color{red}Another one double\footnote{test new two}\footnote{test new two}}
{\color{red}This has to be 6\footnote{test new three}}
{\color{red}From now 6 will forced to be 8\footnote[8]{test new three}}
\lipsum[1-2]
{\color{red} This footnote has to be 7\footnote{new text}}
\lipsum[1-2]
{\color{red} Repeating 8 to see if last number kept\footnote{test new three}}
\lipsum[1-2]
{\color{red} Custom updating footnote 2\footnote[2]{test new last}}\lipsum[3]
{\color{red} Repeating 2 to see if text "test new last" kept\footnote{test new last}}
\lipsum[3]
{\color{red} And numbers continues from 9\footnote{test new 9}}
\end{document}
这个想法是为了跟踪文本、最后一个数字(脚注标记)和每个脚注的最后一页。
如果您发现脚注中有已定义的文本,则将保留旧编号,除非您通过 更新它\footnote[<new number>]{old text}
。在最后一种情况下,将为以下相同的脚注保留给定的编号,但不会释放旧编号。
如果在同一页中发现两个相同的脚注,则页脚中只会打印一个。
如果您使用自定义编号定义脚注\footnote[<custom number>]{some text}
,那么该编号将不适用于下一个脚注,除非您更改它。
输出:
第 1 页:
第2页:
第 4 页:
第 6 页:
第 7 页: