是否可以在单个宏中使用多个\pgfmathparse
?pgfmathresult
在这个 MWE 中,我尝试使用两个。但效果并不好。上面的分数应该是 10/3,而不是 0.3/3。
我错过了什么?
\documentclass{article}
\usepackage{tikz}
\newcommand{\NLdot}[5] %{xscale}{yscale}{xmax}{denom}{xval of dot}
{\tikz[xscale=#1,yscale=#2]
{
\draw (0,0)--(#3,0);
\pgfmathparse{#3*#4}
\foreach \x in {0,...,\pgfmathresult}
\draw (\x/#4,-0.2)--(\x/#4,0.2);
\foreach \x in {0,...,#3}
\node[below] at (\x,-0.3) {\x};
\fill[gray,opacity=0.5] (#5,0) circle [x radius = 0.2/#1, y radius = 0.2/#2];
% This didn't work... would like a fration label right above the dot. Why did I get 0.3 instead of 10 for the numerator?
\pgfmathparse {#4*#5}
\node[above] at (#5,0.3) {$\frac{\pgfmathresult}{#4}$}
}
}
\begin{document}
\NLdot{2.5}{1.25}{5}{3}{10/3}
\end{document}
答案1
通常会\pgfmathresult
被其他东西设置和使用,在本例中可能是\node
(y 坐标为 0.3,因此似乎是可能的候选者)。将 移动\pgfmathparse
到 之前即可\pgfmathresult
解决此问题。
另一个选择是使用将结果保存到宏中\pgfmathsetmacro{\<macro name>}{<calculation>}
。
最后一点:如果您知道案例中的分子将始终为整数(如本例中的 3*10/3),那么您可以使用例如函数int
来避免10.0
输出。所有这些都在下面的示例中进行了演示。
\documentclass{article}
\usepackage{tikz}
\newcommand{\NLdotA}[5] %{xscale}{yscale}{xmax}{denom}{xval of dot}
{\tikz[xscale=#1,yscale=#2]
{
\draw (0,0)--(#3,0);
\pgfmathsetmacro{\EndOfForLoop}{#3*#4}
\foreach \x in {0,...,\EndOfForLoop}
\draw (\x/#4,-0.2)--(\x/#4,0.2);
\foreach \x in {0,...,#3}
\node[below] at (\x,-0.3) {\x};
\fill[gray,opacity=0.5] (#5,0) circle [x radius = 0.2/#1, y radius = 0.2/#2];
\pgfmathsetmacro{\TheNumerator}{int(#4*#5)}
\node[above] at (#5,0.3) {$\frac{\TheNumerator}{#4}$}
}
}
\newcommand{\NLdotB}[5] %{xscale}{yscale}{xmax}{denom}{xval of dot}
{\tikz[xscale=#1,yscale=#2]
{
\draw (0,0)--(#3,0);
\pgfmathparse{#3*#4}
\foreach \x in {0,...,\pgfmathresult}
\draw (\x/#4,-0.2)--(\x/#4,0.2);
\foreach \x in {0,...,#3}
\node[below] at (\x,-0.3) {\x};
\fill[gray,opacity=0.5] (#5,0) circle [x radius = 0.2/#1, y radius = 0.2/#2];
\node[above] at (#5,0.3) {$\frac{\pgfmathparse{#4*#5}\pgfmathresult}{#4}$}
}
\begin{document}
\noindent\NLdotA{2}{1.25}{5}{3}{10/3}
\noindent\NLdotB{2}{1.25}{5}{3}{10/3}
\end{document}