Make a \newcommand argument optional without consequence

I have this code block in my .cls file -

\newcommand{\ExperienceEntry}[5]{
\noindent\makebox[\width][l]{#1} \hfill
\makebox[\width][l]{#2} \\
\noindent\makebox[\width][l]{#3} \hfill \\
\indent
\makebox[\width][l]{\textit{#4}} \vspace{5pt}\\
\noindent
\parbox[t][\height][c]{\textwidth}{\small{#5}}
\vspace{-1pt}
}


How do I make the 3rd argument (#3) optional? By optional I mean if I leave it empty {}, I do not want a gaping space in my document.

For example this is what happens:
This is the .tex code -
and this is the generated document -

I found some relevant documentation about different behaviour depending on the arguments supplied -
Different command definitions with and without optional argument
Optional argument for newcommand?
but i wasn't sure how to use it in my case.
https://stackoverflow.com/questions/1812214/latex-optional-arguments seems most relevant but here one has to define a default behavior whereas I want the default behavior to be to ignore the argument.

• I see no gap if I leave empty #2. By the way, \makebox[\width][l]{abc} is a complicated way to say \mbox{abc}. Feb 9, 2020 at 17:00
• Thanks for the \mbox tip! I edited the question to show an example of the generated gap. Feb 9, 2020 at 17:18
• also why all the \noindent that does nothing mid paragraph. Note it is confusing to call this an optional parameter as latex optional parameters are marked with [] not {} and can be omitted entirely not by using an empty argument. Feb 9, 2020 at 17:31

This is not an "optional argument" in LaTeX terminology, they use [] not {}. However you can test for the argument being non empty in several ways, for example

\if\relax\detokenize{#2}\relax\else
\mbox{#2}\\
\fi


if #2 is empty this ends up testing \if\relax\relax which is true, in any other case the test will be false, and so #2 will be used. (Note I simplified \makebox to \mbox and removed the spurious space before \\)

It looks like you meant to test #3 not #2..

Note

  \noindent\makebox[\width][l]{#3} \hfill \\


you do not need \hfill before \\ and you should not have a space before \hfill and as noted previously you can simply use \mbox, so you can replace this line by

\if\relax\detokenize{#3}\relax\else
\mbox{#3}\\
\fi

• I think I am making some mistake combining your code around {#1} with my existing code around it. This is what I am getting - \noindent\mbox{\if\relax\detokenize{#1}\relax\else{#1}} \hfill \mbox{#2}\\ fi Which does not work. What am I doing wrong? Feb 9, 2020 at 18:41
• @PratyushDas sorry the description was correct but I had a #1 instead of #2 in one place in the code. The fragment in my answer should replace your line \makebox[\width][l]{#2} \\  However as egreg commented an empty #2 will not make any gap with your code as posted I think you meant to test #3 the example you added later had an empty third argument not second. Feb 9, 2020 at 19:05
• @PratyushDas see extended answer Feb 9, 2020 at 19:08