是否可以声明脚注并将其附加到第一的给定单词或短语的出现情况?
完全自动化地完成这项工作可能不太现实(见下面的链接),但如果(例如)使用脚注标签和宏\uniquefn[text]{id}
(标记应视为相同的短语),这应该是可能的。使用这样的宏,人们可以编写如下内容:
\documentclass{article}
\begin{document}
This is a sentence\uniquefn{an-identifier}.
Let's write\uniquefn[Every footnote identifier must have specified text
(inside an optional argument) exactly once for all invocations of this
macro with that identifier; if this is violated, there should be an
error.]{another-identifier}
two more sentences\uniquefn[If an optional argument is given, the text
within that optional argument is what is to be displayed]{an-identifier}.
This is the penultimate sentence\uniquefn{an-identifier}.
And one final sentence\uniquefn{an-identifier}
concludes\uniquefn[One more footnote.]{yet-another-identifier} this document.
\end{document}
达到相同效果的其他等效方法也同样有效。
相关且可能有用的:
答案1
我不太清楚您的实现。以下是您想要的初步想法。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\msg_new:nnnn { undefined_footnote }{ undefined_footnote }{ You've ~ attempted ~ to ~ use ~ an ~ undefined ~ footnote: ~ "#1" }{}
%% --------------------------------------------------------------------------------
%% arg #1 -> key value
%% arg #2 -> content of the footnote
\cs_new:Npn \__los_add_footnote_id:nn #1#2 {
\tl_if_exist:cF { #1 _footnote }
{
\tl_new:c { #1 _footnote }
\tl_gset:cn { #1 _footnote}{#2}
}
}
%% --------------------------------------------------------------------------------
%% arg #1 -> key value
\cs_new:Npn \__los_uniquefn:n #1
{
\int_incr:c { #1 _counter }
\int_compare:nT { \int_use:c { #1 _counter } = 1 }
{
\footnote
{
\tl_use:c { #1 _footnote }
}
}
}
%% --------------------------------------------------------------------------------
%% USER INTERFACE
%% --------------------------------------------------------------------------------
%% Define what should appear in the footnote
%% arg #1 -> key value
%% arg #2 -> content of the footnote
\NewDocumentCommand{\definefootnote}{ mm }
{
\__los_add_footnote_id:nn { #1 } { #2 }
\int_if_exist:cF { #1 _counter }
{
\int_gzero_new:c { #1 _counter }
}
}
%% Possibly create footnote
\NewDocumentCommand{\uniquefn}{ m }
{
\tl_if_exist:cTF { #1 _footnote }
{
\__los_uniquefn:n { #1 }
}
{
\msg_error:nnn { undefined_footnote } { undefined_footnote } { #1 }
}
}
\ExplSyntaxOff
\definefootnote{a-thistle-ftnt}{A thistle is a kind of weed which grows on the side of roads.}
\definefootnote{a-rose-garden}{A place to go and read books.}
\usepackage{lipsum}
\pagestyle{empty}
\begin{document}
\lipsum[1]
thistle\uniquefn{a-thistle-ftnt}
\lipsum[1]
rose garden\uniquefn{a-rose-garden}
\lipsum[1]
\lipsum[1]
\lipsum[1]
thistle\uniquefn{a-thistle-ftnt}
\lipsum[1]
\lipsum[1]
\end{document}
我不清楚的是,为什么要将脚注的内容放在可选参数中。您的实现似乎会让人头疼,因为要检查脚注的主体是否已在任何地方定义。定义一个图书馆对于可用于调用脚注定义的唯一 ID:这就是\definefootnote
应该提供的:因此您可以在一个位置写下所有可能的脚注的列表。
答案2
这会将文本保存在aux
文件中,因此需要一些运行才能稳定下来。
\documentclass{article}
\makeatletter
\newcommand\uniquefn[2][]{%
\ifx\relax#1\relax\else
\immediate\write\@auxout{\global\noexpand\@namedef{FTN-text-#2}{\detokenize{#1}}}%
\expandafter\ifx\csname FTN-text-#2\endcsname\relax
\expandafter\gdef\csname FTN-text-#2\endcsname{#1}%
\else
\def\tmp{#1}%
\expandafter\ifx\csname FTN-text-#2\endcsname\tmp\else
\PackageError{uniqueftn}{text for #2 is not unique}{make it so}%
\fi
\fi
\fi
\expandafter\ifx\csname FTN-seen-#2\endcsname\@empty\else
\footnote{\csname FTN-text-#2\endcsname}%
\fi
\global\expandafter\let\csname FTN-seen-#2\endcsname\@empty
}
\makeatother
\begin{document}
This is a sentence\uniquefn{an-identifier}.
Let's write\uniquefn[Every footnote identifier must have specified text
(inside an optional argument) exactly once for all invocations of this
macro with that identifier; if this is violated, there should be an
error.]{another-identifier}
two more sentences\uniquefn[If an optional argument is given, the text
within that optional argument is what is to be displayed]{an-identifier}.
This is the penultimate sentence\uniquefn{an-identifier}.
And one final sentence\uniquefn{an-identifier}
concludes\uniquefn[One more footnote.]{yet-another-identifier} this document.
\end{document}