0

Since I am not experienced with writing cls so it may be a very simple question and is a duplicate but I cannot use the exact word to express it.

Here is what I want to achieve in this cls:

  • There are two commands, \author and instructor (this is defined in cls), each taking an argument.
  • Normally, they can be set separately.
  • However, if \author or \instructor is empty (i.e. \author{}, \instructor{}) or \author or \instructor is not defined by the user, they should use a fallback, \@author will be \@instructor or \@instructor be assigned \@author according to which one is missing.
  • If both are missed, maybe just do nothing.

This is a MnWE. It compiles with error. (Strangely, with my full cls, it compiles fine but the \if statement does not seem right and the result is wrong.)

\RequirePackage{filecontents}
\begin{filecontents*}{myclass.cls}
    \NeedsTeXFormat{LaTeX2e}
    \ProvidesClass{myclass}
    \LoadClass{article}
\let\@instructor\@empty
\newcommand{\instructor}[1]{\gdef\@instructor{#1}}

\newcommand{\test}{\@instructor}

\makeatletter
\if\@instructor\@empty
    \def\@instructor\@author % author is the instructor (if not specified)
\else
    \if\@author\@empty
        \def\@author\@instructor % instructor is the author (if not specified)
    \fi
\fi
\makeatother

\end{filecontents*}

\documentclass{myclass} \author{Author Name} % \instructor{Instructor Name} \instructor{}

\begin{document} \test{} \end{document}

1 Answers1

0

Thanks to David Carlisle, I finally solved the problem. The difference between \ifx and \if is pointed out here.

The test file needs some changes so as to demonstrate the function (here I use the \maketitle command as an example):

\RequirePackage{filecontents}
\begin{filecontents*}{myclass.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}
\LoadClass{article}

\let@instructor@empty \newcommand{\instructor}[1]{\gdef@instructor{#1}}

\makeatletter \renewcommand\maketitle{ \ifx@instructor@empty \let@instructor@author % author is the instructor (if not specified) \else \ifx@author@empty \let@author@instructor % instructor is the author (if not specified) \fi \fi @instructor } \makeatother \end{filecontents*}

\documentclass{myclass} \author{Author Name} % \instructor{Instructor Name} \instructor{}

\begin{document} \maketitle \end{document}

which now works out fine.