How to define output depending on an variable with an status in LaTeX?
-
05-07-2019 - |
Question
I have some standard-texts, but some portion of it is different. But of these different parts only a few exists.
For instance I want:
\mytext{...}{a}
\mytext{...}{b}
That produces:
\section{Item: ...}\label{item...}
This is a standard item. Items of type a are very precious.
\section{Item: ...}\label{item...}
This is a standard item. Items of type b are cheap.
A simple solution to this would be to define commands mytexta and mytextb, but as I have more options I want more something like an if or switch in programming languages. Has anyone a solution for this problem?
Solution
The ifthen
package (which is included in a standard LaTeX installation) defines a command \ifthenelse
, which is used like this:
\usepackage{ifthen}
\ifthenelse{test}{then-code}{else-code}
so you could do something similar to:
\newcommand\mytext[1]{%
\ifthenelse{\equal{#1}{a}}{very precious}{%
\ifthenelse{\equal{#1}{b}}{cheap}{unknown}}}
For LaTeX programming, I'd recommend getting a copy of The LaTeX Companion. It's a really good reference for this stuff.
OTHER TIPS
You can use \newif\iffoo to declare a new condition. Then \footrue or \foofalse sets it to true or false, and you can use it by \iffoo ... \else ... \fi.
There are more conditionals, see pages 209ff in the TeXbook.