2
\newcommand{\entry}[4]
{
\hspace{\ls}
\textbf{\MakeUppercase{#1},}\MakeUppercase{#2}
\hfill
\MakeUppercase{#3}

\hspace{\ls}
{#4}
\vspace{-0.7em}
}

If I leave the 4th argument blank, I do not want the space to be shown in the code (i.e. my itemize should appear/start on the line where argument #4 would have been).

\entry
{1}
{2}
{3}
{4}
\begin{itemize}
\item one
\item two
\end{itemize}

\entry{1}{2}{3}{}
\begin{itemize}
\item one
\item two
\end{itemize}
3
  • 1
    Welcome! Could you please provide us with a complete minimal working example that starts with \documentclass and ends with \end{document}? Anyone answering this will want to know what \ls it, at least.
    – user194703
    Jan 29, 2020 at 5:27
  • 1
    You may might be interested in this thread.
    – user194703
    Jan 29, 2020 at 5:33
  • Several methods for testing whether a macro argument is empty/does not contain any token at all are elaborated in the answers to the question Expandable test for an empty token list—methods, performance, and robustness. Jan 30, 2020 at 2:50

2 Answers 2

2

You can check whether an argument is empty to condition on setting it or not:

enter image description here

\documentclass{article}

\newlength{\ls}
\setlength{\ls}{1.5em}

\newcommand{\entry}[4]{%
  \hspace{\ls}%
  \textbf{\MakeUppercase{#1},}\MakeUppercase{#2}
  \hfill
  \MakeUppercase{#3}

  % https://tex.stackexchange.com/q/53068/5764
  \if\relax\detokenize{#4}\relax\else
    \hspace{\ls}%
    {#4}
  \fi
  \vspace{-0.7em}
}

\begin{document}

\entry{1}{2}{3}{4}
\begin{itemize}
  \item one
  \item two
\end{itemize}

\entry{1}{2}{3}{}
\begin{itemize}
  \item one
  \item two
\end{itemize}

\end{document}

Note the correction of some spurious spaces in your definition of \entry.

2

You could use the ifmtarg package which checks if an argument is empty or not.

% mtargprog. tex SE 526111 is argument empty

\documentclass{article}
\usepackage{ifmtarg}

\newlength{\ls}
\setlength{\ls}{1.5em}

\makeatletter
\newcommand{\entry}[4]{%
  \hspace{\ls}%
  \textbf{\MakeUppercase{#1},}\MakeUppercase{#2}
  \hfill
  \MakeUppercase{#3}

  \@ifnotmtarg{#4}{% only do this if the argument is empty
    \hspace{\ls}%
    {#4}%
  \vspace{-0.7em}
  } % end of \@ifnotmatarg
} % end of \entry
\makeatother

\begin{document}

\entry{1}{2}{3}{4}
\begin{itemize}
  \item one
  \item two
\end{itemize}

\entry{1}{2}{3}{}
\begin{itemize}
  \item one
  \item two
\end{itemize}

\end{document}

With apologies to @Werner for using some of his code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .