## LaTeX forum ⇒ General ⇒ ifthenelse doesn't work with providecommand Topic is solved

LaTeX specific issues not fitting into one of the other forums of this category.
leo simon
Posts: 17
Joined: Wed Aug 19, 2009 5:41 pm

### ifthenelse doesn't work with providecommand

Hi

I'm trying to define a variable conditional on the setting of a counter, with an optional argument. Specifically, in the example below, the variable gOpt should return the second argument if the counter general is set to 1, and the first if the counter is set to 0. If only one argument is provided, and general = 0, the default should be used.

This command works fine *if* only one argument is provided, but if two are provided, gOpt returns both #1 and #2, rather than choosing depending on the value of \thegeneral.

If I use a \def command, (gAlt) then everything works ok, but I can't specify a default option.

Is this a bug in \providecommand? Is there an easy way around the problem? Thanks very much for any advise.

\documentclass[11pt,reqno,fleqn]{amsart}\usepackage{ifthen}\newcounter{general}\setcounter{general}{1}\providecommand{\gOpt}[2][this is the specific case]{\ifthenelse{\thegeneral=1}{#2}{#1}}\def\gAlt#1#2{\ifthenelse{\thegeneral=1}{#2}{#1}}\begin{document}\gOpt{this is the general case} \gOpt{this is the specific case}{this is the general case} \gAlt{this is the specific case}{this is the general case} \ifthenelse{\thegeneral=1}{this is the general case}{this is the specific case}\end{document}
Last edited by cgnieder on Thu Aug 01, 2013 12:07 am, edited 1 time in total.
Reason: corrected block code markup, added inline code markup

cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm
You defined \gOpt to have an optional argument so you need to use LaTeX's syntax for optional arguments, that is, square brackets:

\documentclass[11pt,reqno,fleqn]{amsart}\usepackage{ifthen}\newcounter{general}\setcounter{general}{1}\providecommand{\gOpt}[2][this is the specific case]{\ifthenelse{\thegeneral=1}{#2}{#1}}\def\gAlt#1#2{\ifthenelse{\thegeneral=1}{#2}{#1}}\begin{document}\gOpt{this is the general case} \gOpt[this is the specific case]{this is the general case} \gAlt{this is the specific case}{this is the general case} \ifthenelse{\thegeneral=1}{this is the general case}{this is the specific   case} \setcounter{general}{2} \gOpt{this is the general case} \gOpt[this is the specific case]{this is the general case} \gAlt{this is the specific case}{this is the general case} \ifthenelse{\thegeneral=1}{this is the general case}{this is the specific   case} \end{document}

ifthen.png (6.69 KiB) Viewed 4192 times

You're probably aware that \providecommand{<cmd>}{<definition>} only defines <cmd> if it doesn't exist yet and otherwise silently does nothing? \newcommand would be the usual variant.

BTW: you don't need the ifthen package for this:

\documentclass{article} \newcounter{general}\setcounter{general}{1} \makeatletter% \ifnumequal{<number expression 1>}{< number expression 2>}%   {<true>}%   {<false>}% we /do/ want to provide this one: the etoolbox package already defines it,% probably in a more robust way:\providecommand\ifnumequal[2]{%   \ifnum\numexpr#1\relax=\numexpr#2\relax     \expandafter\@firstoftwo   \else     \expandafter\@secondoftwo   \fi}\makeatother \newcommand{\gOpt}[2][this is the specific case]{%   \ifnumequal{\value{general}}{1}{#2}{#1}}\def\gAlt#1#2{\ifnumequal{\value{general}}{1}{#2}{#1}} \begin{document}\gOpt{this is the general case} \gOpt[this is the specific case]{this is the general case} \gAlt{this is the specific case}{this is the general case} \ifnumequal{\value{general}}{1}{this is the general case}{this is the specific   case} \setcounter{general}{2} \gOpt{this is the general case} \gOpt[this is the specific case]{this is the general case} \gAlt{this is the specific case}{this is the general case} \ifnumequal{\value{general}}{1}{this is the general case}{this is the specific   case}\end{document}

Regards
site moderator & package author

leo simon
Posts: 17
Joined: Wed Aug 19, 2009 5:41 pm
Aghh, thanks very much, I should have known that. I didn't know about \providecommand, it's just that \newcommand is so annoying about errors. But I didn't know I could do without the ifthen call.

Thanks again, Leo
Last edited by cgnieder on Thu Aug 01, 2013 12:47 am, edited 1 time in total.
Reason: code markup

cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm
leo simon wrote:it's just that \newcommand is so annoying about errors.

but that's the good thing about it: cannot overwrite a macro by chance with it and LaTeX tells you if you would have. That's a very welcomed safety net, IMHO. If you want to overwrite a macro you have to do it knowingly with \renewcommand. \providecommand also doesn't overwrite things but it doesn't even tell you if it fails to define the macro!

Regards
site moderator & package author