11

Let's say I have a bunch of data, like so:

1,15,53
5,74,12
74,23,66

It doesn't really matter how I store the data. I could put it in text form within my .tex file or I could store it in an external .csv file.

Now, I would like to fetch single data points within my tikz code. So, for example, let's say I wanted to use the data point at row 2, column 0 (which, in this case, is the number 74). My code would then look something like this:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}    
\node[draw] at (0,\dataPointAtRow2Column0) {test};
\end{tikzpicture}

Is there something I could write instead of \dataPointAtRow2Column0 to make tikz draw a node at (0,74)?

Note that I'm not looking for a solution like this. I do not want to rewrite my code using \pgfplots.

2 Answers 2

12

One possible way is to use pgfplotstable macro \pgfplotstablegetelem

\documentclass[border=2mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotstableread[col sep=comma]{
1,15,53
5,74,12
74,23,66
}\mydata

\begin{document}
\begin{tikzpicture}
\node (o) at (0,0) {O};
\pgfplotstablegetelem{2}{0}\of\mydata
\node[draw] at (0,\pgfplotsretval pt) {test};
\end{tikzpicture}
\end{document}

enter image description here

4

As long as the macro only uses expansion it can be used as the coordinate:

\documentclass{article}
\usepackage{tikz}


\def\data{
{1,15,53}
{5,74,12}
{74,23,66}
}

\makeatletter
\def\dataPointAt#1#2{\expandafter\xdpa\data{}{}\relax{#1}{#2}}

\def\xdpa#1#2\relax#3#4{%
\ifnum#3>\z@
\xdpa#2\relax{\numexpr#3-1\relax}{#4}%
\else
\xxpda#1,\relax{#4}%
\fi}

\def\xxpda#1,#2\relax#3{%
\ifnum#3>\z@
\xxdpa#2\relax{\numexpr#3-1\relax}%
\else
#1%
\fi}

\makeatother

\typeout{===\dataPointAt{2}{0}===}

\makeatother
\begin{document}
\begin{tikzpicture}    
\node[draw] at (0, \dataPointAt{2}{0}) {test};
\end{tikzpicture}

\end{document}

You must log in to answer this question.

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