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}
\ifblank
from etoolbox, or\tl_if_blank:nTF
from expl3.\tl_if_blank:nTF
but because I am so unexperienced I couldn't make it happen.\ExplSyntaxOn \NewDocumentCommand \mainentry { m m O{} } { .. \tl_if_blank:nTF { #3 } { NO } { YES } .. } \ExplSyntaxOff
.{ m m o }
into{ m m O{} }
please?o
is something that works precisely for\IfValue
tests.O{}
is just an optional argument with default empty value (if you didO{asdf}
thenasdf
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).