我怎样才能先自动写“Mr. Firstname Lastname”,然后再自动写“Mr. Lastname”?

我怎样才能先自动写“Mr. Firstname Lastname”,然后再自动写“Mr. Lastname”?

我是第一个不是指的是 bibtex 或类似的东西。

我的目标是写:

 I spoke to \somecommand{Mustermann} and confirmed the data.
After that, \somecommand{Mustermann} told me this and that. \\
% or simpler:
first occurrence: \somecommand{Mustermann} \\
every other occurrence: \somecommand{Mustermann} 

这应该导致(类似于缩写):

I spoke to Mr. Max Mustermann and confirmed the data. After that, Mr. Mustermann told me this and that.  
first occurrence: Mr. Max Mustermann
every other occurrence: Mr. Mustermann

这会在第一次出现时动态添加名字。我可以使用缩写来解决这个问题,但我不希望它们出现在我的缩写列表中。

任何帮助都将不胜感激!

答案1

您可以编写一个重新定义自身的宏。

\documentclass{article}

\newcommand*{\mustermann}{\renewcommand*{\mustermann}{Mr.~Mustermann}Mr.~Max Mustermann}

\begin{document}

first occurrence: \mustermann\par
every other occurrence: \mustermann

\end{document}

在此处输入图片描述

或者,您可以在序言中定义“名称”。

\documentclass{article}

\newcommand*{\newname}[3]{% #1: prefix/title; #2: given name; #3: family name
   \expandafter\newcommand\csname name@\detokenize{#3}\endcsname{\expandafter\renewcommand\csname name@\detokenize{#3}\endcsname{#1~#3}#1~#2 #3}
}
\newcommand{\usename}[1]{%
   \ifcsname name@\detokenize{#1}\endcsname
      \csname name@\detokenize{#1}\endcsname
   \else
      ???%
   \fi
   }

\newname{Mr.}{Max}{Mustermann}
\newname{Ms.}{Erika}{Musterfrau}

\begin{document}

See \usename{Mustermann}, and then \usename{Mustermann}
as well as \usename{Musterfrau}, \usename{Mustermann},
\usename{Musterfrau}, and \usename{Musterkind}.

\end{document}

在此处输入图片描述

如果是同音异义词,则稍微不同的方法是定义“标签”

\documentclass{article}

\newcommand*{\newname}[3]{% #1=label; #2=first occurrence; #3=further occurrences
   \expandafter\newcommand\csname name@\detokenize{#1}\endcsname{\expandafter\renewcommand\csname name@\detokenize{#1}\endcsname{#3}#2}
}
\newcommand{\usename}[1]{%
   \ifcsname name@\detokenize{#1}\endcsname
      \csname name@\detokenize{#1}\endcsname
   \else
      ???%
   \fi
}
\newname{foo}{Mr.~Max Mustermann}{Mr.~Mustermann}
\newname{baz}{Ms.~Erika Musterfrau}{Ms.~Musterfrau}

\begin{document}

See \usename{foo}, and then \usename{foo}
as well as \usename{baz}, \usename{foo},
\usename{baz}, and \usename{bar}.

\end{document}

(与上面输出相同。)

相关内容