我在David Hanak的回答之后编辑了这个问题(顺便说一句,谢谢!)。他帮忙语法,但似乎我起初并没有使用正确的函数。
基本上我想让编译器忽略某个标签的多重定义,并且只使用第一个。为了做到这一点,我想要做的就是像这样:
makeatletter ewcommand{mylabel}[1]{ @ifundefined{#1}{label{#1}}{X} } makeatother
然而,这并不起作用,因为总是选择第一选项(无论标签是否被定义)。我认为@ifundefined(以及建议的ifundefined)只适用于命令而不适用于标签,但我不太了解LaTeX。希望有关此事的任何帮助!谢谢!
Much later update: I marked David Hanak s response as the correct answer to my question, but it isn t a complete solution, although it really helped me. The problem is, I think but I m no specialist, that even though David s code checks to see if a label is defined, it only works when the label was defined in a previous run (i.e. is in the .aux file). If two mylabels with the same name are defined in the same run, the second will still be defined. Also, even if you manage to work around this, it will make LaTeX use the first label that you defined chronologically, and not necessarily the first in the text. Anyway, below is my quick and dirty solution. It uses the fact that counters do seem to be defined right away.
ewcommand{mylabel}[1]{% @ifundefined{c@#1}{% ewcounter{#1}% setcounter{#1}{0}% }{}% ifthenelse{value{#1} > 0}{}{% label{#1}% addtocounter{#1}{1}% }% }
I m not sure if it is necessary to initialize the counter to 0, as it seems like a likely default, but I couldn t find if that was the case, so I m just being safe. Also, this uses the ifthen package, which I m not sure is necessary.