# Include string if variable is defined, leave blank otherwise

I'm trying to have something appear conditional on if an argument was given.

In the beginning of my document I want to be able to fill in two pieces of data

% Pre or Post epoxy?
\epoxy{pre}
% Checklist ID number
\idnum{123456}


and then have them appear in this form

(\@epoxy-epoxy -- \@idnum)


at various places in the document if they are given and have the line be totally blank if not

I've set up the class document like this

\def\epoxy#1{\gdef\@epoxy{#1}}
\def\idnum#1{\gdef\@idnum{#1}}


I want to define the document header to be either

\AtBeginDocument {\listtitle \newline (\@epoxy-epoxy -- \@idnum) \newline \header \begin{checklist}}


or

\AtBeginDocument {\listtitle \newline \newline \header \begin{checklist}}


depending on if the idnum and epoxy arguments are given

Is there a way for me to do this?

• Try \ifx\@empty\@epoxy\relax ... \fi – John Kormylo Jul 6 '17 at 21:02

Initialize \@epoxy and \@idnum to something fixed; then define a \@print@epoxy@idnum macro:

\let\@epoxy\hfuzz % initialize
\let\@idnum\vfuzz % initialize
\def\epoxy#1{\gdef\@epoxy{#1}}
\def\idnum#1{\gdef\@idnum{#1}}
\def\@print@epoxy@idnum{%
\ifx\@epoxy\hfuzz
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\ifx\@idnum\vfuzz
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\mbox{}}{(\@epoxy--epoxy -- \@idnum)}%
}%
}


You don't have to have conditionals you can just arrange that the internal macros produce nothing unless both commands have been used. So here versions 1-3 produce a blank line but 4 and 5 show the two terms.

\documentclass{article}

\def\inittitle{%
\def\stuff{\mbox{}}%
\def\xidnum##1{}%
\def\epoxy##1{\def\stuff{\xidnum{##1-epoxy -- }}}%
\def\idnum##1{\def\xidnum####1{####1##1}}%
}

\setlength\parskip{20pt}

\begin{document}

\inittitle
1title \newline \stuff  \newline zzz

\inittitle
\epoxy{pre}
2title \newline \stuff  \newline zzz

\inittitle
\idnum{123456}
3title \newline \stuff  \newline zzz

\inittitle
\epoxy{pre}
\idnum{123456}
4title \newline \stuff  \newline zzz

\inittitle
\idnum{123456}
\epoxy{pre}
5title \newline \stuff  \newline zzz

\end{document}


You can set Boolean conditions for each of them and then set them true at assignment. If you always have either none or both it is enough with one condition. Here the ones defined will be written.

\documentclass{article}
\newif\ifepoxy\epoxyfalse
\newif\ifidnum\idnumfalse
\makeatletter
\def\epoxy#1{\gdef\@epoxy{#1}\epoxytrue}
\def\idnum#1{\gdef\@idnum{#1}\idnumtrue}
\def\writeStuff{%
\ifepoxy
\@epoxy-epoxy
\fi
\ifidnum
-- \@idnum
\fi}
\makeatother
\begin{document}
Test1: \writeStuff

\epoxy{pre}
Test2: \writeStuff

\idnum{123456}
Test3: \writeStuff

\end{document}


EDIT -- Simplification

If you want to test if the command is defined or not you can use \@ifundefined. To test the definitions separately you can simplify my previous answer as:

\documentclass{article}
\makeatletter
\def\epoxy#1{\gdef\@epoxy{#1}}
\def\idnum#1{\gdef\@idnum{#1}}
\def\writeStuffII{%
\@ifundefined{@epoxy}{}{\@epoxy-epoxy}%
\@ifundefined{@idnum}{}{ -- \@idnum}
}
\makeatother
\begin{document}


If instead both should be defined to write anything, the conditions can be nested:

\def\writeStuffIII{%
\@ifundefined{@epoxy}{}{%
\@ifundefined{@idnum}{}{\@epoxy-epoxy -- \@idnum}}
}