Generalifthenelse doesn't work with providecommand

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

ifthenelse doesn't work with providecommand

Post by leo simon »

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.

Code: Select all

\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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

ifthenelse doesn't work with providecommand

Post by cgnieder »

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

Code: Select all

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

Code: Select all

\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

ifthenelse doesn't work with providecommand

Post by leo simon »

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.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

ifthenelse doesn't work with providecommand

Post by cgnieder »

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
Post Reply