3

I have to check if the \author{} macro is empty because the output should change if this condition is set.

There are two ways the author can be empty:

  • \author{}
  • No \author{} command in the document.

I tried with the \ifdefempty{\macro}{<true>}{<false>} command of etoolbox, which works fine for self defined macros like \def\testmacro{foo}, but for \author it always results on false.

How could I test if an author is set and then modify the output based on the result of this check? (The check should work as the \ifdefempty command)

Sam
  • 2,958
  • 3
    The \author{Me} command essentially does \def\@author{Me}, so you need to test \@author for emptiness. Beware that some classes initialize \@author to be something else than empty. – egreg Sep 13 '17 at 15:12
  • Yes, that's right, but if I set \author{}, nothing is printed, even though \@author is probably not empty (as it also evaluates as false). I want to check if there is a content that is also printed on the page. – Sam Sep 13 '17 at 15:16
  • Indeed, in the standard classes \@author is not initialized to empty. – egreg Sep 13 '17 at 15:18
  • put \show\@author then tex will stop (as for an error) and show you what the initial definition is with the class you are using – David Carlisle Sep 13 '17 at 15:19
  • 1
    @egreg No I didn't use the \makeat* commands. I now tried with them and it works, so could you please tell me what those do? I never got it. They seem to be pretty important. – Sam Sep 13 '17 at 15:23
  • 1
    They just allow you to use the @ character in the document so you can access the \@author command (the idea being that this is a special command that would normally only be used in a class or package file). – musarithmia Sep 13 '17 at 15:24
  • 1

1 Answers1

8

If you redefine the user command \author and the internal command \@author, you can make them do what you want them to. You can set them up in a way that you can test. A simple example is below, explained in the comments.

\documentclass{article}
\makeatletter % make it safe to use \@author

% Make \author do what you want it to: set a value for \@author
\renewcommand{\author}[1]{\def\@author{#1}}

% Create a test for whether author is present (non-empty)
\newif\ifauthor
\authorfalse % Set default value of conditional to false
\def\@author{} % Set default value of \@author to empty

% Create a command to check if \@author is empty and set the
% \ifauthor conditional accordingly, for use in other commands
\newcommand{\authorcheck}{%
    \ifx\@author\empty
    \authorfalse
    \else
    \authortrue
    \fi
}

% A sample command that would use this value to print the author, 
% if non-empty, or "Anonymous" instead
\newcommand{\printauthor}{%
    \authorcheck
    AUTHOR: \ifauthor\@author\else Anonymous\fi\par
}
\makeatother % We're done using @ now

% \author{Me} % Uncomment to test
\title{This}

\begin{document}
\printauthor
\end{document}
musarithmia
  • 12,463
  • This is a nice answer. For anyone using their own .cls file, everything above % \author{Me} can be put in the .cls file. Make sure to still call the \printauthor somewhere in the .tex file to cause \authorcheck to run, otherwise \ifauthor will remain false. – cwa Feb 19 '24 at 23:20