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.
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}
\foo
without opt. arg. and\foo[shubidoo]
with opt. arg.) – moewe Jan 14 '19 at 20:13\NewDocumentCommand{\test}{+g}{\IfValueF{#1}{omitted}{given}}
fromxparse
– user31729 Jan 14 '19 at 20:14\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 '19 at 20:49\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 '19 at 15:47