2

This code uses LaTeX3, the xparse-package and in particular the \IfNoValueTF command as explained in Will Robertson's answer to the OP "Different command definitions with and without optional argument".

It sounds a little bit strange if you want to use a command called \IfNoValueTF, of which the name seems to suggest it checks for whether or not an argument contains any value, to have to ask a question to force a return as if the check resulted in a "non-value", in a case where in everyday language one would argue there is no non-empty-value in that argument indeed.

But, quite on the contrary ... \IfNoValueTF only seems to check whether or not there are any brackets. Please take a look at the self-explanatory MWE below, which checks whether or not a third argument is included.

Of course, quite possibly I have just misunderstood how to use either \IfNoValueTF or \DeclareDocumentCommand \mainentry { m m o } {%.

MWE

\documentclass[a4paper]{article}
\usepackage{xparse}

\DeclareDocumentCommand \mainentry { m m o } {%
  \IfNoValueTF {#3} {%
\Large {\bfseries#1} \endgraf #2 \scriptsize NO \normalsize \vskip 1cm
  }{%
\Large {\bfseries#1} \endgraf #2 \endgraf #3 \scriptsize YES \normalsize \vskip 1cm
  }%
}

\begin{document}

A) There should be a YES here. OK.

\mainentry{
first
}{
second
}[
third
]

B) There should be a NO here. OK.

\mainentry{
first
}{
second
}

C) There should be a NO here. FAIL.

\mainentry{
first
}{
second
}[]

D) There should be a NO here. FAIL.

\mainentry{
first
}{
second
}[
]


E) There should be a NO here. FAIL.

\mainentry{
first
}{
second
}[
%
]

\end{document}
  • 3
    A missing argument is not the same as an empty argument or an argument with spaces. If you want to test for a blank argument (empty or only spaces), use \ifblank from etoolbox, or \tl_if_blank:nTF from expl3. – Ulrike Fischer Oct 7 '17 at 13:07
  • Thanks. If you could find some time, I would be interested in an answer. After your comment, I tried using \tl_if_blank:nTF but because I am so unexperienced I couldn't make it happen. – Vincent Mia Edie Verheyen Oct 7 '17 at 13:25
  • \ExplSyntaxOn \NewDocumentCommand \mainentry { m m O{} } { .. \tl_if_blank:nTF { #3 } { NO } { YES } .. } \ExplSyntaxOff. – Manuel Oct 7 '17 at 13:28
  • Thanks to the both of you. @Manuel Why did have to change { m m o } into { m m O{} } please? – Vincent Mia Edie Verheyen Oct 7 '17 at 13:37
  • o is something that works precisely for \IfValue tests. O{} is just an optional argument with default empty value (if you did O{asdf} then asdf would be the default value), and with that one you can have a correct \tl_if_blank: test (no optional = blank, optional but empty = blank, optional with something inside = not blank). – Manuel Oct 7 '17 at 13:46
2

As per Ulrike Fischer's comment, combined with Manuel's first comment:

One has to change the lines

\DeclareDocumentCommand \mainentry { m m o } {%
  \IfNoValueTF {#3} {%

into

\DeclareDocumentCommand \mainentry { m m O{} } {%
  \tl_if_blank:nTF { #3 } {%

Manuel's second comment explained that for O{} is just an optional argument with a default empty value.

This works both for blank arguments, arguments with spaces or even arguments of which there are no brackets present.

  • Or create an \IfEmptyTF command: \ExplSyntaxOn \cs_set_eq:NN \IfEmptyTF \tl_if_blank:nTF \ExplSyntaxOff. – jon Oct 7 '17 at 14:23
  • Using multiple comments from various people and gluing an answer together is not really kosher, one should rather ask the users to provide an answer – user31729 Oct 7 '17 at 18:52
  • @ChristianHupfer OK. I just thought they didn't want to, as they already answered in the comments ... Users with a very high score have asked me to provide the answer myself before ... I guess they sometimes might be too busy to want to do it themselves. – Vincent Mia Edie Verheyen Oct 7 '17 at 22:43

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.