Ich möchte, dass je nachdem, ob \a x den Wert 0x oder 1x oder -1x besitzt, <void> oder x oder -x ausgegeben wird.

Die ersten beiden Fälle funktionieren. Was muss ich machen, damit im Fall "\a = -1" nicht -1.0x ausgegeben wird?

Öffne in Overleaf
%\documentclass[varwidth, margin=10mm]{standalone}
\documentclass{article}
\usepackage[ngerman]{babel}

\usepackage[fleqn]{amsmath} % fleqn = noindent bei alignat*
\usepackage{amsfonts, amssymb}
\usepackage{array}

\usepackage{pgffor} % damit auch reelle float-Zahlen in if-Methode
% \usepackage{tikz} % alternativ, lädt pgffor

\setlength{\parindent}{0pt}
\begin{document}

%\pgfmathtruncatemacro{\a}{3} %  0, 1 ,-1,  sonst % tut nicht mit -1 
\pgfmathsetmacro{\a}{-1} % tut nicht mit -1 
$a x$ wird ausgegeben als:
\if\a 0 {}% LEER
\else{%
     \if\a 1 {$x$}% NURx
     \else {%%
                \if\a -1 {$-x$}% NURMinusx
            \else {$\a x$}
            \fi
          }%%
          \fi
    }%
\fi
\bigskip

% ODER alte Idee: 
\newcounter{b}
\setcounter{b}{-1} %  0  ,   1  ,    -1  ,  int %    tut nicht mit float-Zahlen
$b x$  wird ausgegeben als:
\ifnum\value{b}=0 {}% LEER
\else{%
     \ifnum\value{b}= 1 {$x$}% NURx
     \else {%%
                \ifnum\value{b}= -1 {$-x$}% NURMinusx
            \else {$\theb x$}
            \fi
          }%%
          \fi
    }%
\fi
\bigskip

\end{document}

gefragt 26 Dez '17, 07:19

cis's gravatar image

cis
9.5k75452491
Akzeptiert-Rate: 29%

bearbeitet 26 Dez '17, 20:32


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

Öffne in Overleaf
\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

Bisschen einfacher geht's mit expl3.

Öffne in Overleaf
\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}
Permanenter link

beantwortet 27 Dez '17, 01:02

Henri's gravatar image

Henri
15.7k133943
Akzeptiert-Rate: 46%

bearbeitet 27 Dez '17, 01:16

Ah, klasse. Jetzt verstehe ich "if" wesentlich besser.

(27 Dez '17, 08:46) cis
Deine Antwort
Vorschau umschalten

Folgen dieser Frage

Per E-Mail:

Wenn sie sich anmelden, kommen Sie für alle Updates hier in Frage

Per RSS:

Antworten

Antworten und Kommentare

Markdown-Grundlagen

  • *kursiv* oder _kursiv_
  • **Fett** oder __Fett__
  • Link:[Text](http://url.com/ "Titel")
  • Bild?![alt Text](/path/img.jpg "Titel")
  • nummerierte Liste: 1. Foo 2. Bar
  • zum Hinzufügen ein Zeilenumbruchs fügen Sie einfach zwei Leerzeichen an die Stelle an der die neue Linie sein soll.
  • grundlegende HTML-Tags werden ebenfalls unterstützt

Frage-Themen:

×9
×5

gestellte Frage: 26 Dez '17, 07:19

Frage wurde gesehen: 5,040 Mal

zuletzt geändert: 27 Dez '17, 08:46