# Using predefined constants with TikZ

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)

-
pgfplot is out of the problem –  Alain Matthes Apr 6 '11 at 8:40
Also have a look at Why is the ifthen package obsolete ? –  Martin Scharrer Apr 6 '11 at 8:45
Side remark: Both constants you define are built into pgfmath. Simply writing \coordinate (PointA) at (2*e-4,2/3*pi); works as expected. –  Caramdir Apr 6 '11 at 16:18
Yeah I saw the why ifthen is obsolete previously but obviously did not understand it. Also, I plan to use this to be able to return a lot more constants and need it outside of pgf as well. I will try to remember to not dumb down the problem too much from now on -- The PI and E were just examples to illustrate the problem. Thanks for all the help. –  Peter Grill Apr 6 '11 at 20:19

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}{%
}{%
\@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.

-
I added a missing “not” to your second sentence (I think it makes more sense now). –  Caramdir Apr 6 '11 at 16:20
Thanks @Caramdir: It got deleted by accident in an early edit. –  Martin Scharrer Apr 6 '11 at 16:21

You could use Luatex and constants of Lua.

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


inserts 3.1415926535898 in your document.

-
Nice idea, if you use LuaTeX. I personally would go for a simple \def\PI{3.1415926535898}. –  Martin Scharrer Apr 6 '11 at 16:23

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}

-
This seems to be working great. Thanks. –  Peter Grill Apr 6 '11 at 23:41