[Tex/LaTex] How to create conditionals in a document class for latex

conditionalspackages

I am trying to write a document class highschool.cls for the high school research papers in our school based on the book.cls in order to make the format of the papers standardized. I am trying to define a signature page with heading “CERTIFICATION'' such that the following text appears on the page.

enter image description here

To achieve this, I made the following definitions on LaTeX class 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

In my source TeX file, I would type something like

\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}

What I want to achieve is that

  • when there are no \surnameb,\firstnameb,\midinitialb,\surnamec,\firstnamec, and \midinitialc declared, I get the following text:

enter image description here

  • when there are no \surnamec,\firstnamec, and \midinitialc declared, I get the following text:

enter image description here

  • and when all are declared, I get the text:
    enter image description here

Is it possible to do this? If it is, how do I do it?

Best Answer

The following solution is based on these assumptions:

  1. You can only have

    • Author 1; or
    • Author 1, and Author 2; or
    • Author 1, Author 2, and Author 3

    and not, for example

    • Author 1, and Author 3; or
    • Author 2, and Author 3; or
    • Author 2 only; or
    • Author 3 only.
  2. It is sufficient to supply only a first name to distinguish whether an author "exists" or not.

The second condition can be modified, but it seemed logical that one would have a first name and a surname and possibly a middle initial, but never not a first name. Consequently, you can then use the following definitions:

\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

I've added the conditional definitions inside each of the respective author components (this is not really necessary, since only \if@firstnameX is used) and placed the conditions throughout \signaturep to include/exclude certain words/phrases/punctuation based on the truth of the conditions.

The input

\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}

yields

enter image description here


The following minimal example adds an option to specify the list of authors as a single macro \listauthors{{<first>},{<second>},{<third>},...} using a CSV list.

enter image description here

\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}

The definition of \authorseq determines how the titles are printed. \authorseq defaults to \authorfml which prints the author list as <first name>\ <middle name>\unskip\ <last name> (\unskip takes care of missing/non-existent <middle name> in removing the preceding control space \). Switching around the author display (for use in your abstract and introduction, if needed) is done by a redefinition of \authorseq, while formatting is controlled within the macros used (\authorfml or \authorlfm or whatever).

Related Question