3

I realize it must be something SO basic, but I'm having difficulty figuring out where the problem can possible be.

It seems to me all three of these \addplots should be the same. However, only the first one works as expected (which is a problem, because I'm needing it to perform the calculation.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture} \pgfmathparse{1+(6/100)} \begin{axis}[ xtick distance=5, minor x tick num=4 ] \addplot[smooth,green,domain=0:25] gnuplot [id=plot3] {1551.06x}; % works \addplot[smooth,green,domain=0:25] gnuplot [id=plot2] {155(1+(6/100))x)}; % makes dumb flat line \addplot[smooth,green,domain=0:25] gnuplot [id=plot4] {155*\pgfmathresultx}; % makes some sort of weird parobalic nonsense? \end{axis} \end{tikzpicture}

\end{document}

I can tell I'm not explaining my question very well, so please don't hesitate to direct me to reword or explain something.

jesse
  • 215
  • 1
    \pgfmathresult is heavily used for any math operation. So probably, it will be overwritten by the time you use it (to calculate internal domain limit, whatever). I think you are looking for declare function? – Rmano Apr 26 '23 at 08:00
  • 2
    Ah, wait, you are using gnuplot. You have a closing parenthesis more in the second function, and you need to write 1+(6.0/100) for gnuplot, otherwise the division is integer.You have to use valid gnuplot expression there, not lpgfmath ones. – Rmano Apr 26 '23 at 08:14

1 Answers1

4

There are several problems here.

  1. \pgfmathresult is used by almost all the operations in the pgfmath layer, so you can use it only immediately after the calculation, or it will be overwritten.

  2. When using gnuplot, the expression is passed to it with almost no processing, so you have to use gnuplot syntax and rules. 6/100 is an integer expression, so it will give zero. Use 6.0/100 or similar tricks to force floating point.

  3. You should manually expand the macros before passing them to gnuplot:

\documentclass[tikz, margin=2.72mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture} \newcommand{\myk}{(1+(6.0/100))} \begin{axis}[ xtick distance=5, minor x tick num=4 ] \addplot[green,domain=0:25] gnuplot [id=plot3] {1551.06x}; % works \addplot[smooth,red,dashed,domain=0:25] gnuplot [id=plot2] {155(1+(6./100))x}; % makes dumb flat line % this trick will "prepare" an expanded "addplot" command into \tmp % and then we'll call it. \edef\tmp{\noexpand\addplot[smooth,blue,densely dotted,domain=0:25] gnuplot [id=plot4] {155*(\mykx)}} \tmp; \end{axis} \end{tikzpicture}

\end{document}

enter image description here

Rmano
  • 40,848
  • 3
  • 64
  • 125