8

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'?

2 Answers 2

6

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}
3
  • 1
    No wonder I couldn't find that:-) Mar 19, 2013 at 9:27
  • Isn't adding just 1 to the @cell content more practical with every head row?
    – percusse
    Mar 19, 2013 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, 2013 at 12:10
5

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

2
  • 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? Mar 19, 2013 at 6:19
  • @FedericoMagallanez see Jake's answer. Mar 19, 2013 at 12:19

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.