5

Is it possible to use more than one \pgfmathparse and pgfmathresult in a single macro?

In this MWE, I try to use two of each. It did not quite work. The fraction on top should say 10/3, not 0.3/3.

What am I missing?

\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}
5

The \pgfmathresult is often set and used by other stuff, in this case probably the \node (the y-coordinate is 0.3, so seems a likely candidate). Moving the \pgfmathparse to immediately before the \pgfmathresult solves this.

Another option is to save the result to a macro with \pgfmathsetmacro{\<macro name>}{<calculation>}.

One final point: If you know that the numerator in your cases will always become an integer (as here, with 3*10/3), then you can use for example the int function to avoid getting 10.0 as output. All of this is demonstrated in the example below.

enter image description here

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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