如何在 Latex 文檔類中建立條件?

如何在 Latex 文檔類中建立條件?

我正在嘗試highschool.cls根據我們學校的高中研究論文編寫一個文件類book.cls,以便使論文的格式標準化。我試圖定義一個標題為「CERTIFICATION」的簽名頁,以便以下文字出現在頁面上。

在此輸入影像描述

為了實現這一點,我對 LaTeX 類別做了以下定義highschool.cls

\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesClass{highschool}
                      [2012/08/22 v0.1e
         Standard LaTeX document class]

\newcommand\@ptsize{}
\newif\if@surnamea  \@surnameafalse
\newif\if@surnameb  \@surnamebfalse
\newif\if@surnamec  \@surnamecfalse
\newif\if@firstnamea    \@firstnameafalse
\newif\if@firstnameb    \@firstnamebfalse
\newif\if@firstnamec    \@firstnamecfalse
\newif\if@midinitiala    \@midinitialafalse
\newif\if@midinitialb    \@midinitialbfalse
\newif\if@midinitialc    \@midinitialcfalse

\setlength\paperheight {11in}%
\setlength\paperwidth  {8.5in}%
\setlength\textwidth   {6in}%

\def\defenseDate#1{\gdef\@defensedate{#1}}
\def\surnamea#1{\gdef\@surnamea{#1}}
\def\firstnamea#1{\gdef\@firstnamea{#1}}
\def\midinitiala#1{\gdef\@midinitiala{#1}}
\def\surnameb#1{\gdef\@surnameb{#1}}
\def\firstnameb#1{\gdef\@firstnameb{#1}}
\def\midinitialb#1{\gdef\@midinitialb{#1}}
\def\surnamec#1{\gdef\@surnamec{#1}}
\def\firstnamec#1{\gdef\@firstnamec{#1}}
\def\midinitialc#1{\gdef\@midinitialc{#1}}

\if@compatibility
  \renewcommand\@ptsize{0}
\else \DeclareOption{10pt}{\renewcommand\@ptsize{0}} \fi
\DeclareOption{11pt}{\renewcommand\@ptsize{1}}
\DeclareOption{12pt}{\renewcommand\@ptsize{2}}
\ExecuteOptions{12pt,oneside,final,openright} 
\ProcessOptions
\input{bk1\@ptsize.clo}

\def\signaturep{ %% signature page..
   \topmargin 0in \headsep 0in
   \begin{center}
   \textbf{CERTIFICATION}\\[6pt]
   \end{center}
   This is to certify that this research paper entitled,
   \textbf{``{\uppercase\expandafter{\@title}}''} and submitted by
  \textbf{\uppercase\expandafter{\@firstnamea} \uppercase\expandafter{\@midinitiala}. \uppercase\expandafter{\@surnamea}}, \textbf{\uppercase\expandafter{\@firstnameb} \uppercase\expandafter{\@midinitialb}. \uppercase\expandafter{\@surnameb}}, and \textbf{\uppercase\expandafter{\@firstnamec} \uppercase\expandafter{\@midinitialc}. \uppercase\expandafter{\@surnamec}} 
   to fulfill part of the requirements for
   the course Science and Technology Research II was successfully
   defended and approved on \expandafter{\@defensedate}.
}%% signature page

\def\prelim{%%prelim page
\typeout{Making SMCT preliminary headings...}%
   \pagestyle{plain}
   \pagenumbering{roman}
   \signaturep \newpage
   \baselineskip=20pt
} %%prelim

\endinput

在我的來源 TeX 檔案中,我會輸入類似的內容

\documentclass{highschool}

\defenseDate{18 August 2012}
\title{This is the Title of the Paper}
\surnamea{Author}
\firstnamea{First}
\midinitiala{N}
\surnameb{Author}
\firstnameb{Second}
\midinitialb{N}
\surnamec{Author}
\firstnamec{Third}
\midinitialc{N}

\begin{document}
\prelim %% Prints Preliminary chapters

\end{document}

我想要實現的是

  • 當沒有\surnameb,\firstnameb,\midinitialb,\surnamec,\firstnamec,\midinitialc聲明時,我得到以下文本:

在此輸入影像描述

  • 當沒有\surnamec,\firstnamec,\midinitialc聲明時,我得到以下文本:

在此輸入影像描述

  • 當所有內容都被聲明後,我得到文本: 在此輸入影像描述

是否有可能做到這一點?如果是,我該怎麼做?

答案1

以下解決方案基於這些假設:

  1. 你只能有

    • 作者 1;或者
    • 作者 1, 和作者2;或者
    • 作者 1,作者2, 和作者3

    而不是,例如

    • 作者 1, 和作者3;或者
    • 作者2, 和作者3;或者
    • 作者2僅有的;或者
    • 作者3僅有的。
  2. 僅提供名字就足以區分作者是否「存在」。

第二個條件可以修改,但一個人有名字似乎是合乎邏輯的一個姓氏和可能中間名首字母,但從來沒有不是一個名字。因此,您可以使用以下定義:

\def\defenseDate#1{\gdef\@defensedate{#1}}
\def\surnamea#1{\gdef\@surnamea{#1}\@surnameatrue}
\def\firstnamea#1{\gdef\@firstnamea{#1}\@firstnameatrue}
\def\midinitiala#1{\gdef\@midinitiala{#1}\@midinitialatrue}
\def\surnameb#1{\gdef\@surnameb{#1}\@surnamebtrue}
\def\firstnameb#1{\gdef\@firstnameb{#1}\@firstnamebtrue}
\def\midinitialb#1{\gdef\@midinitialb{#1}\@midinitialbtrue}
\def\surnamec#1{\gdef\@surnamec{#1}\@surnamectrue}
\def\firstnamec#1{\gdef\@firstnamec{#1}\@firstnamectrue}
\def\midinitialc#1{\gdef\@midinitialc{#1}\@midinitialctrue}

\def\signaturep{ %% signature page..
  \topmargin 0in \headsep 0in
  \begin{center}
  \textbf{CERTIFICATION}\\[6pt]
  \end{center}
  This is to certify that this research paper entitled,
  \textbf{``{\MakeUppercase{\@title}}''} and submitted by
  \textbf{\MakeUppercase{\@firstnamea} \MakeUppercase{\@midinitiala}.\ \MakeUppercase{\@surnamea}}%
  \if@firstnameb, \if@firstnamec\else and \fi
  \textbf{\MakeUppercase{\@firstnameb} \MakeUppercase{\@midinitialb}.\ \MakeUppercase{\@surnameb}}\if@firstnamec, \else\space\fi
  \if@firstnamec and \textbf{\MakeUppercase{\@firstnamec} \MakeUppercase{\@midinitialc}.\ \MakeUppercase{\@surnamec}} \fi\else\space\fi
  to fulfill part of the requirements for
  the course Science and Technology Research II was successfully
  defended and approved on \expandafter{\@defensedate}.
}%% signature page

我在每個相應的作者組件中添加了條件定義(這並不是真正必要的,因為僅使用了),並根據條件的真實性\if@firstnameX將條件放置在各處以包含/排除某些單字/短語/標點符號。\signaturep

輸入

\documentclass{highschool}

\defenseDate{18 August 2012}
\title{This is the Title of the Paper}
\surnamea{Author}
\firstnamea{First}
\midinitiala{N}
\surnameb{Author}
\firstnameb{Second}
\midinitialb{N}
%\surnamec{Author}
%\firstnamec{Third}
%\midinitialc{N}

\begin{document}
\prelim %% Prints Preliminary chapters

\end{document}

產量

在此輸入影像描述


\listauthors{{<first>},{<second>},{<third>},...}以下最小範例新增了一個選項,用於使用 CSV 清單將作者清單指定為單一巨集。

在此輸入影像描述

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\def\@firstmiddlelast#1,#2,#3\@nil{%
  \gdef\@firstname{#1}% Extract first name
  \gdef\@middlename{#2}% Extract middle name
  \gdef\@lastname{#3}% Extract last name
}
\newcommand{\authorfml}{{\bfseries\MakeUppercase{\@firstname\ \@middlename\unskip\ \@lastname}}}% First Middle Last
\newcommand{\authorlfm}{\@lastname,\ \@firstname\ \@middlename\unskip}% Last, First Middle
\newcommand{\authorseq}{\authorfml}% Default author order
\newcounter{@list@item}\newcounter{@total@item}
\newcommand{\listauthors}[1]{%
  \setcounter{@total@item}{0}% Reset total count
  \renewcommand*{\do}[1]{\stepcounter{@total@item}}%
  \docsvlist{#1}% Count number of items in list
  \setcounter{@list@item}{1}% Start at first item
  \renewcommand*{\do}[1]{%
    \stepcounter{@list@item}% Move to next item
    \@firstmiddlelast##1\@nil% Extract first/middle/last names
    \authorseq% Print author names
    \ifnum\value{@list@item}=\value{@total@item}\relax
      , and % Second-to-last name
    \else
      \ifnum\value{@list@item}<\value{@total@item}\relax
        , % First set of names
      \fi
    \fi
  }%
  \docsvlist{#1}% Print list
}
\makeatother
\begin{document}
See \listauthors{{First1,Middle1,Last1},{First2,,Last2},{First3,Middle3,Last3},
  {First4,Middle4,Last4},{First5,,Last5},{First6,Middle6,Last6},
  {First7,Middle7,Last7},{First8,Middle8,Last8},{First9,,Last9}} from first to last. \par
\renewcommand{\authorseq}{\authorlfm}
See \listauthors{{First1,Middle1,Last1},{First2,,Last2},{First3,Middle3,Last3}} from first to last. \par
\end{document}

的定義\authorseq決定了標題的列印方式。\authorseq預設\authorfml將作者清單列印為<first name>\ <middle name>\unskip\ <last name>(在刪除前面的控制空間時\unskip處理遺失/不存在的情況)。切換作者顯示(如果需要,可在摘要和簡介中使用)是透過重新定義 來完成的,而格式設定是在所使用的巨集(或其他巨集)內控制的。<middle name>\\authorseq\authorfml\authorlfm

相關內容