3

I want to code a \foreach for each element in the column Label of my.dat. Something like:

\foreach \L[count=\i] in column {Label} from {my.dat}

What is the right syntax? I can't find anywhere.

\begin{filecontents}{my.dat}
Label       Value
AS  1
JK  .9
OW  1.5
CU  3.1
TR  .5
\end{filecontents}
1
4

You could try \csvloop from the package csvsimple:

\documentclass{article}
\usepackage{filecontents}
\usepackage{csvsimple}
\begin{filecontents*}{my.dat}
Label,Value
AS,1
JK,.9
OW,1.5
CU,3.1
TR,.5
\end{filecontents*}
\begin{document}
\csvloop{file={my.dat}, head to column names, command={\csvcoli\ is row number \thecsvrow\\}}
\end{document}

See the csvsimple manual for more information.

3

pgfplotstable provides functions to read data and access to it in loops, and can be used inside TikZ pictures. For example:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{filecontents}
\usepackage{pgfplotstable}

\begin{filecontents}{my.dat}
Label       Value
AS  1
JK  .9
OW  1.5
CU  3.1
TR  .5
\end{filecontents}

\begin{document}
% First, read the table into a data structure
\pgfplotstableread{my.dat}{\MyData}

\begin{tikzpicture}
\node[draw,circle, fill=red] (Origin) at (0,0) {};
% Following loop is equivalent to your proposed
% \foreach \L[count=\i] in column {Label} from {my.dat}
\pgfplotstableforeachcolumnelement{Label}\of\MyData\as\L{%
  \edef\i{\pgfplotstablerow}
  \draw (Origin) -- (60*\i:2) node[circle,draw,fill=orange!20] {\L};
  }
\end{tikzpicture}
\end{document}

Result:

Result

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.