0

This is a question re: Lost global definition after \ifthenelse using package xifthen. I'll reference the Answer given in this question here since it's clear and clean.

MWE:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{xifthen}

\newcommand{\test}[1][]{%
\ifthenelse{\equal{#1}{}}{omitted}{given}%
}

\begin{document}
\par The optional argument was \test[].
\par The optional argument was \test[shubidu].
\end{document}

Problem:

How to rewrite the command definition of \test so that:

\test{}

Output: omitted

\test{shubidu}

Output: given

EDIT:

Thank you everyone for your tips and tricks. For some more information that may help clarify the purpose of this question: I'm trying to define optional args using {} instead of [] because I'm in the process of making a TeX -> LyX transfer. LyX inserts an optional arg with [] if you click "Insert" on a drop-down menu and type the arg. Otherwise, LyX will insert your arg with {} if you do what I've done below. I would like to keep the formatting of what I've done below which is written with this: \newcommand{\BriefName}[1]{\xdef\BriefName{#1}} but make the argument inserted for \BriefName optional. Thus, if the LyX user enters nothing in the \BriefName line, the program still compiles, with an "empty" argument for the \BriefName command.

enter image description here

Updated MWE:

This is more what I'm looking for. I created this thanks to this Q&A.

\documentclass{article}
\usepackage{xifthen}

\newcommand{\BriefName}[1]{ %
        \xdef\BriefName{#1}\ifx\BriefName\empty
          {}%
        \else
          {#1}%
        \fi
}%

\begin{document}
\par This should be empty:\BriefName{}
\par This should also be empty: \BriefName
\par This should say Argument: \BriefName{Argument}
\par This should also say Argument: \BriefName
\end{document}
  • 4
    So strictly speaking, the argument is mandatory, but it may be empty. (Which is different from the usual meaning of optional argument in LaTeX, where one has \foo without opt. arg. and \foo[shubidoo] with opt. arg.) – moewe Jan 14 at 20:13
  • 1
    tex.stackexchange.com/q/53068/35864 may be interesting. – moewe Jan 14 at 20:14
  • 3
    This is confusing syntax and is not recommended, but it is possible with \NewDocumentCommand{\test}{+g}{\IfValueF{#1}{omitted}{given}} from xparse – user31729 Jan 14 at 20:14
  • 1
    why are you using the iftenelse test there at all, rather than just use the optional argument? In both your uses \test[] and \test[shubidu] you have used the optional argument, but you have defined the argument to be optional so you could just use \test if you do not need the argument. – David Carlisle Jan 14 at 20:49
  • Thank you for these comments, they were helpful. @DavidCarlisle yes I agree with you that \test[] can be called simply as \test{} and this was badly represented in my question/MWE. The issue is in LyX, once I create a command that I want to be used written like this: \newcommand{\BriefName}[1]{\xdef\BriefName{#1}} the LyX application will automatically call the command as \BriefName{Argument} if the user types anything or as \BriefName{} if the user types nothing. In the latter case, the document breaks as a result of this LyX feature. – jed Jan 15 at 15:47
1

You can do it with xparse; the trick is not redefining \BriefName as you're trying to do, but another control sequence holding the current value (empty at the beginning).

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\BriefName}{g}
 {
  \IfNoValueF{#1}
   {
    \tl_gset:Nn \g_jalep_briefname_tl { #1 }
   }
  \tl_use:N \g_jalep_briefname_tl
 }
\tl_new:N \g_jalep_briefname_tl
\ExplSyntaxOff

\begin{document}

This should be empty:\BriefName

This should also be empty: \BriefName[]

This should say Argument: \BriefName{Argument}

This should also say Argument: \BriefName

\end{document}

However, the standard LaTeX syntax uses [] for optional arguments, so it would be much better to stick with it.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\BriefName}{o}
 {
  \IfNoValueF{#1}
   {
    \tl_gset:Nn \g_jalep_briefname_tl { #1 }
   }
  \tl_use:N \g_jalep_briefname_tl
 }
\tl_new:N \g_jalep_briefname_tl
\ExplSyntaxOff

\begin{document}

This should be empty:\BriefName

This should also be empty: \BriefName[]

This should say Argument: \BriefName[Argument]

This should also say Argument: \BriefName

\end{document}

enter image description here

The version with [] also admits a solution with classical tools.

\documentclass{article}

\newcommand{\BriefName}[1][]{%
  \if\relax\detokenize{#1}\relax
  \else
    \gdef\jalepbriefname{#1}%
  \fi
  \jalepbriefname
}
\def\jalepbriefname{}

\begin{document}

This should be empty:\BriefName

This should also be empty: \BriefName[]

This should say Argument: \BriefName[Argument]

This should also say Argument: \BriefName

\end{document}
  • Works perfect, thank you! Can I ask how to change the first solution with {} so that optional arg is only printed when called like: \BriefName and not when inserting the optional argument in: \BriefName{Argument}? Your solution perfectly answered my question asked here but I simplified the need of my final TeX document for this question. – jed Jan 15 at 15:34
  • 1
    @Jalep Sorry, I know nothing about LyX and don't want to. If a tool imposes its will on you, change the tool. – egreg Jan 15 at 18:55
  • Ok, no worries. Thanks so much for your solution anyway -- it was helpful and I learned a lot. – jed Jan 15 at 19:08
1

To get what you ask you just need to delete [] in the declaration

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{xifthen}

\newcommand{\test}[1]{%
\ifthenelse{\equal{#1}{}}{omitted}{given}%
}

\begin{document}

The optional argument was \test{}.

The optional argument was \test{shubidu}.

\end{document}

However the original example appears to have been mis-using the optional argument syntax.

The intended syntax for the "not given" case is \test not \test[]

So you do not need to test for an empty argument, latex has already tested for [ being present:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{xifthen}

\newcommand{\test}[1][default]{#1\ldots}

\begin{document}

The optional argument was \test.

The optional argument was \test[shubidu].

\end{document}

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.