1

I have following command definition (it's entry of an entrylist) which works fine:

\newcommand{\entry}[4]{%
  #1&\parbox[t]{11.2cm}{%
    #2%
    \hfill%
    #3%
    \\#4%
  }\\}

Now I have a test case when 4th parameter can be empty. In that case I don't want to dispaly the new line defined before it. I've tried the following approach:

\newcommand{\entry}[4]{%
  #1&\parbox[t]{11.2cm}{%
    #2%
    \hfill%
    #3%
    \ifthenelse{\isempty{#4}}{}{\\#4}%
  }\\}

but it gives me an error:

Undefined control sequence. ^^I{test test2}

Missing number, treated as zero. ^^I{test test2}

Missing = inserted for \ifnum. ^^I{test test2}

Missing number, treated as zero. ^^I{test test2}

Could you please help?

  • they are not the error messages from TeX but possibly a (misleading) summary from your editor, the full error in the log will show which command is undefined. As you hav enot provided an example or the full error message it is very hard to help, please add a small test file that makes the error, – David Carlisle Feb 20 '18 at 22:59
  • Thank you for the hint! I've just discovered that I forgot to add \usepackage{xifthen}, \isempty was undefined. – kpater87 Feb 20 '18 at 23:07
  • do you use texstudio? – David Carlisle Feb 20 '18 at 23:11
  • Yes, I am using TeXstudio – kpater87 Feb 22 '18 at 19:11
  • sad that I can recognise an editor by the fact that it mangles the error messages, but: sourceforge.net/p/texstudio/feature-requests/1068 – David Carlisle Feb 22 '18 at 19:16
5

The problem appears to have been solved in the comments but I want to suggest two different approaches.

First, \newcommand makes it easy to define commands that have one optional argument, provided that it is the first argument. Using this feature you can rewrite your macro as:

\newcommand{\entry}[4][]{%
  #2&\parbox[t]{11.2cm}{%
    #3%
    \hfill%
    #4%
    \if\relax\detokenize{#1}\relax\else\\#1\fi
  }\\}

The [] says that by default #1 is empty. This macro is essentially the same as yours except that I have permuted the parameter numbers. The main difference is in how you use the macro:

\entry{second}{third}{fourth}

without the optional parameter or, if you want to give the optional paramter:

\entry[first]{second}{third}{fourth}

The second alternative is to use \NewDocumentCommand from the xparse package. The advantage of \NewDocumentCommand is that it lets you put the optional argument where ever you want it, including at the end:

\NewDocumentCommand{\entry}{ mmmo }{%
  #1&\parbox[t]{11.2cm}{%
    #2%
    \hfill%
    #3%
    \IfNoValueF{#4}{\\#4}% print #4 when it is given
  }\\}

The mmmo says that there are three manadtory arguments and one optional argument. Again, the only real difference to your code is in how the macro is used:

\entry{first}{second}{third}% without optional argument
\entry{first}{second}{third}[fourth]% with optional argument
  • Your answer is worth to mark it as a solution :) I will test it in my code! – kpater87 Feb 22 '18 at 20:31

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.