3

I have a command \mydatapoint which takes two arguments, x and y. It prints x and y in a specific format in my file.

\newcommand{\mydatapoint}[2][]{ \textbf{#1} #2}

Now I want to use these instances of x and y to print a kind of summary plot at the end.

For the plot I found this nice example of how to do it, but there, a datafile has to be read for the points.

The datafile simply needs this kind of format for the x and y data:

6 1
7 8
9 10

So now I want my command \mydatapoint to additionally print to an auxiliary file from which I can read the points back, but can't find anything online about how to write to auxiliary files. I haven't really done anything like that in Latex before, so I don't know where to start looking for information.

Any pointers in the right direction would be appreciated.

Also, is it possible to do this without the auxiliary file?

5

This will create a file called points.txt with a list of all your points

\documentclass[]{article}

\usepackage{etoolbox}
\usepackage{pgfplots}

\newwrite\points
\newcommand\pointlist{}
\newcommand{\mydatapoint}[2]{\listadd{\pointlist}{#1,#2}\textbf{#1} #2}
\def\parsepoint#1,#2{(#1,#2)}

\AtEndDocument{%
  \immediate\openout\points=points.txt
  \renewcommand*{\do}[1]{\immediate\write\points{#1}}
  \dolistloop{\pointlist}
  \immediate\closeout\points
}



\begin{document}

This is my first data point \mydatapoint{1}{2}, and the second one is
\mydatapoint{3}{4}. Yest another point is \mydatapoint{5}{6}

% points up to this point will be in the plot
\renewcommand*{\do}[1]{%
  \addplot[mark = square*, blue] coordinates {%
    \parsepoint#1
  };
}
\begin{tikzpicture}
\begin{axis}
  \dolistloop{\pointlist}
\end{axis}
\end{tikzpicture}


This point will be included in the file, but not in the plot:
\mydatapoint{7}{8}

\end{document}

enter image description here

  • Question: If the OP wants to put the scatterplot at the end of the same document, could one do without the file at all, and just collect the points in a list and then loop over them to do the scatterplot? – Paul Stanley Apr 23 '18 at 19:26
  • I think, having tested, that one can do without the auxiliary file if the plot comes at the end. pgfplots seems happy with a macro which accumulates coordinates. – Paul Stanley Apr 23 '18 at 19:50
  • @PaulStanley Thanks for the comment. I am afraid I misunderstood the OP's question, it is not the file what (s)he wants, it is a plot. I'm fixing it now – caverac Apr 23 '18 at 19:51
  • Well, I thought it was impossible without an auxiliary file, so I asked for an auxiliary file, but if it is possible without one, that would be even better – fifaltra Apr 23 '18 at 19:56
  • 1
    @caverac Well don't leave out your answer, which is good (and I upvoted it), but maybe add the additional alternative. – Paul Stanley Apr 23 '18 at 19:56

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.