Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Possible Duplicate:
Use first row of a table as legend entry in pgfplot graph ?

I am creating a number of very similar plots using pgfplots. The raw data for these is provided in a single .csv file, so something like

x,a,b,c,d,e
0,0.04,0.03,0.04,0.03,0.04
1,0.06,0.05,0.06,0.05,0.06
2,0.06,0.07,0.06,0.07,0.06
3,0.09,0.08,0.09,0.08,0.09
4,0.13,0.12,0.13,0.12,0.13
5,0.13,0.12,0.13,0.12,0.13

(In the real case, there are a lot more rows and columns.) Each plot is separate (i.e. not on the same axes), but they are produced 'semi-automatically' using a suitable macro containing a plot environment of the form

\newcommand{\buildplot}[1]%
  {%
    \begin{tikzpicture}
      \begin{axis}
        [
          axis x line = bottom,
          axis y line = left,
        ]
        \addplot [mark = none]
          table [col sep = comma, y index = #1] 
          {example.csv};
       \end{axis}
     \end{tikzpicture}
  }

The plots need to be differentiated, and the easiest way is to use the column header (in the example data a, b, etc.) as the title key. However, I cannot find a way to recover the header within the plotting environment to achieve this. As the information is already in the .csv, I would rather avoid retyping it in the document if at all possible.

share|improve this question

marked as duplicate by Joseph Wright Jan 4 '12 at 13:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Note that in the real case there is quite a bit more to the \buildplot macro, as there are various other settings which have to be adjusted on a plot-by-plot basis, plus some general set up to make things look good. –  Joseph Wright Jan 4 '12 at 8:58
    
Not really an answer, but the datatool package (Nicola Talbot) uses provides an API for creating tabulars from CSV input. It seems to be capable of extracting the information in the first line/as well as ignoring it. –  Marc van Dongen Jan 4 '12 at 9:12
    
Perhaps the accepted answer to this question could be adapted. I recently did something similar to the requirements of this question using it without changes, saving the extracted text in a macro. –  qubyte Jan 4 '12 at 9:31
    
@MarcvanDongen The {input} tag seems to adhere to the answer you provided, not to the question, and so I removed it. See meta.tex.stackexchange.com/questions/1931/…. –  lockstep Jan 4 '12 at 10:51
    
You can use the approach described in tex.stackexchange.com/questions/23993/… to extract entries from a table/file. –  Jake Jan 4 '12 at 13:06
show 6 more comments

1 Answer

The following is a partial solution that shows how you may approach it. The disadvantage is that it uses write18. The example uses head -1 to extract the first line from the .csv file and then another program to process the output, save it to a file and include the file. For the purpose of the example I use sed to change the delimiters. It's up to you to change this to whatever you want.

\immediate\write18{head -1 data.csv | sed -e 's/,/ delim /g' > file.tex}
\input{file.tex}
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.