我是一名 LaTeX 新手。
我有一个名为的自定义宏\gloss
,它以某种方式装饰其参数 - 假设它为 MWE 的目的添加了一个星号。
我把它包装成一个\onetimegloss
宏,只调用它来第一次出现在文档中。
\section{}
为什么它在和内部不起作用\caption{}
,并且它会抱怨一个额外的}
?
(撇开使用预制词汇表包会更方便这一事实不谈。)
\documentclass[10pt,a4paper]{article}
\usepackage{ifthen}
\newcommand{\true}{1}
\newcommand{\false}{0}
\usepackage{pifont}
\newcommand{\gloss}[1]{#1\textsuperscript{\ding{72}}}
\def\addvalue#1{\expandafter\gdef\csname my@data@\detokenize{#1}\endcsname{\true}}
\def\usevalue#1{%
\ifcsname my@data@\detokenize{#1}\endcsname\true\else\false\fi
}
\newcommand{\onetimegloss}[1]{
\ifthenelse{
\equal{\usevalue{#1}}{\true}
}
{#1}
{\gloss{#1}\addvalue{#1}}
}
\begin{document}
\onetimegloss{foo}
\onetimegloss{bar}
\onetimegloss{foo}
\onetimegloss{bar}
\onetimegloss{baz}
% this outputs foo* bar* foo bar baz*
\section{\gloss{bam}}
This works ok
\section{\onetimegloss{foo}}
This breaks :(
\end{document}
其结果是:
! Undefined control sequence.
<argument> \equal
{\usevalue {foo}}{\true }
l.34 \section{\onetimegloss{foo}}
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
! Argument of \@tempc has an extra }.
<inserted text>
\par
l.34 \section{\onetimegloss{foo}}
I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.
Runaway argument?
{\TE@neg }\def \@tempb {\@tempc }\ifx \@tempa \@tempb \toks@ \expandafter \ETC.
! Paragraph ended before \@tempc was complete.
<to be read again>
\par
l.34 \section{\onetimegloss{foo}}
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.
答案1
section 的参数在多个地方使用。它可以在 header 和 toc 中徘徊。一些命令(“脆弱命令”,参见https://texfaq.org/FAQ-protect) 在这种情况下可能会中断。
要么使用
\protect\onetimegloss{foo}
或者
\section[text for toc and header]{\onetimegloss{foo}}
或者定义一个健壮的命令
\DeclareRobustCommand{\onetimegloss}[1]{...}
请注意,第一个词汇表调用可以位于目录中。