# How to test if some command similar to \author is used in .tex file?

I want to modify \maketitle command in a new document class based in memoir. \maketitle command should include a new \director command similar to author. If this command is defined in text file, the word Director (or Directors) followed by director's name is included in title but if not defined, nothing should appear.

Next code is a combination of the answers in How to count the number of authors of a document? and How to make a new command similar to \author. With second answer project directors name is added to title with \maketitle command and with the first one, the word Director or Directors is selected according command \and appering in \director command.

The problem is how to test if command \director is not used in order to avoid 'Director'. I know how to add a \newif\director but not where to set and use.

\documentclass{memoir}% http://ctan.org/pkg/memoir
\makeatletter
\newcommand{\director}[1]{\gdef\@director{#1}}%
\newcommand{\@director}{\@latex@warning@no@line{No \noexpand\director given}}
\addtoiargdef{\director}{%
\begingroup\let\footnote\@gobble}{%
\begingroup
\renewcommand{\thanks}[1]{}
\renewcommand{\and}{\unskip, }
\renewcommand{\andnext}{\unskip, }
\renewcommand{\thanksmark}[1]{}
\renewcommand{\thanksgap}[1]{}
\protected@xdef\thedirector{#1}
\endgroup\endgroup}
\newcommand{\predirector}[1]{\def\@bspredirector{#1}}
\newcommand{\postdirector}[1]{\def\@bspostdirector{#1}}
\renewcommand{\maketitlehookc}{%
{\@bspredirector \@director \@bspostdirector}%
}
\predirector{\begin{center}
\large \lineskip .5em%
\printdirector\par
\begin{tabular}[t]{c}}
\postdirector{\end{tabular}\par\end{center}}

\newcommand\testdirector{%
\expandafter\tst@dir\@director\and\nil%
}
\def\tst@dir#1\and#2\nil{%
\edef\test{#2}%
\ifx\test\@empty Director\else Directors\fi%
}
\newcommand\printdirector{%
{%
\protected\def\and{and }%
\large \testdirector\par}
}
\makeatother

\title{A Title}
\author{An Author \and Another Author}
%\director{My Director \and My other Director}
%\director{My Director}
\date{\today}
\begin{document}

\maketitle

\end{document}

-
add comment

## 2 Answers

Since \director seems to be optional, say

\let\@director\@empty


instead of

\newcommand{\@director}{\@latex@warning@no@line{No \noexpand\director given}}


Then try

\renewcommand{\maketitlehookc}{%
\ifx\@director\@empty
\else{\@bspredirector \@director \@bspostdirector}%
\fi}


So, if a \director command has not been issued, the test will be true and the \else part will be skipped.

-
Thank you, it worked! But could you explain me why you solution with \let and \ifx...\@empty works while David Carlisle's one with \newcommand doesn't? I imagin there is some difference between \@empty and 'nothing' (see David's answer) but I don't know it. –  Ignasi Jun 29 '12 at 12:44
add comment

If \director is optional then you don't want

 \newcommand{\@director}{\@latex@warning@no@line{No \noexpand\director given}}


you just want

 \newcommand{\@director}{}


Then (more or less) you don't need to test anything, just use \@director and it will be the author supplied name or nothing. You may need to make it a bit more complicated depending what you want to do pre and post director in the default (empty) case. I wasn't clear on that. But you could for example make all three internal commands empty and make director define the pre and post commands to use some spacing. So again, no testing is needed.

-
Thank you for your help. But I didn't worked as expected. I need to test that \@director is 'nothing' in order to avoid executing \predirector which pints 'Director (or Directors)'. How can I test this 'nothing'? –  Ignasi Jun 29 '12 at 12:49
Tou could use \ifx\@empty as in @egreg's answer but as I say you don't need to test. You can define \@predirector to be empty {} and define \director to define \@directpr to the supplied name and \@predirector to "director or directors: " then \@predirector\@director does the right thing in both cases –  David Carlisle Jun 29 '12 at 13:00
add comment