I've been looking for a switch/case statement in latex. Something
like:
\newcounter{myCounter}
\setcounter{myCounter}{2}
\begin{case}
\case{\equal{\value{myCounter}}{1}}{It's 1!}
\case{\equal{\value{myCounter}}{2}}{It's 2!}
\default{I don't know what to do with this!}
\end{case}
I played with boolexpr and came up with this:
\switch
\case{\equal{\value{tfigCount}}{9}}Yes%
\case{\equal{\value{tfigCount}}{7}} 7 is yes%
\case{\equal{\value{tfigCount}}{6}} 6 is yes%
\case{\equal{\value{tfigCount}}{1}} 1 is yes%
\endswitch
and many explosions ensued. I also tried:
\switch
\case{\value{tfigCount}=9}Yes%
\case{\value{tfigCount}=7} 7 is yes%
\case{\value{tfigCount}=6} 6 is yes%
\case{\value{tfigCount}=1} 1 is yes%
\endswitch
and got many errors, including:
Latex Error: ./DiagramTest.tex:168 Missing number, treated as zero.
Latex Error: ./DiagramTest.tex:168 Missing = inserted for \ifnum.
Latex Error: ./DiagramTest.tex:168 Missing number, treated as zero.
Latex Error: ./DiagramTest.tex:168 Argument of \bex@after@endswitch
has an extra }.
Runaway argument?
=======
Anyway, I didn't post a minimal, because I figured that a switch
statement was something that TeX people probably know about but I
don't. I'm just trying to execute some code based on the value of a
counter. It seems basic, but I'm trying to avoid 1000 ifthenelse
statements.
Thanks all! It seems I dip into you guys as a resource too often, but
I really do appreciate it.
> I've been looking for a switch/case statement in latex. Something
> like:
>
> \newcounter{myCounter}
> \setcounter{myCounter}{2}
> \begin{case}
> \case{\equal{\value{myCounter}}{1}}{It's 1!}
> \case{\equal{\value{myCounter}}{2}}{It's 2!}
> \default{I don't know what to do with this!}
> \end{case}
\documentclass{minimal}
\newcommand\testMyCounter[1]{%
\ifcase#1 % 0
I don't know what to do with this!
\or% 1
It's 1!
\or% 2
It's 2!
\else% >2
I don't know what to do with this!
\fi}
\newcounter{myCounter}
\begin{document}
\testMyCounter{\themyCounter}
\setcounter{myCounter}{2}
\testMyCounter{\themyCounter}
\stepcounter{myCounter}
\testMyCounter{\themyCounter}
\end{document}
Herbert
[ switch statement ]
I noticed a solution has already been posted. Still the following may
be of interest as a more general solution.
http://www.tug.org/TUGboat/Articles/tb14-1/tb38fine.pdf
http://www.tug.org/TUGboat/Articles/tb13-1/tb34fine.pdf
Regards,
Marc van Dongen
I've used nested if then stuff contained in newcommands like this
%
% If-Then-Else
\usepackage{ifthen}
%
%%%% activity bases shorthand %%%%
\newcommand{\ba}[1]{Base {#1}}
% Base Titles
\newcommand{\bat}[1]{\ifthenelse{#1=0}{-- Zero --}{% 0 else
\ifthenelse{#1=1}{Shrek}{% 1 else
\ifthenelse{#1=2}{Alchemy 101}{% 2 else
\ifthenelse{#1=3}{Dunk `em}{% 3 else
\ifthenelse{#1=4}{Wand Shoppe}{% 4 else
\ifthenelse{#1=5}{Witches Brew}{% 5 else
\ifthenelse{#1=6}{--6--}{% 6 else
\ifthenelse{#1=7}{Track Mold-making}{% 7 else
\ifthenelse{#1=8}{Dragon Quest}{% 8 else
\ifthenelse{#1=9}{Dragon Lair}{--oops--% 9 else
}}}}}}}}}}}
% names/colours of the Sixes
\newcommand{\six}[1]{\ifthenelse{#1=0}{-blank-}{% 0 else
\ifthenelse{#1=1}{\colorbox{red!50}{Cats}}{% 1 else
\ifthenelse{#1=2}{\colorbox{orange}{Bats}}{% 2 else
\ifthenelse{#1=3}{\colorbox{yellow}{Newts}}{% 3 else
\ifthenelse{#1=4}{\colorbox{green}{Rats}}{% 4 else
\ifthenelse{#1=5}{\colorbox{blue!50}{Frogs}}{% 5 else
\ifthenelse{#1=6}{\colorbox{cyan}{Toads}}{% 6 else
\ifthenelse{#1=7}{\colorbox{magenta}{Lizards}}{% 7 else
\ifthenelse{#1=8}{\colorbox{purple!70}{Spiders}}{--oops--% 8 else
}}}}}}}}}}
%
% usage
%
\section*{Sixes}
\six{1} -- the Witchs' familiar\\
\six{2} -- flying night creature\\
\six{3} -- creatures of the water\\
\six{4} -- scavengers of the night\\
\six{5} -- friends of the newt\\
\six{6} -- found in the swamps\\
\six{7} -- famous for gizzards\\
\six{8} -- creepy critters
--
> \newcounter{myCounter}
> \setcounter{myCounter}{2}
> \begin{case}
> \case{\equal{\value{myCounter}}{1}}{It's 1!}
> \case{\equal{\value{myCounter}}{2}}{It's 2!}
> \default{I don't know what to do with this!}
> \end{case}
==> it's a good idea to put \switch \endswitch in such an environment.
However, I wonder I will need the environ package to implement this...
>
> I played with boolexpr and came up with this:
>
> \switch
> \case{\equal{\value{tfigCount}}{9}}Yes%
> \case{\equal{\value{tfigCount}}{7}} 7 is yes%
> \case{\equal{\value{tfigCount}}{6}} 6 is yes%
> \case{\equal{\value{tfigCount}}{1}} 1 is yes%
> \endswitch
Please could you :
1) Send a complete compilable (or not) example
2) Read boolexpr documentation...
The syntax of \boolexpr is not the same as of ifthenelse!
Then:
% ------------------------------------------------------------------
\documentclass{minimal}\makeatletter
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{boolexpr}
\begin{document}\makeatletter
\newcounter{myCounter}
\setcounter{myCounter}{2}
\loggingall
\switch
\case{\number\value{myCounter}=1}{It's 1!}% use \number or \numexpr
\case{\number\value{myCounter}=2}{It's 2!}% use \number or \numexpr
\otherwise{I don't know what to do with this!}% use \otherwise not \default
\endswitch
\edef\result{%
\switch
\case{\ifnum\value{myCounter}=1 0\else 1\fi}{It's 1!}
\case{\ifnum\value{myCounter}=2 0\else 1\fi}{It's 2!}
\otherwise{I don't know what to do with this!}
\endswitch}
result in expansion (edef): \meaning\result
\stop
%------------------------------------------------------------------
Presently, the scanner used in boolexpr does not recognize \value...
I will (probably) add it - I have to think of it a bit before ;-)
Thanks for using \boolexpr !
With the new revision (2.9 2008/08/31) of boolexpr.sty - not uploaded yet - you will be able to
write more easily :
\edef\result{%
\switch[\numexpr\value{myCounter}=]
\case{1}{It's 1!}
\case{2}{It's 2!}
\otherwise{I don't know what to do with this!}
\endswitch}
\meaning\result% => macro:->{It's 2!}
Regards
> With the new revision (2.9 2008/08/31) of boolexpr.sty - not uploaded
> yet - you will be able to write more easily :
\boolexpr revision 2.9 uploaded.
Syntax:
\switch[\numexpr\value{myCounter}]
\case{=0}It's 1!%
\case{<2}It's 2!%
\case{<=10}It's <=10%
\otherwise{I don't know what to do with this!}
\endswitch
Now available (when mounted on CTAN...)
it would already have been installed if you had sent the upload to
www.tex.ac.uk or www.dante.de rather than the american site.
tug.ctan.org is good for european installations that arrive very late
(rainer and i tend to stay up late so i _mean_ "very"), but sending an
installation to vermont at 0100 (its) local time is to ask for delays.
but maybe you don't trust a british or german site...
--
Robin Fairbairns, Cambridge
I'm used to surf on tex.ac.uk (Graham Williams' Catalogue) nevertheless...