0

I define two new macros and commands to change their content with the following commands in a .cls-file.

\newcommand{\@mymacroa}{}
\newcommand{\mymacroa}[1]{\renewcommand{\@mymacroa}{#1}}
\newcommand{\@mymacrob}{}
\newcommand{\mymacrob}[1]{\renewcommand{\@mymacrob}{#1}}

Now I want to test whether one (or both) of these is non-empty (i.e. has been modified from the default value) and if one of these has content, print their values. My previous attempts using \ifthenelse, \@ifnotmtarg and plain TeX \if have not been successful.

In pseudo-code, what I want to achieve is:

\@ifnotmtarg{\@mymacroa}\or\@ifnotmtarg{\@mymacrob} 
do {print some text and the macros which are non-empty}
  • 1
    Welcome to TeX.SX! To highlight code-blocks, either indent them by four spaces or use the {} on the gui. Also, rather than posting snippets of code can you please expand this to a compliable minimal working example. – Andrew Dec 23 '19 at 20:04
  • 1
    It might be better to say what you are actually trying to achieve as this seems to be a rather convoluted approach. In similar situations I have used either \providecommand to set a default value for a command or document class options to set defaults that can be easily changed. If I really wanted to do something like this then I would use a "binary counter" and then test for values of 0 (nothing set), 1 or 10 (one set) and 11 (both set). – Andrew Dec 23 '19 at 20:11
  • @Andrew What I want is designing a class file as a complement to a genealogytree. This class is meant to produce a pdf with a persons picture, their place and date of birth (and death respectively) in a tabular followed by some text. In the tabular, if either place or date birth is defined, I want to print the gtrsymborn sign followed by the place and/or date. I hope my intention becomes clear from this. – Enno Dec 23 '19 at 20:21
1

It is hard to say the best way to do this without knowing in what other ways this data will be used in the package, especially since I am not familiar with the genealogytree package.

With this caveat, one way to do this is to define commands, say \DateOfBirth and \PlaceOfBirth, to set the date and place of birth respectively, which could be saved in macros such as \@dateofbirth and \@placeofbirth. By setting the default values of both of these macros to \relax you can now test to see if either of them have been redefined by defining a new boolean, say \ifHaveDateOrPlace, and using something like:

% include the date and/or place of birth if available
\HaveDateOrPlacefalse% reset date and place boolean
\if\@dateofbirth\relax\else\HaveDateOrPlacetrue\fi
\if\@placeofbirth\relax\else\HaveDateOrPlacetrue\fi
\ifHaveDateOrPlace\gtrsymBorn \@placeofbirth \@dateofbirth \fi

Of course, you would also need macros to get the name of the image file and probably the persons name, but perhaps these are already provided by genealogytree. With this in place you could define a macro \MugShot so that the code

 \MugShot

 \PlaceOfBirth{Mars}
 \MugShot

 \DateOfBirth{Tuesday}
 \MugShot

 \PlaceOfBirth{Venus}
 \DateOfBirth{Wednesday}
 \MugShot

would produce:

enter image description here

(My default picture is a question mark.)

The full code for this MWE consists of the latex file:

\documentclass{myclass}

\begin{document}

 \MugShot

 \PlaceOfBirth{Mars}
 \MugShot

 \DateOfBirth{Tuesday}
 \MugShot

 \PlaceOfBirth{Venus}
 \DateOfBirth{Wednesday}
 \MugShot

\end{document}

together with the class file myclass.cls, which contains all of the content:

\LoadClass[12pt]{amsart}

\RequirePackage{genealogytree}
\RequirePackage{graphicx}

% boolean to keep track of place and date of birth
\newif\ifHaveDateOrPlace
% place of birth
\providecommand\@placeofbirth{\relax}
\newcommand\PlaceOfBirth[1]{\renewcommand\@placeofbirth{\space#1}}

% date of birth
\providecommand\@dateofbirth{\relax}
\newcommand\DateOfBirth[1]{\renewcommand\@dateofbirth{\space#1}}

\providecommand\@personpicture{{\Huge?}}
\newcommand\Picture[2][]{\edef\@personpicture{\noexpand\includegraphics[width=30mm,#1]{#2}}}

% reset the people data
\newcommand\ResetData{%
\renewcommand\@placeofbirth{\relax}%
\renewcommand\@dateofbirth{\relax}%
}

\newcommand\MugShot{%
  \begin{tabular}{c}
    \@personpicture\\
    % include the date and/or place of birth if available
    \HaveDateOrPlacefalse% reset date and place boolean
    \if\@dateofbirth\relax\else\HaveDateOrPlacetrue\fi
    \if\@placeofbirth\relax\else\HaveDateOrPlacetrue\fi
    \ifHaveDateOrPlace\gtrsymBorn \@placeofbirth \@dateofbirth \fi
  \end{tabular}%
  \ResetData
}

\endinput

If this data is only going to be used once then a better approach would probably be to define a single macro to print the photo, together with the associated data, or to use something like pgfkeys so that you can use a key-value syntax to specify everything.

| 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.