我似乎无法在 TikZ 中定义常量。我查看了这个相关问题问题,而且对我来说也不起作用。此示例有效,但如果交换定义则无效\coordinate
:
\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}
如果有相关的话,我拥有的软件包版本是:
pgfplots 2010/07/14 版本 1.4
pgf 2008/01/15 v2.00 (rcs-修订版 1.12)
答案1
给出的坐标表达式必须扩展为数字。似乎没有这样做。一种解决方案是以可扩展的方式\ifthenelse
定义:\ValueOf
\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}
就我个人而言,我并不认为拥有像这样的宏常量存在什么问题\PI
。
答案2
答案3
这不完全是你的代码,但解决方案是:最好使用 xstring 包
\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}