# If-if-else-expression using a variable

I am trying to create a document class containing the layout for a bachelors thesis and want to keep it as general as possible. In my title page (which is defined in the document class, not the main document) a line like this shall appear:

Handed in by {\@gender} {\@author} on {\@handindate}.


The variables were created using these commands:

\newcommand{\@handindate}{\@handindate}

\newcommand{\@gender}{Unknown}


I want LaTeX to replace \@gender by "Mr." if \@gender=male or by "Mrs." if \@gender=female and by nothing if \@gender is anything else.

I thought of using an \ifcase-command but as far as I know this command only accepts a number as input but I would like to use the variable as input. What I basically need is something like a " if the value is equal to male, print <text1>, if the value is equal to female print <text2> in all other cases print <> "-syntax but I am not very good at programming, so can anyone help?

• You may already know this... Unless you are using these in a style file, or following a \makeatletter invocation in your TeX file, the use of the @ symbol in variable names will not work with the syntax you are using. – Steven B. Segletes Jun 20 '16 at 13:49
• @Steven B. Segletes :Yes, I already know this. As written above, this is an extract from my document class, so from the .cls file. For \@author ´ and \@handindate´ this works perfectly in my .tex file. The point is only the insertion of "Mr." or "Mrs." by LaTeX. – Enno Jun 20 '16 at 14:16

Do the check by comparing with “male” and “female”:

\documentclass{article}

\makeatletter
\newcommand{\Handed}{%
\par
Handed in by \Apply@Gender\@Author\space on \@Handindate
\par
}

\newcommand{\@Gender}{Unknown}
\newcommand{\@Author}{Unknown}
\newcommand{\@Handindate}{Unknown}


\newcommand{\Apply@Gender}{%
\begingroup
\long\def\@male{male}\long\def\@female{female}%
\ifx\@Gender\@male
Mr.~%
\else
\ifx\@Gender\@female
Mrs.~%
\fi
\fi
\endgroup
}
\makeatother

\begin{document}

\Gender{male}
\Author{G. Byron}
\Handindate{2016-06-21}

\Handed

\bigskip

\Gender{female}
\Author{M. Wollstonecraft}
\Handindate{2016-06-22}

\Handed

\bigskip

\Gender{whatever}
\Author{The Creature}
\Handindate{2016-06-23}

\Handed

\end{document}


This example uses xstring, and pgffor to create a lopp that iterates through a list of accepted phrases. Adding more phrases is as easy as adding them to the list.

\def\genderInput{%
%Input: <input gender>/<output phrase>,% (without the <>, ending by commma)
female/Mrs.,%
male/Mr.,%
boy/young Mr.,%
girl/Miss,%
none/}


Note that in british, this would be Mr and Mrs without the dot.

Output

Code

\documentclass[11pt]{article}
\usepackage{xstring}
\usepackage{pgffor}

\def\genderInput{%
%Input: <input gender>/<output phrase>,% (without the <>, ending by commma)
female/Mrs.,%
male/Mr.,%
boy/young Mr.,%
girl/Miss,%
none/}

\makeatletter
\newcommand{\@handindate}{\@handindate}

\newcommand{\@gender}{Unknown}

\gender{male}
\author{Joe Doe}

\newcommand{\getName}{%
\foreach \theGender/\theString in \genderInput{%
%\theGender - \theString
%\theGender
\IfSubStr{\@gender}{\theGender}{%
\theString~%
\breakforeach}{%
%invalid gender input %\texttt{\slash}
}%
}\@author%
}
\makeatother
\begin{document}
This document was written by \getName.

\gender{female}
\author{Jane Doe}

This document was written by \getName.

\gender{}
\author{Duck}

This document was written by \getName.

\gender{boy}
\author{Jimmy Nogood}

This document was written by \getName.

\gender{girl}
\author{Lily Johnson}

This document was written by \getName.
\end{document}

• Sadly this is not what I am looking for. I just wrote that stuff with the "Herr" and "Herrn" to show that it is not possible to solve this problem by "cheating". I will rephrase my question in order to make it clear. But thanks for your effort anyway. – Enno Jun 21 '16 at 19:14
• @Enno After your rewrite of the question, I still feel that this answer will accomplish exactly that. Just need to translate Herr and Frau into english. Is this what you need? – Runar Jun 21 '16 at 22:26
• Strictly speaking you are right, this answer solves this particular problem. But as you use the "male" in "female" to differentiate between the options this is limited to "male" and "female". So it will not work if I decide to use for example "boy" and "girl". But in this particular case your answer will work. – Enno Jun 22 '16 at 16:56
• ok, does this make it easier? I hope I am not misinterpreting anything here, but now I've made it really easy to make to phrases. Simply add yours to the list. – Runar Jun 22 '16 at 17:25
• This is actually quite nice because I can now freely choose the input and the output and can have as many options as I want. I already accepted egregs answer but this is also a good solution. You shall get my upvote. – Enno Jun 22 '16 at 17:43