4

I'm still learning latex, and copying and pasting a lot of what I don't understand, so apologies for the ignorance.

Assuming a document like this:

\documentclass{mycls}


\title{A title}
\targetaudience{A target audience}

\begin{document}

\maketitle

\end{document}

In my .cls I have something like this:

\newcommand{\targetaudience}[1]{\gdef\tf@targetaudience{#1}}
\targetaudience{\@empty}

I am trying to add content to my .cls, the part that defines the \maketitle page. I want to add to the title page, something conditionally, i.e. something like this:

\ifx\tf@targetaudience\empty
\else
       \textbf{Target Audience:} & \tf@targetaudience\\
\fi

I know that people like to have a "minimum working example" but I honestly have no idea how to turn what I have into an example. I am googling for different ways of doing "if" and nothing has worked so far. Any tips would be appreciated.

5

It depends on how want define "nothing". Some people define nothing to be a specific thing that exists, while others test for nothing as being something that doesn't exist.

Let's consider the following content in your .cls:

\newcommand{\targetaudience}[1]{\gdef\tf@targetaudience{#1}}
\targetaudience{\@empty}

You're defining \targetaudience to redefine (and/or overwrite) \tf@targetaudience, and immediately setting it to \@empty. So, you can test whether \tf@targetaudience looks like \@empty in order to see whether the user didn't do anything:

\expandafter\ifx\expandafter\@empty\tf@targetaudience
  Target audience not set.
\else
  Target audience set: \@tf@targetaudience
\fi

We have to expand \tf@targetaudience in order to test it against \@empty. Alternatively, consider

\newcommand{\targetaudience}[1]{\gdef\tf@targetaudience{#1}}

without setting any \targetaudience. Now you can test whether \tf@targetaudience exists or not:

\@ifundefined{tf@targetaudience}
  {Target audience not set.}
  {Target audience set: \@tf@targetaudience}

An e-TeX approach similar to the above would be

\ifcsname tf@targetaudience \endcsname
  Target audience set: \@tf@targetaudience
\else
  Target audience not set.
\fi
  • I really appreciate the time you took to provide such a helpful answer to a latex newbie! This is perfect. – Jacob Feb 13 '17 at 4:39

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.