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.

I've had a look at the attached answer Automatic table row numbers but I'm not sure how to apply it to pgfplotstable. I'd like to get automatic rownumbers on my table so I can apply formatting by row index. (for subtotals and the like).

I have over 100 rows so its hard to scan visually and know the row number, and the columns aren't named so something like

\pgfplotstableset{create on use/new/.style={create col/set={\thisrow{columns={0}}}}}

followed by a

\pgfplotstabletypeset[columns={new,0,1,2,3,4,5,6,7}]\blah

doesnt work and gives me an error. Is there an easy way to get automatic row numbers in a pgfplotstable?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

You could use the key every first column to display the counter value at the start of column 1:

enter image description here

Notes:

  • As you can see, I don't have much experience with pgfplotstable, hence the bad formatting. Will come back and fix the columns later.

Code:

\documentclass{article}
\usepackage{,pgfplotstable}

\usepackage{filecontents}
  \begin{filecontents*}{sample.csv}
    Column A, Column B, Column C
       99, 98, 96
       88, 87, 84
  \end{filecontents*}

\pgfplotstableread[col sep=comma]{sample.csv}\MySampleData

\newcounter{MyCount}
\begin{document}

\pgfplotstabletypeset[
    every first column/.style={column type/.add={>{\makebox[3em][l]{\arabic{MyCount}\stepcounter{MyCount}~}}}{} }
]\MySampleData

\end{document}
share|improve this answer
    
thanks @PeterGrill. I had half hoped there would be something that used some of the pgfplotstable native functionality. Add a column before and fill using \pgfplotstablerows or something. But this does the job just as well. –  Tahnoon Pasha Oct 5 '12 at 0:35
    
@TahnoonPasha: There might well be. As I mentioned I have just started learning pgfplotstable, so perhaps a better answer will come along. If I come across it I will post it here. –  Peter Grill Oct 5 '12 at 0:37
add comment

You can use the key create col/expr accum:

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotstableset{create on use/new/.style
  = {create col/expr accum={\pgfmathaccuma+1}{0}}}
\begin{document}
\pgfplotstableread{
  3 7 2 7 8 1 6 3
  7 3 0 2 3 7 2 5
  1 6 3 7 4 9 7 2
  4 8 2 9 6 3 5 2
}\mytable
\pgfplotstabletypeset[columns={new,0,1,2,3,4,5,6,7}]\mytable
\end{document}

share|improve this answer
2  
Nice! You could simplify this a little bit by using the fact that the current row number is available in the \pgfplotstablerow macro, so you could say: create col/expr={\pgfplotstablerow+1} –  Jake Jun 13 '13 at 18:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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