0

I use the "variables" in my header and would like to shorten the code by combining \ifdefined\@@author and \ifdefined\@group. Searching the repository for "ifdefined multiple arguments" etc. didn't give a useful result. I experimented with ||, as from different programming languages, but you can imagine... :)

Is there no easier way to do that?

\rhead{\small\color{darkgray2}%
    \ifdefined\@@author%
        \@@author%
        \ifdefined\@group%
            , %
        \fi%
    \fi%
    \ifdefined\@group%
        \@group%
    \fi%
    \ifdefined\@@date%
        \linebreak\@@date%
    \fi%
}

Thanks.

Edit (MWE):

\LoadClass{article}
\newcommand\group[1]{\def\group{#1}}
\author{Me}
\group{Group}
\title{Work Hygiene and Safety}
\date{18th~September~2018}

\RequirePackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}% 

\makeatletter
\rhead{
    \ifdefined\@author%
        \@author%
        \ifdefined\group%
            , %
        \fi%
    \fi%
    \ifdefined\group%
        \group%
    \fi%
    \ifdefined\@date%
        \linebreak\@date%
    \fi%
}
\makeatother
\begin{document} 
Hello
\end{document}
  • I can't see what logic changes would make this shorter: could you write out in pseudo-code what flow you are after? – Joseph Wright Sep 2 '18 at 8:44
  • I just want the learn "whats's possible". I thought there must be a solution like \ifdefined\THIS&&THAT ... – Steffen Sep 2 '18 at 8:48
  • @Steffen there are no boolean and and or connectives although of course any logical combination can be made with suitable \if combinations. – David Carlisle Sep 2 '18 at 8:55
  • unrelated but you have % at the ends of exactly the wrong lines, you need one after \rhead{ but the ones after command names like \group% do nothing as white space is ignored after a command name. – David Carlisle Sep 2 '18 at 8:59
  • 1
    I would guess you intended \newcommand\group[1]{\def\group{#1}} to be \newcommand\group[1]{\def\@group{#1}} – David Carlisle Sep 2 '18 at 9:02
3

The logic of all TeX primitive conditionals is the same: they are 'one shot' tests with no combining logic. There are a variety of 'wrappers' which provide such logic, most notable in etoolbox and expl3, which use a more 'LaTeX-like' syntax. At the primitive level, one can create combinations by using \ifnum or \ifodd, for example

\ifnum0%
  \ifdefined\@@author 1\fi
  \ifdefined\@group 1\fi
  >0 %
  % Code for 'at least one is true' case
\else
  % Code for 'neither is true' case
\fi

This uses the fact that TeX keeps expanding material in the number test until it finds a non-numeral, in particular a comparator here. So the two \ifdefined tests get converted into either a value or nothing at all.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.