我正在更新自定义 latex 类,但我是 latex 新手,我需要将这样的命令解析\author{First Middle Last}
为新命令。我无法直接以格式\author{Last}{First Middle}
提供,因为我无法控制生成它的后端。我查看了包,但不确定如何在这里使用它。\author{First Middle Last}
\author{Last}{First Middle}
stringstrings
我也尝试过类似的事情\let\auxauthor\author \renewcommand\author[1]{\auxauthor{#2}{#1}}
,但我确实忽略了一些重要的细节。
答案1
我假设你正在使用需要指定作者的文档类
\author{Last}{First Middle}
最好了解一下这个类,这样才能对真实的例子进行实验。无论如何,以下是您可以采取的方法。
我使用该类article
,但通过重新定义命令\author
并定义一个\useauthor
进行测试来伪造你的类;该类可能会以不同的方式使用数据,但这并不重要。
\documentclass{article}
% let's emulate what the (unknown) used class does
\makeatletter
\def\author#1#2{\gdef\@authorlast{#1}\gdef\@authorfirstmiddle{#2}}
\def\useauthor{\@authorlast, \@authorfirstmiddle} % for testing
\makeatother
% your modification should go in the preamble, before any \author command is used
\makeatletter
\let\class@author\author
\renewcommand{\author}[1]{%
% split the data
\def\gp@firstmiddle{}%
\gp@divide{#1}%
}
\newcommand\gp@divide[1]{%
\gp@@divide#1 \@nil
}
\def\gp@@divide#1 #2\@nil{%
\if\relax\detokenize{#2}\relax
\def\gp@last{#1}%
\expandafter\@firstoftwo
\else
\ifx\gp@firstmiddle\@empty
\def\gp@firstmiddle{#1}%
\else
\edef\gp@firstmiddle{\unexpanded\expandafter{\gp@firstmiddle} \unexpanded{#1}}%
\fi
\expandafter\@secondoftwo
\fi
{\gp@author}%
{\gp@@divide#2\@nil}%
}
\def\gp@author{%
\expandafter\expandafter\expandafter\class@author
\expandafter\expandafter\expandafter
{\expandafter\gp@last\expandafter}\expandafter{\gp@firstmiddle}%
}
\makeatother
\begin{document}
\author{First Middle Last}
\useauthor
\author{First Last}
\useauthor
\author{Last}
\useauthor
\end{document}
更短的代码:
\documentclass{article}
% let's emulate what the (unknown) used class does
\makeatletter
\def\author#1#2{\gdef\@authorlast{#1}\gdef\@authorfirstmiddle{#2}}
\def\useauthor{\@authorlast, \@authorfirstmiddle} % for testing
\makeatother
\usepackage{xparse}
% your modification
\ExplSyntaxOn
\cs_set_eq:NN \gp_author_class:nn \author
\RenewDocumentCommand{\author}{m}
{
\seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 }
\seq_pop_right:NN \l_tmpa_seq \l_tmpa_tl % last
\use:x
{
\exp_not:N \gp_author_class:nn
{ \exp_not:V \l_tmpa_tl }
{ \seq_use:Nn \l_tmpa_seq { ~ } }
}
}
\ExplSyntaxOff
\begin{document}
\author{First Middle Last}
\useauthor
\author{First Last}
\useauthor
\author{Last}
\useauthor
\end{document}