2

What I want is

wanted

which could be drawn by the following code.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[xmin=0, xmax=4, ymin=-1, ymax=1]
            \pgfmathsetmacro{\myresultA}{1}
            \fill (\myresultA,0) circle [radius=5pt];
            \pgfmathsetmacro{\myresultB}{2}
            \fill (\myresultB,0) circle [radius=5pt];
            \pgfmathsetmacro{\myresultC}{3}
            \fill (\myresultC,0) circle [radius=5pt];
            % Many more \fill and \draw commands which require unique coordinate calculation here
        \end{axis}
    \end{tikzpicture}
\end{document}

I would like to avoid using separate variable names for each pgfmath calculation, since I have A LOT to compute. Unfortunately, the code

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[xmin=0, xmax=4, ymin=-1, ymax=1]
            \pgfmathsetmacro{\myresult}{1}
            \fill (\myresult,0) circle [radius=5pt];
            \pgfmathsetmacro{\myresult}{2}
            \fill (\myresult,0) circle [radius=5pt];
            \pgfmathsetmacro{\myresult}{3}
            \fill (\myresult,0) circle [radius=5pt];
            % Many more \fill and \draw commands which require unique coordinate calculation here
        \end{axis}
    \end{tikzpicture}
\end{document}

just gives me

failed

How to do a lot of calculation without separate result variables? Even better would be inline calculation without using any result variable? Is there a clever way? Of course this is just a small example of what I really like to draw. So my question is more of conceptional nature.

12
  • With the information you gave so far there are plenty of ways to achieve the desired result. So can you be a bit more specific what you really want to calculate and how it should look like? Maybe by adding a sketch with some annotations on it or then as explaining text. Oct 17, 2016 at 20:20
  • use another addplot with only marks and give coordinates
    – percusse
    Oct 17, 2016 at 23:12
  • @StefanPinnow I will draw something manually which I don't know precisely, yet. What is clear is that I need calculations for almost every coordinate. Please view it more like a conceptional question.
    – Rob
    Oct 18, 2016 at 5:57
  • @percusse What do you mean? I would be happy about another answer!
    – Rob
    Oct 18, 2016 at 6:00
  • You may use your formulas (here 1, 2 and 3) directly in your coordinates. Ex: \fill ({1+1+1},0) circle [radius=5p]; Oct 18, 2016 at 6:10

1 Answer 1

3

I'm not a pgfplots expert, but it looks like pgfplots delay the \fill command. So it first run the three \pgfmathsetmacro and then the three \fill commands.

To overcome this you can put your computations inside \pgfextra like this :

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[xmin=0, xmax=4, ymin=-1, ymax=1]
            \fill \pgfextra{\pgfmathsetmacro{\myresult}{1}}
              (\myresult,0) circle [radius=5pt];
            \fill \pgfextra{\pgfmathsetmacro{\myresult}{2}}
              (\myresult,0) circle [radius=5pt];
            \fill \pgfextra{\pgfmathsetmacro{\myresult}{3}}
              (\myresult,0) circle [radius=5pt];
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

2
  • Nice! It's not only \fill, it's also \draw and everything else which requires coordinates. Quote from the manual regarding \pgfextra: "Note that this operation should only be used by real experts and should only be used deep inside clever macros, not on normal paths." So I have some concerns about using it. However it works perfectly! Please allow me to wait a little longer for other answers before I accept yours.
    – Rob
    Oct 18, 2016 at 5:53
  • Here, the let operator (from calc library) may provide the same possibilities... Oct 18, 2016 at 6:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .