[Tex/LaTex] Using counters and \ifthen in a macro: what’s wrong

conditionalscountersmacros

I have (made, actually) a graphic of the forepaw-print of a bear that I want to use as a flourish mark at the end of chapters and a few other places. After finishing with the image, I created a mirror version, so that I had right and left paw versions, originally to see which looked better. The thought then came that it would be fun to alternate between the two at each occurrence, letting my bear, as it were, go for a walk through my book (on it's front paws, I know; let's not carry the notion too far).

For repeated inline graphics I use \newcommand* to create a "mini-macro" that does nothing but an \includegraphic of a given image in a particular size, mostly to be included in other macros; in this case, the two are called \lbpimage and \rbpimage, and by themselves they work fine.

Now I need an intermediary macro to switch between the two. What I intended is a \newcommand, \bearpaw, that would create another command (via \renewcommand*) called \bpaw, and then run it. Referencing a counter named paw and using \ifthen, \bpawis created with the intent of running one or the other of the "mini-macros" and then resetting paw from 0 to 1 or vise versa.

Of course, to be able to use \renewcommand* the commandname needs to be pre-existing, so before \newcommand*\bearpaw I initialize \bpaw with a dummy version, \newcommand*\bpaw{foo} thus:

\documentclass[letterpaper,twoside,12pt,final]{memoir}

\usepackage{graphicx}
\usepackage{ifthen}

\newcommand*{\lbpimage}%
  {\includegraphics[width=0.548in]{lbearpaw}}

\newcommand*{\rbpimage}%
  {\includegraphics[width=0.548in]{rbearpaw}}

\newcounter{paw}%     Preset to zero by default.

\newcommand*\bpaw%
  {foo}

\newcommand*\bearpaw%
  {\ifthenelse{\equal{paw}{0}}%
    {\renewcommand*\bpaw%
      {\lbpimage%
      \setcounter{paw}{1}}}{}%
  \ifthenelse{\equal{paw}{1}}%
    {\renewcommand*\bpaw%
      {\rbpimage%
      \setcounter{paw}{0}}}{}%
  \bpaw}

\begin{document}

\bearpaw \bearpaw \bearpaw

\end{document}

And what I get is:

foo
foo
foo

Clearly, my conditionals aren't working as I expected. What have I done wrong?

Best Answer

Commands \ifthenelse and \equal can be replace with \ifnum to get the macro work successfully.

\newcommand*\bearpaw%
  {\ifnum\thepaw=0%
    \renewcommand*\bpaw%
      {\lbpimage%
      \setcounter{paw}{1}}\fi%
  \ifnum\thepaw=1%
    \renewcommand*\bpaw%
      {\rbpimage%
      \setcounter{paw}{0}}\fi%
  \bpaw}

Using this you will not need the package ifthen.

Related Question