LaTeX forum ⇒ Generalifthenelse 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

Postby leo simon » Wed Jul 31, 2013 10:28 pm

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.

  1. \documentclass[11pt,reqno,fleqn]{amsart}
  2. \usepackage{ifthen}
  3. \newcounter{general}
  4. \setcounter{general}{1}
  5. \providecommand{\gOpt}[2][this is the specific case]{\ifthenelse{\thegeneral=1}{#2}{#1}}
  6. \def\gAlt#1#2{\ifthenelse{\thegeneral=1}{#2}{#1}}
  7. \begin{document}
  8. \gOpt{this is the general case}
  9.  
  10. \gOpt{this is the specific case}{this is the general case}
  11.  
  12. \gAlt{this is the specific case}{this is the general case}
  13.  
  14. \ifthenelse{\thegeneral=1}{this is the general case}{this is the specific case}
  15. \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

Tags:

User avatar
cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Thu Aug 01, 2013 12:20 am

You defined \gOpt to have an optional argument so you need to use LaTeX's syntax for optional arguments, that is, square brackets:

  1. \documentclass[11pt,reqno,fleqn]{amsart}
  2. \usepackage{ifthen}
  3. \newcounter{general}
  4. \setcounter{general}{1}
  5. \providecommand{\gOpt}[2][this is the specific case]{\ifthenelse{\thegeneral=1}{#2}{#1}}
  6. \def\gAlt#1#2{\ifthenelse{\thegeneral=1}{#2}{#1}}
  7. \begin{document}
  8. \gOpt{this is the general case}
  9.  
  10. \gOpt[this is the specific case]{this is the general case}
  11.  
  12. \gAlt{this is the specific case}{this is the general case}
  13.  
  14. \ifthenelse{\thegeneral=1}{this is the general case}{this is the specific
  15. case}
  16.  
  17. \setcounter{general}{2}
  18.  
  19. \gOpt{this is the general case}
  20.  
  21. \gOpt[this is the specific case]{this is the general case}
  22.  
  23. \gAlt{this is the specific case}{this is the general case}
  24.  
  25. \ifthenelse{\thegeneral=1}{this is the general case}{this is the specific
  26. case}
  27.  
  28. \end{document}


ifthen.png
ifthen.png (6.69 KiB) Viewed 3761 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:

  1. \documentclass{article}
  2.  
  3. \newcounter{general}
  4. \setcounter{general}{1}
  5.  
  6. \makeatletter
  7. % \ifnumequal{<number expression 1>}{< number expression 2>}
  8. % {<true>}
  9. % {<false>}
  10. % we /do/ want to provide this one: the etoolbox package already defines it,
  11. % probably in a more robust way:
  12. \providecommand\ifnumequal[2]{%
  13. \ifnum\numexpr#1\relax=\numexpr#2\relax
  14. \expandafter\@firstoftwo
  15. \else
  16. \expandafter\@secondoftwo
  17. \fi
  18. }
  19. \makeatother
  20.  
  21. \newcommand{\gOpt}[2][this is the specific case]{%
  22. \ifnumequal{\value{general}}{1}{#2}{#1}}
  23. \def\gAlt#1#2{\ifnumequal{\value{general}}{1}{#2}{#1}}
  24.  
  25. \begin{document}
  26. \gOpt{this is the general case}
  27.  
  28. \gOpt[this is the specific case]{this is the general case}
  29.  
  30. \gAlt{this is the specific case}{this is the general case}
  31.  
  32. \ifnumequal{\value{general}}{1}{this is the general case}{this is the specific
  33. case}
  34.  
  35. \setcounter{general}{2}
  36.  
  37. \gOpt{this is the general case}
  38.  
  39. \gOpt[this is the specific case]{this is the general case}
  40.  
  41. \gAlt{this is the specific case}{this is the general case}
  42.  
  43. \ifnumequal{\value{general}}{1}{this is the general case}{this is the specific
  44. case}
  45. \end{document}


Regards
site moderator & package author

leo simon
Posts: 17
Joined: Wed Aug 19, 2009 5:41 pm

Postby leo simon » Thu Aug 01, 2013 12:37 am

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

User avatar
cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Thu Aug 01, 2013 12:46 am

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


Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests