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.

This seems a very simple question, but I cannot find an answer.


I have a very simple CSV file and I want to make a LaTeX table from it.

tiny.csv

-1, 1, -1, 1
2, 4, 6, 8
3, 9, 27, 81

LaTeX file

\documentclass{standalone}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstabletypeset[col sep=comma]{tiny.csv}

\end{document}

Then I have a table as follows:

 0  1   2   3       <- head row
−1  1  −1   1
 2  4   6   8
 3  9  27  81

I wish the numbers in the head row to begin with 0, not 1. And I don't want to change the file tiny.csv as it will be used by other scripts for experiment. How can I change the head row to '1 2 3 4' from '0 1 2 3'?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

You can alter the internal PGFPlotstable macro that assigns the numeric column names. Here's a way that introduces a new key first numeric column name=<number> that allows to specify the starting number of the automatic column names. By default it's set to 0 so the normal behaviour isn't changed. If you set first numeric column name=1, the numbering will start with 1:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}

\makeatletter
\pgfplotstableset{first numeric column name/.initial=0}
\newcounter{numeric@colname}
\def\pgfplotstableread@create@column@names@with@numbers{%
    \pgfplotstableread@countreset\pgfplotstableread@curcol%
    \pgfutil@loop
    \ifnum\pgfplotstableread@curcol<\pgfplotstableread@numcols\relax
        \setcounter{numeric@colname}{\thepgfplotstableread@curcol}%
        \addtocounter{numeric@colname}{\pgfkeysvalueof{/pgfplots/table/first numeric column name}}%
        \edef\pgfplotstable@loc@TMPb{\thenumeric@colname}%
        \expandafter\pgfplotslistpushbackglobal\pgfplotstable@loc@TMPb\to\pgfplotstable@colnames@glob
        \pgfplotstableread@countadvance\pgfplotstableread@curcol
    \pgfutil@repeat
}
\makeatother

\begin{document}

\pgfplotstabletypeset[col sep=comma, first numeric column name=1]{
-1, 1, -1, 1
2, 4, 6, 8
3, 9, 27, 81
}

\end{document}
share|improve this answer
1  
No wonder I couldn't find that:-) –  David Carlisle Mar 19 '13 at 9:27
    
Isn't adding just 1 to the @cell content more practical with every head row? –  percusse Mar 19 '13 at 11:56
    
@percusse: Depends on what you want to do. That approach wouldn't change the actual column name, so you couldn't refer to the columns using the same numbers. Feel free to add an answer, though. –  Jake Mar 19 '13 at 12:10
add comment

One option would be to use column name inside the columns style:

\documentclass{standalone}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstabletypeset[col sep=comma,
columns/0/.style={column name=$1$},
columns/1/.style={column name=$2$},
columns/2/.style={column name=$3$},
columns/3/.style={column name=$4$},
]{tiny.cvs}

\end{document}

enter image description here

share|improve this answer
    
Hmm. OK, I can live with this option. Then, I want to generalize this approach so that I can use a for loop. (maybe \foreach?) Is that possible? –  Federico Magallanez Mar 19 '13 at 6:19
    
@FedericoMagallanez see Jake's answer. –  Gonzalo Medina Mar 19 '13 at 12:19
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.