帮助在宏中制定条件

帮助在宏中制定条件

我有一段代码,每次\AddAuthor调用时都会以粗体列出作者,并以斜体列出他们的所属机构,并将所有作者发送到目录。

\newcommand{\AddAuthor}[2]{} %container

\newcommand{\ListAuthor}[1]{
    \renewcommand{\AddAuthor}[2]{##1}
    \addtocontents{toc}{\quad#1\par}

    \renewcommand{\AddAuthor}[2]{
        \textbf{##1} \\
        \textit{##2} \\
    }
}

但是,我希望目录中的作者格式正确,除最后一个作者外,其他作者之间用逗号分隔。如您所见,此代码仅发送以空格分隔的作者。我想可以通过使用计数器并设置条件来实现这一点,但到目前为止我还没有成功。

这是我迄今为止所做的 MWE。

\documentclass[oneside]{book}

\newcommand{\AddAuthor}[2]{} %container

\newcommand{\ListAuthor}[1]{
    \renewcommand{\AddAuthor}[2]{##1}
    \addtocontents{toc}{\quad#1\par}

    \renewcommand{\AddAuthor}[2]{
        \textbf{##1} \\
        \textit{##2} \\
    }
    \vspace{-5ex}
    #1
    \vspace{5ex} 
}     


\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\chapter{The Awesome Chapter}
\ListAuthor{
    \AddAuthor{First Author}{Goodest Boy}
    \AddAuthor{Second Author}{Not the Best One}
}

\chapter{The Awesome Chapter, part 2}
\ListAuthor{
    \AddAuthor{First Author}{Another Good Boy}
    \AddAuthor{Second Author}{The Best of Its Kind}
    \AddAuthor{Third Author}{The Baddie}
}

\end{document}

答案1

我不明白为什么你不想用逗号分隔最后一位作者。应该如何分隔???这是一个解决方案,所有作者之间都用逗号分隔:

\documentclass[oneside]{book}

\newcommand{\AddAuthor}[2]{%
  \textbf{#1} \\
  \textit{#2} \\
}

\newcommand{\ListAuthor}[1]{% <-- to avoid potentially unwanted spaces
  \begingroup
    \def\AddAuthor{\string\AddAuthor}% local redefinition to write \AddAuthor commands to the toc
    \addtocontents{toc}{\begin{l@listauthors}#1\end{l@listauthors}}% write l@listauthor environment with all the \AddAuthor commands to the toc
  \endgroup
  \vspace{-5ex}
  \begin{flushleft}
    #1
  \end{flushleft}
  \vspace{5ex} 
}
\newif\iffirstauthor% new boolean to detect the first \AddAuthor inside the toc
\newenvironment{l@listauthors}{% environment to be used inside the toc for encapsulating the \AddAuthor commands.
  \quad
  \firstauthortrue
  \renewcommand{\AddAuthor}[2]{% local redefinition of \AddAuthor inside l@listauthors environment (used inside the toc)
    \iffirstauthor\firstauthorfalse\else, \fi% if it is the first author set boolean to false otherwise add a comma
    ##1% output author name
  }%
}{\par}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\chapter{The Awesome Chapter}
\ListAuthor{%
  \AddAuthor{First Author}{Goodest Boy}%
  \AddAuthor{Second Author}{Not the Best One}%
}

\chapter{The Awesome Chapter, part 2}
\ListAuthor{%
    \AddAuthor{First Author}{Another Good Boy}%
    \AddAuthor{Second Author}{The Best of Its Kind}%
    \AddAuthor{Third Author}{The Baddie}%
}

\end{document}

这是, and最后一个。

\documentclass[oneside]{book}

\newcommand{\AddAuthor}[2]{%
  \textbf{#1} \\
  \textit{#2} \\
}

\newcommand{\ListAuthor}[1]{% <-- to avoid potentially unwanted spaces
  \begingroup
    \def\AddAuthor{\string\AddAuthor}% same as above
    \addtocontents{toc}{\begin{l@listauthors}#1\end{l@listauthors}% same as above
    }%
  \endgroup
  \vspace{-5ex}
  \begin{flushleft}
    #1
  \end{flushleft}
  \vspace{5ex} 
}
\newcounter{authorcnt}% used for the number of authors
\newcounter{lastauthor}% number of last author in the end code of l@listauthors
\newcommand*{\authorlist}{}% used for the local author list of a l@listauthors environment
\newenvironment{l@listauthors}{%
  \quad
  \setcounter{authorcnt}{0}% init the number to none
  \def\authorlist{}% empty the local list (should not be needed)
  \renewcommand{\AddAuthor}[2]{%
    \stepcounter{authorcnt}% we have one more author
    \expandafter\def\expandafter\authorlist\expandafter{% define the author list
      \authorlist% by the current list
      \stepcounter{authorcnt}% and if the current number
      \ifnum\value{authorcnt}>1 , % is > 1 by a comma
        \ifnum \value{authorcnt}=\value{lastauthor} and \fi% and if the current number is the last by "and" + space
      \fi
      ##1% and always by the author name
    }%
  }
}{%
  \setcounter{lastauthor}{\value{authorcnt}}%
  \setcounter{authorcnt}{0}% start the output again with author number 0
  \authorlist
  \par
}


\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\chapter{The Awesome Chapter}
\ListAuthor{%
  \AddAuthor{First Author}{Goodest Boy}%
  \AddAuthor{Second Author}{Not the Best One}%
}

\chapter{The Awesome Chapter, part 2}
\ListAuthor{%
    \AddAuthor{First Author}{Another Good Boy}%
    \AddAuthor{Second Author}{The Best of Its Kind}%
    \AddAuthor{Third Author}{The Baddie}%
}

\end{document}

最后但并非最不重要的一个解决方案是,仅对两个作者and使用而不使用逗号:

\documentclass[oneside]{book}

\newcommand{\AddAuthor}[2]{%
  \textbf{#1} \\
  \textit{#2} \\
}

\newcommand{\ListAuthor}[1]{% <-- to avoid potentially unwanted spaces
  \begingroup
    \def\AddAuthor{\string\AddAuthor}%
    \addtocontents{toc}{\begin{l@listauthors}#1\end{l@listauthors}}
  \endgroup
  \vspace{-5ex}
  \begin{flushleft}
    #1
  \end{flushleft}
  \vspace{5ex} 
}
% This is almost the same as above, only the conditionals are changed
\newcounter{authorcnt}
\newcounter{lastauthor}
\newcommand*{\authorlist}{}
\newenvironment{l@listauthors}{%
  \quad
  \setcounter{authorcnt}{0}%
  \def\authorlist{}%
  \renewcommand{\AddAuthor}[2]{%
    \stepcounter{authorcnt}%
    \expandafter\def\expandafter\authorlist\expandafter{%
      \authorlist
      \stepcounter{authorcnt}%  
      \ifnum\value{authorcnt}>1 % for more than one authors add a separator
        \ifnum \value{authorcnt}=\value{lastauthor}% before the last author
          \ifnum \value{authorcnt}>2
            , and % and this, if they are more than 2
          \else
            \ and % and this, if less than two
          \fi
        \else , \fi% before all other authors only a comma
      \fi
      ##1%
    }%
  }
}{%
  \setcounter{lastauthor}{\value{authorcnt}}%
  \setcounter{authorcnt}{0}%
  \authorlist
  \par
}


\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\chapter{The Awesome Chapter}
\ListAuthor{%
  \AddAuthor{First Author}{Goodest Boy}%
  \AddAuthor{Second Author}{Not the Best One}%
}

\chapter{The Awesome Chapter, part 2}
\ListAuthor{%
    \AddAuthor{First Author}{Another Good Boy}%
    \AddAuthor{Second Author}{The Best of Its Kind}%
    \AddAuthor{Third Author}{The Baddie}%
}

\end{document}

我更喜欢这个:

在此处输入图片描述

如果您想要其他结果,可以使用附加条件(或删除一些条件)轻松扩展最后的解决方案。

相关内容