允许用户使用一个命令指定多个值

允许用户使用一个命令指定多个值

我正在尝试定义一个宏,可用于指定论文/学位论文的委员会成员姓名。我想允许用户多次指定此命令以添加多个委员会成员,就像在联结类文件。

查看 mitthesis 类文件时,我想到使用一个框来组合对宏的调用。他们这样做的方式是:

% since there can be more than one supervisor,
% we build the appropriate boxes for the titlepage and
% the abstractpage as the user makes multiple calls
% to \supervisor
\newbox\@titlesupervisor    \newbox\@abstractsupervisor

\def\supervisor#1#2{\setbox\@titlesupervisor\vbox
  {\unvbox\@titlesupervisor \vskip 10pt% plus 1fil minus 1fil
  \def\baselinestretch{1}\large
  \signature{Certified by}{#1 \\ #2 \\ Thesis Supervisor}}
  \setbox\@abstractsupervisor\vbox{\unvbox\@abstractsupervisor
  \vskip\baselineskip \def\baselinestretch{1}\@normalsize 
  \par\noindent Thesis Supervisor: #1 \\ Title: #2}}

据我所知,我们可以用保存框做类似的事情,所以我尝试了这样的事情:

% We need to allow for multiple calls to committee
% Therefore start building the committee section of the title page
% Each call to committee should first specify the name as arg 1, and  
% then the degree as arg 2
% for instance \committee{John Doe}{Ph.D.}
\newsavebox\@committeebox
\def\committee#1#2{%
    \sbox{\@committeebox}{%
        \usebox{\@committeebox}\\%
        \bfseries
        #1, #2%
    }%
}

但由于某种原因,这只显示添加的第一位委员会成员。

我通过添加一个来测试这个想法

\sbox{\@committeebox}{test}

\def上面的例子中(之后\newsavebox),通过在第一个指定的委员会成员之前向输出中添加“测试”来稍微起作用,尽管它并没有像我想象的那样将内容移动到新行(我明白testJane Doe, Ph.D.这一点,但它仍然没有显示其他指定的委员会成员。

有人能看出这里出了什么问题吗?或者告诉我为什么这行不通?显然,我可以使用 mitthesis 的功能,但我问这个问题是为了更好地了解事情的进展情况。

作为 mwe,你可以

\documentclass{sample}

\committee{Jane Doe}{MS}
\committee{John Doe}{Ph.D.}

\begin{document}

  \maketitle

 \end{document}

作为你的 tex 文件和

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{sample}[2016/01/29 Sample]

\DeclareOption*{%
    \ClassWarning{sample}{No options permitted}%
}

\ProcessOptions\relax
\LoadClass{report}

\def\maketitle{%
    \begin{center}
        \usebox{\@committeebox}
    \end{center}
}

作为您的 sample.cls 文件。

答案1

我不明白为什么要在盒子里构建它,因为没有必要。此外,当我使用您的示例时,我获得了所有委员会成员的姓名,只是他们彼此相邻。这是因为您不是拳击内容的\vbox方式mitthesis做。

我建议不要对内容进行装箱,而是将它们原封不动地累积在宏中,并在稍后使用必要的定义进行设置。以下是这样的实现:

在此处输入图片描述

\documentclass{article}

% We need to allow for multiple calls to committee
% Therefore start building the committee section of the title page
% Each call to committee should first specify the name as arg 1, and  
% then the degree as arg 2
% for instance \committee{John Doe}{Ph.D.}
\newcommand\committeemembers{}% This willl store committee members
\makeatletter
\newcommand\committee[2]{% Add another committee member to the list
  \g@addto@macro\committeemembers{\nextcommitteemember #1, #2}%
}
\makeatother

\newcommand{\nextcommitteemember}{}% Just a placeholder
\newcommand{\printcommittee}[1][c]{{% Print the list of committee members
  % http://tex.stackexchange.com/a/89187/5764
  \renewcommand{\nextcommitteemember}{\gdef\nextcommitteemember{\tabularnewline}}%
  \bfseries% Committee member formatting
  \begin{tabular}{#1}
    \committeemembers
  \end{tabular}
}}
\committee{Jane Doe}{MSc}
\committee{John Doe}{PhD}
\committee{Who Cares}{BSc}

\begin{document}

\begin{center}
  \printcommittee[l]
  \qquad
  \printcommittee
  \qquad
  \printcommittee[r]
\end{center}

\end{document}

相关内容