Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I think about trying $T=0$ but it seems kind of wrong because we do not assign but check for value == to... So what is the correct way to say that somewhere there was no such variable (say named T) that would be equal to some value (say 0)?

share|improve this question
8  
TeX is just a typesetter not an arbiter of notation in different fields. You appear to be talking about assignment in a programming language, each language has its own vocabulary and notation for assignment and missing values. TeX is flexible enough to typeset any such convention, but you need to say what you want, then someone can say how to do that in TeX. –  David Carlisle Apr 9 '12 at 17:13
add comment

closed as off topic by tohecz, lockstep, Roelof Spijker, percusse, Stefan Kottwitz Apr 24 '12 at 16:27

Questions on TeX - LaTeX Stack Exchange are expected to relate to TeX, LaTeX or related typesetting systems within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

You can use an construct to first check if the variable is defined, and then check to see if it has the appropriate value. Here I have used the xstring package's \IfEq for the numerical comparison but any other method will do:

enter image description here

Code:

\documentclass{article}
\usepackage{xstring}

\newcommand{\CheckValue}[1]{%
    \ifdefined\T%
        \IfEq{\T}{0}{%
           Variable T is defined and is zero%
        }{%
           Variable T is defined and is not zero%
        }%
    \else%
       Variable T not defined%
    \fi%
}%

\begin{document}
\CheckValue{\T}

\newcommand{\T}{1}
\CheckValue{\T}

\renewcommand{\T}{0}
\CheckValue{\T}
\end{document}

Without using the xstring package you can (thanks to @egreg):

  1. Use \pdfstrcmp which will check that the macro \T expands to zero, but not if \T is a counter

    \newcommand{\CheckValue}[1]{%
        \ifdefined\T%
            \ifnum\pdfstrcmp{\T}{0}=0\relax%
                Variable T is defined and is zero%
            \else%
                Variable T is defined and is not zero%
            \fi%
        \else%
            Variable T not defined%
        \fi%
    }%
    
  2. If you're sure that \T is a counter or a macro expanding to a number, then \ifnum\T=0 is sufficient:

    \newcommand{\CheckValue}[1]{%
        \ifdefined\T%
            \ifnum\T=0\relax%
                Variable T is defined and is zero%
            \else%
                Variable T is defined and is not zero%
            \fi%
        \else%
            Variable T not defined%
        \fi%
    }%
    
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.