作者组分隔符(IF 条件)

作者组分隔符(IF 条件)

我是 LaTeX 的新手,只是个初学者,基本上是一个 Perl 开发人员。

我已经创建了一个 LaTeX 文件

\begin{document}

\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ititles{Some title Text}\ipage{995}}

\end{document}

现在我需要定义 LaTeX 命令,以便输出如下所示

Name1 KCG、Name11 YQ 和 Name111 C.-Z。一些标题文本 - 995。

\iname- 人名将重复多次
\isurname- 人的姓氏
\ifname- 人的名字

\iname将重复任意次数,例如 1 到...

如果\iname里面只找到一个\cont,则显示应该如下所示

名称1 KCG 一些标题文本 - 995。

如果\iname里面只找到两个\cont,则显示应该如下所示

Name1 KCG 和 Name11 YQ 一些标题文本 - 995。

如果在 \cont 内发现两个以上\iname,则显示应如下所示(并且应位于最后一个之后\iname

Name1 KCG、Name11 YQ 和 Name111 C.-Z。一些标题文本 - 995。

我可以定义用\iname逗号分隔的输入,但我不知道如何编写宏来\iname根据 if 条件定义不同的输出。

我希望已经清楚地解释了我的疑问。

答案1

您的输入是非常具体,这个答案也是如此。它假设内部有一个\isurname-结构:\ifname\iname

在此处输入图片描述

\documentclass{article}
\newcounter{namecntr}\newcounter{curname}
\providecommand{\iname}[1]{}
\providecommand{\isurname}[1]{}
\providecommand{\ifname}[1]{}
\providecommand{\ititles}[1]{}
\providecommand{\ipage}[1]{}
\newcommand{\cont}[1]{{%
  \setcounter{namecntr}{0}\setcounter{curname}{0}% Restart counters
  \renewcommand*{\iname}{\stepcounter{namecntr}}% Set namecntr
  #1% Count number of names
  \renewcommand*{\iname}{\stepcounter{curname}% Set names
    \ifnum\value{curname}>1
      \ifnum\value{curname}<\value{namecntr}
        \unskip, 
      \else
        \unskip, and
      \fi\fi}%
  \renewcommand*{\isurname}[1]{##1}% Print surname
  \renewcommand*{\ifname}[1]{~##1}% Print first name
  \renewcommand*{\ititles}[1]{\unskip\space##1}% Print title
  \renewcommand*{\ipage}[1]{~-~##1}% Print page(s)
  #1% Process "list"
}}
\begin{document}

% One author
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\ititles{Some title Text}\ipage{995}}

% No authors
\cont{\ititles{Some title Text}\ipage{995}}

% Two authors
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\ititles{Some title Text}\ipage{995}}

% Three authors
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ititles{Some title Text}\ipage{995}}

% Three authors with no page
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ititles{Some title Text}}

% Four authors
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ititles{Some title Text}\ipage{995}}

% Four authors with no title
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ipage{995}}

\end{document}

答案2

请检查以下 MWE 是否适合您的要求:

\documentclass{article}

\begin{document}
\pagestyle{empty}

\makeatletter
\newcounter{name}
\def\auCon{\unskip,\space}
\def\auAndCon{\unskip\space and\space}
\def\auStop{\space}
\long\def\cont#1{\setcounter{name}{0}%
  \expandafter\let\csname iname@ititles\endcsname\@undefined
  \expandafter\let\csname iname@ipage\endcsname\@undefined
  \def\isurname##1{\expandafter\gdef\csname iname@isurname@\thename\endcsname{##1}}%
  \def\ifname##1{\expandafter\gdef\csname iname@ifname@\thename\endcsname{##1}}%
  \def\iname##1{\stepcounter{name}##1}%
  \def\ititles##1{\expandafter\gdef\csname iname@ititles\endcsname{##1}}%
  \def\ipage##1{\expandafter\gdef\csname iname@ipage\endcsname{##1}}%
  #1%
  \@tempcnta=0%
  \@tempcntb=\thename%
  \advance\@tempcntb by -1%
  \loop\ifnum\@tempcnta<\thename%
    \advance\@tempcnta by 1%
    \xdef\myname{\the\@tempcnta}%
    \csname iname@isurname@\myname\endcsname\space%
    \csname iname@ifname@\myname\endcsname%
    \ifnum\@tempcnta<\@tempcntb%
      \auCon\else\ifnum\@tempcnta=\@tempcntb%
      \auAndCon\else\auStop\fi\fi%
  \repeat%
  \expandafter\ifx\csname iname@ititles\endcsname\relax\else%
  \textit{\csname iname@ititles\endcsname}\space\fi%
  \expandafter\ifx\csname iname@ipage\endcsname\relax\else%
  \textbf{\csname iname@ipage\endcsname}\fi\par
}
\makeatother

% One author
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\ititles{Some title Text}\ipage{995}}

% No authors
\cont{\ititles{Some title Text}\ipage{995}}

% Two authors
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\ititles{Some title Text}\ipage{995}}

% Three authors
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ititles{Some title Text}\ipage{995}}

% Three authors with no page
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ititles{Some title Text}}

% Four authors
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ititles{Some title Text}\ipage{995}}

% Four authors with no title
\cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. Q.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\iname{\isurname{Name111}\ifname{C.-Z.}}\ipage{995}}

\end{document}

相关内容