全局命令的变量输入

全局命令的变量输入

我正在开发一个模板,并且我有一个制作批准页面的简陋但有效的方法:

我在主文档中定义各种宏,然后从我的包中调用命令:

\documentclass[11pt]{book}

\usepackage{mypackage}

\newcommand{\advisor}{Name, Degrees}

%uncomment the following line to add a second advisor
%\newcommand{\advisortwo}{Name, Degrees}

\newcommand{\dean}{Name, Degrees}
\newcommand{\reviewerone}{Name, Degrees}
\newcommand{\reviewertwo}{Name, Degrees}
\newcommand{\reviewerthree}{Name, Degrees}
\newcommand{\reviewerfour}{}
\newcommand{\reviewerfive}{}

\begin{document}

\frontmatter

\makeapprovals

\mainmatter

\chapter{One}

Content

\end{document}

我的包裹里有以下物品:

\ProvidesPackage{mypackage}[2017/03/21 Package info]


\usepackage{tabularx}
\usepackage{float}
\usepackage[doublespacing]{setspace}

\newcommand{\makeapprovals}[1]{\gdef\@\makeapprovals{#1}%
\chapter{Approvals}
\thispagestyle{plain}

\vspace*{1in}

Approval blurb.

\begin{center}
\begin{minipage}{0.9\textwidth} 

\ifdefined\advisortwo
  \begin{tabular}{p{4in}p{1.5in}}
    \hrulefill & \hrulefill \\[-10pt]
    Dissertation Advisor - \advisor & Date \\ [10pt]
    \hrulefill & \hrulefill \\[-10pt]
    Dissertation Advisor - \advisortwo & Date \\ [10pt]
    \hrulefill & \hrulefill \\[-10pt]
    Dean - \dean & Date \\ 
\end{tabular}
\else
  \begin{tabular}{p{4in}p{1.5in}}
    \hrulefill & \hrulefill \\[-10pt]
    Dissertation Advisor - \advisor & Date \\ [10pt]
    \hrulefill & \hrulefill \\[-10pt]
    Dean - \dean & Date \\ 
\end{tabular}
\fi
\vskip 0.25in
  \begin{tabular}{p{2.5in}}
\textbf{\reviewerone}\\
\textbf{\reviewertwo}\\
\textbf{\reviewerthree}\\
\textbf{\reviewerfour}\\
\textbf{\reviewerfive}\\
\end{tabular}
\end{minipage}
\end{center}
}

\endinput

我希望能够以这种方式定义事物并使用循环:

\dean{Name, Degrees}
\advisor{Name, Degrees; Name, Degrees}
\reviewers{Name, Degrees; Name, Degrees; Name, Degrees}
\makeapprovals

我知道xparse允许这样的事情,但我搞不懂。如果有简单的方法,其他语法也可以。

答案1

我将其用于\singlespacing大部分审批页面,因为它使控制空间变得更容易。

以下方法的优点是顾问和审阅者列表也可用于其他目的:修饰名称的代码仅在构建批准页面时添加。

\documentclass[11pt]{book}

%% code to go in the package
\RequirePackage{xparse}
\RequirePackage[doublespacing]{setspace}

\ExplSyntaxOn
\NewDocumentCommand{\makeapprovals}{}
 {
  \chapter*{Approvals}
  \thispagestyle{plain}

  Approval ~ blurb.

  \begin{center}
  \singlespacing
  \begin{tabular}{@{}p{4in}p{1.5in}@{}}
  \int_step_inline:nnnn { 1 } { 1 } { \seq_count:N \g_bfh_template_advisors_seq }
   {
    \tl_put_right:Nn \l__bfh_template_temp_tl
     {
      \hrulefill & \hrulefill \\
      Dissertation ~ Advisor ~ -- ~ \seq_item:Nn \g_bfh_template_advisors_seq { ##1 } & 
      Date \\[15pt]
     }
   }
   \tl_use:N \l__bfh_template_temp_tl
   \hrulefill & \hrulefill \\
   Dean ~ -- ~ \tl_use:N \g_bfh_template_dean_tl & Date \\
   \end{tabular}
   \par
   \vspace{0.25in}
   \bfseries
   \begin{tabular}{@{}l@{}}
   \seq_use:Nn \g_bfh_template_reviewers_seq { \\ }
   \end{tabular}
   \end{center}
 }
\NewDocumentCommand{\dean}{m}
 {
  \tl_gset:Nn \g_bfh_template_dean_tl { #1 }
 }
\NewDocumentCommand{\advisors}{m}
 {
  \seq_gset_split:Nnn \g_bfh_template_advisors_seq { ; } { #1 }
 }
\NewDocumentCommand{\reviewers}{m}
 {
  \seq_gset_split:Nnn \g_bfh_template_reviewers_seq { ; } { #1 }
 }
\tl_new:N \g_bfh_template_dean_tl
\tl_new:N \l__bfh_template_temp_tl
\seq_new:N \g_bfh_template_advisors_seq
\seq_new:N \g_bfh_template_reviewers_seq
\ExplSyntaxOff
%% end of code for package

\begin{document}

\dean{Dean Name, Degrees}
\advisors{
  Advisor1 Name, Degrees;
  Advisor2 Name, Degrees;
  Advisor3 Name, Degrees;
  Advisor4 Name, Degrees
}
\reviewers{
  Reviewer1 Name, Degrees;
  Reviewer2 Name, Degrees;
  Reviewer3 Name, Degrees
}

\makeapprovals

\frontmatter

\makeapprovals

\mainmatter

\chapter{One}

Content

\end{document}

在此处输入图片描述

例如,你可以定义一个宏,\advisedby例如

\NewDocumentCommand{\advisedby}{}
 {
  Advised ~ by ~ \seq_use:Nnnn \g_bfh_template_advisors_seq { ~and~ } { ,~ } { ,~and~ }
 }

(当然是在\ExplSyntaxOn和之间\ExplSyntaxOff),它将与一个、两个、三个或更多顾问一起做正确的事情:

  • 一位顾问:“由顾问姓名提供建议”
  • 两位顾问:“由顾问 1 姓名和顾问 2 姓名提供建议”
  • 三位顾问:“由顾问 1 姓名、顾问 2 姓名和顾问 3 姓名提供建议”

当然,您可以从尾随参数中删除牛津逗号。

答案2

在此处输入图片描述

\documentclass[11pt]{book}
\addtolength\textwidth{62pt}
\makeatletter
\def\@advisor{}
\def\@reviewer{}
\def\@dean{}
\def\advisor#1{\g@addto@macro\@advisor{%
    Dissertation Advisor -- #1& Date \\[10pt]
    \hrulefill & \hrulefill \\%[-10pt] % really?
    }}
\def\reviewer#1{\g@addto@macro\@reviewer{#1\\}}
\def\dean#1{\g@addto@macro\@dean{\\#1}}

\def\makeapprovals{%
\begin{center}
  \begin{tabular}{p{4in}p{1.5in}}\@advisor\\[10pt]\@dean\end{tabular}
\bigskip
 \begin{tabular}{p{2.5in}}\@reviewer\end{tabular}
\end{center}}

\makeatletter

\advisor{Advisor1, D1}
\advisor{Advisor2, D2}

\reviewer{Reviewer1, RD1}
\reviewer{Reviewer2, RD2}
\reviewer{Reviewer3, RD3}


\dean{Name, Degrees}


\begin{document}

\frontmatter

\makeapprovals

\mainmatter

\chapter{One}

Content

\end{document}

答案3

在此处输入图片描述

\documentclass[11pt]{book}

% \applyto{..loop body..}{..list of items..}
\newcommand\applyto[2]{\def\doxloop{#1}\expandafter\xloop#2\relax}
\newcommand\xloop[1]{\ifx\relax#1\else\doxloop{#1}\expandafter\xloop\fi}

% \thedean, \theadvisors, and \thereviewers store the names provided
% repeatedly with \dean, \advisor, and \reviewer, respectively.
\newcommand\thedean{}
\newcommand\dean[1]{\def\thedean{#1}}
\newcommand\theadvisors{}
\newcommand\thereviewers{}
\makeatletter
\newcommand\advisor[1]{\g@addto@macro\theadvisors{{#1}}}
\newcommand\reviewer[1]{\g@addto@macro\thereviewers{{#1}}}
\makeatother

% \dosignature{..function..}{..name..}
\newcommand\dosignature[2]%
  {\vspace*{15pt}\par\noindent
   \makebox[3in]{\hrulefill}\quad\makebox[1in]{\hrulefill}\\[-3pt]
   \parbox[t]{3in}{#1~-- #2}\quad\makebox[1in]{Date}%
   \par
  }

% \doreviewer{..name..}
\newcommand\doreviewer[1]%
  {\vspace*{15pt}\par\noindent
   \textbf{#1}%
   \par
  }

\newcommand\makeapprovals
  {\chapter*{Approvals}
   \thispagestyle{plain}
   \vspace*{1in}
   Approval blurb.
   \begin{center}
     \begin{minipage}{0.9\textwidth}
       \applyto{\dosignature{Dissertation Advisor}}\theadvisors
       \dosignature{Dean}\thedean
       \vskip 0.25in
       \applyto\doreviewer\thereviewers
     \end{minipage}
   \end{center}
  }

\begin{document}
\dean{DeanName, DeanDegrees}
\advisor{A1Name, A1Degrees}
\advisor{A2Name, A2Degrees}
\reviewer{R1Name, R1Degrees}
\reviewer{R2Name, R2Degrees}
\reviewer{R3Name, R3Degrees}
\makeapprovals
\end{document}

答案4

我使用 提出了自己的解决方案etoolbox。与 egreg 的解决方案一样,它最终得到了可用的字符串。

\documentclass[oneside, 11pt]{book}
\usepackage[letterpaper, margin = 1in]{geometry}

\usepackage{tabularx}
\usepackage{float}
\usepackage{etoolbox}
\usepackage[doublespacing]{setspace}
\usepackage{xstring}

\newcommand\dean[1]{\def\dean{#1}}
\newcommand\advisor[1]{\def\advisor{#1}}
\newcommand\reviewers[1]{\def\reviewers{#1}}

\newcommand{\makesigline}[1]{%
\hrulefill & \hrulefill \\[-10pt]
    #1 & Date \\ [10pt]}

\DeclareListParser*\forsemicolonlist{;}
\newcommand\handler[1]{%
\makesigline{Dissertation Advisor - #1}}
\newcommand\secondhandler[1]{\textbf{#1}\\}

\newcommand{\makeapprovals}[1]{\gdef\@\makeapprovals{#1}%
\chapter*{Approvals}
\thispagestyle{plain}

\vspace*{1in}

Approval blurb.

\begin{center}
\begin{minipage}{0.9\textwidth} 

\begin{tabular}{p{4in}p{1.5in}}
    \expandafter\forsemicolonlist\expandafter\handler\expandafter{\advisor}
    \makesigline{Dean - \dean} 
\end{tabular}
\vskip 0.25in
  \begin{tabular}{p{2.5in}}
\expandafter\forsemicolonlist\expandafter\secondhandler\expandafter{\reviewers}
\end{tabular}
\end{minipage}
\end{center}
}

\advisor{Foo, Degrees; Bar, Degrees}
\dean{Name, Degrees}
\reviewers{Foobar, Degrees; Barbar, Degrees; Bazbar, Degrees}

\begin{document}

\frontmatter

\makeapprovals

\mainmatter

\chapter{One}

Advised by \StrSubstitute{\advisor}{;}{ and}


\end{document}

第一页有批准

列表仍可访问

相关内容