2

I can't seem to get definitions of constants working in TikZ. I looked at this related question question and that too did not work for me. This example works, but not if the swap the \coordinate definition:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{ifthen}

\newcommand{\PI}{3.14}

\newcommand{\ValueOf}[1]{%
    \ifthenelse{\equal{#1}{PIE}}{3.14159}{}%
    \ifthenelse{\equal{#1}{E}}{2.78}{}%
}

\begin{document}

    Value of -PIE is -\ValueOf{PIE}.\par
    Value of E is \ValueOf{E}.

    \begin{tikzpicture}

    \coordinate (PointA)  at (-\PI,-1);             % Only this works
    %\coordinate (PointA) at (-\ValueOf{PIE},-1); 
    %\coordinate (PointA) at (\pgfextra{\pgfmathparse{-\ValueOf{PIE}}},-1); 

    \draw (PointA) circle (1);

\end{tikzpicture}
\end{document}

In case it is related, the package versions I have are:

pgfplots 2010/07/14 Version 1.4
pgf 2008/01/15 v2.00 (rcs-revision 1.12)

Peter Grill
  • 223,288

3 Answers3

2

The expression given for coordinates must expand to a number. The \ifthenelse seem not to do that. One solution would be to define \ValueOf in an expandable way:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{ifthen}

\newcommand{\PI}{3.14}

\makeatletter
\newcommand{\ValueOf}[1]{%
    \@ifundefined{ValueOf@#1}{%
    % Add your error handler
    }{%
    \@nameuse{ValueOf@#1}%
    }
}
\newcommand{\DefValueOf}[1]{%
    \@namedef{ValueOf@#1}%
}

\DefValueOf{PIE}{3.14159}
\DefValueOf{E}{2.78}%
\makeatother

\begin{document}
    Value of -PIE is -\ValueOf{PIE}.\par
    Value of E is \ValueOf{E}.

    \begin{tikzpicture}

    \coordinate (PointA)  at (-\PI,-1);             % Only this works
    \coordinate (PointA) at (-\ValueOf{PIE},-1); 
    %\coordinate (PointA) at (\pgfextra{\pgfmathparse{-\ValueOf{PIE}}},-1); 

    \draw (PointA) circle (1);

\end{tikzpicture}
\end{document}

Personally I don't see your problem with having macro constants like \PI.

Caramdir
  • 89,023
  • 26
  • 255
  • 291
Martin Scharrer
  • 262,582
2

You could use Luatex and constants of Lua.

\directlua{tex.print(math.pi)}

inserts 3.1415926535898 in your document.

1

It's not exactly your code but a solution is : Better it's to use xstring package

\documentclass{article}
\usepackage{tikz}

\newcommand{\PI}{PI} 
\newcommand{\EULER}{EULER} 
\newcommand{\ValueOf}[1]{  
\ifx \PI#1 3.14159 %
  \else 
    \ifx \EULER#1 2.71828 % 
      \else 1 %
    \fi
\fi}  

\begin{document}

    Value of -PI is -\ValueOf{\PI}.\par
    Value of E is \ValueOf{\EULER}. \par 
\vspace*{1cm}
    \begin{tikzpicture}
    \coordinate (PointA) at (\ValueOf{\PI} ,-1); 
    \draw (PointA) circle (\ValueOf{\PI});
    \draw  circle (\ValueOf{\EULER});  
\end{tikzpicture}
\end{document}
Alain Matthes
  • 95,075