计数并显示编译错误

计数并显示编译错误
\documentclass{article}

\def\ad#1{$^{#1}$}
\def\aff#1{$^{#1}$}
\def\address#1{#1}
\def\dept#1{#1}
\def\inst#1{#1}
\def\cyname#1{#1}
\def\country#1{#1}
\def\pcode#1{#1}
\def\stname#1{#1}
\def\phone#1{#1}
\def\fax#1{#1}
\def\ad#1{$^{\rm #1}$}
\def\aff#1{$^{#1}$}
\def\fn#1{#1}
\def\sn#1{#1}
\def\corr#1{#1}
\def\statename#1{#1}


\begin{document}

\title{Sample}

\author{Sas Santhome\ad{a} Author\ad{b} and Susanna Waxxarin\ad{c} }

\maketitle

\address{\aff{a}\dept{Department of Sciences}, \inst{University}, \cyname{City}, \country{Italy}; \aff{b}\dept{Department of Economics}, \inst{University}, \cyname{Trieste}, \country{Italy}}

\end{document}

我想要实现的是\author我有\ad{}'n'次,

链接文本\ad{a}也应该在那里\aff{a},否则我需要显示编译错误,

例如在上面的代码中,我们可以看到

\ad{a} - \aff{a}
\ad{b} - \aff{b}
\ad{c} - Missing \aff{c}

\aff{c} 缺失,但 \ad{c} 存在,因此我应该抛出编译错误

请指导

答案1

例如,您可以将\ad标签保存到宏中,并在需要时\afflist使用这些标签。这意味着您不需要再次重复标签,并且宏的使用无需参数。\afflist\aff\aff

\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\ad#1{$^{\rm #1}$\global\addto\afflist{{#1}}}
\def\aff{%
   \ifx\afflist\empty \errmessage{There is more \noexpand\aff than \string\ad}%
   \else \expandafter\affA\afflist\end $^{\rm\tmp}$%
   \fi
}
\def\affA#1#2\end{\gdef\afflist{#2}\def\tmp{#1}}
\def\address#1{#1%
  \ifx\afflist\empty\else \errmessage{There is more \noexpand\ad than \string\aff}\fi
}
\def\afflist{}    

\def\dept#1{#1}   
\def\inst#1{#1}   
\def\cyname#1{#1} 
\def\country#1{#1}
\def\pcode#1{#1} 
\def\stname#1{#1}
\def\phone#1{#1}
\def\fax#1{#1} 
\def\fn#1{#1}  
\def\sn#1{#1}  
\def\corr#1{#1}
\def\statename#1{#1}

...

\address{\aff\dept{Department of Sciences}, \inst{University}, \cyname{City}, \country{Italy}; 
         \aff\dept{Department of Economics}, \inst{University}, \cyname{Trieste}, \country{Italy}; 
         \aff\dept{Somewhere}}

如果需要\aff再次重复宏中的标签,那么您可以检查标签是否相同:

\def\aff#1{\def\tmpa{#1}%
   \ifx\afflist\empty \errmessage{There is more \noexpand\aff than \string\ad}%
   \else \expandafter\affA\afflist\end $^{\rm\tmp}$%
         \ifx\tmp\tmpa \else \errmessage{unmatched labels \tmpa--\tmp}\fi
   \fi
}

相关内容