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 Answers2

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}
Jake
  • 232,450
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

Gonzalo Medina
  • 505,128