`\if` funktioniert so nicht. Aus dem TeXbook:
> * `\if` <token1> <token2> (test if character codes agree)
> TeX will expand macros following `\if` until two unexpandable tokens are found. If either token is a control sequence, TeX considers it to have character code 256 and category code 16, unless the current equivalent of that control sequence has been `\let` equal to a non-active character token. In this way, each token specifies a (character code, category code) pair. The condition is true if the character codes are equal, independent of the category codes. For example, after `\def\a{*}` and `\let\b=*` and `\def\c{/}`, the tests ‘`\if*\a`’ and ‘`\if\a\b`’ will be true, but ‘`\if\a\c`’ will be false. Also ‘`\if\a\par`’ will be false, but ‘`\if\par\let`’ will be true.
Das bedeutet, dass der Test `\if\a -1` expandiert zu `\if-1.0-1` was dazu führt, dass die ersten beiden Zeichen `-` und `1` verglichen werden (offensichtlich immer falsch). Der Rest `.0-1` landet im Wahr-Zweig und würde ausgegeben werden wenn `-` und `1` jemals gleich sein würden. Wenn du verleichen möchtest ob zwei Makros denselben Inhalt haben, verwende `\ifx`.
> * `\ifx` <token1> <token2> (test if tokens agree)
> In this case, TeX does *not* expand control sequences when it looks at the two tokens. The condition is true if (a) the two tokens are not macros, and they both represent the same (character code, category code) pair or the same TeX primitive or the same `\font` or `\chardef` or `\countdef`, etc.; or if (b) the two tokens are macros, and they both have the same status with respect to `\long` and `\outer`, and they both have the same parameters and “top level” expansion. For example, after ‘`\def\a{\c}` `\def\b{\d}` `\def\c{\e}` `\def\d{\e}` `\def\e{A}`’, an
`\ifx` test will find `\c` and `\d` equal, but not `\a` and `\b`, nor `\d` and `\e`, nor any other combinations of `\a`, `\b`, `\c`, `\d`, `\e`.
\documentclass{article}
\usepackage{pgffor}
\begin{document}
\pgfmathsetmacro{\a}{-1}
% Konstanten zum vergleichen
\pgfmathsetmacro{\zero}{0}
\pgfmathsetmacro{\one}{1}
\pgfmathsetmacro{\mone}{-1}
$a x$ wird ausgegeben als:
\ifx\a\zero
\else
\ifx\a\one
$x$%
\else
\ifx\a\mone
$-x$%
\else
$\a x$%
\fi
\fi
\fi
\end{document}
[![alt text][1]][1]
Bisschen einfacher geht's mit `expl3`.
\documentclass{article}
\usepackage{xparse}
\begin{document}
\def\a{-1}
$a x$ wird ausgegeben als:
\ExplSyntaxOn
\int_case:nnF
{ \a }
{
{ -1 } { $-x$ }
{ 0 } { }
{ 1 } { $ x$ }
}
{ $\a x$ }
\ExplSyntaxOff
\end{document}
[1]: http://texwelt.de/wissen/upfiles/test_367.pnghttp://texwelt.de/wissen/upfiles/test_367.png