6

I am typsetting a simple table with pgfplotstable and attempting to include units in the headers. I've noticed that certain math macros e.g. \circ or \gamma (2 random test cases) fail to typeset. When these are included in a subsequent non-header row, there are no issues! The failure mode is a Missing \endcsname inserted. There is no problem including math in a straight Tabular environment, whether in the header or not. Any thoughts on how to include math in pgfplotstable header row?

MWE: (works)

\documentclass{article}
\usepackage{pgfplotstable}
%set global options for pgfplotstables
\pgfplotstableset{
string type,col sep=&,row sep=\\,
every head row/.style={after row=\hline},
column type={c}
}

\begin{document}

\pgfplotstabletypeset{
header 1 & header 2 & header 3 \\
col1 & NECL (ppm-m-$^{\circ}$ C) & 10\\
col1 & $\frac{\gamma}{1-\gamma}$ & col3 \\
}

\end{document}

MWE: (fails)

\documentclass{article}
\usepackage{pgfplotstable}

%set global options for pgfplotstables
\pgfplotstableset{
string type,col sep=&,row sep=\\,
every head row/.style={after row=\hline},
column type={c}
}

\begin{document}

\pgfplotstabletypeset{
header 1 & header 2 $\gamma$ & header 3 \\
col1 & NECL (ppm-m-$^{\circ}$ C) & 10\\
col1 & $\frac{\gamma}{1-\gamma}$ & col3 \\
}

\end{document}
mlh3789
  • 523
  • 5
  • 12

2 Answers2

3

Going with simplicity, and the most common use case for the win I'm copying my edits here as the answer.

According to How can LaTeX code in a data file be read by pgfplotstable? it is impossible to insert expandable material in headers to be formatted by pgfplotstable. Therefore, as in percusse comment use column name key to access and format any column headers for display purposes.

If one does not need access to the column header names, simply use header=false, and output empty row in the head row style. This causes typesetting to treat the top row as a generic row, allowing expandable material (ie symbols etc) in the header row. (Thanks to this answer for hint)

MWE: (works)

%set global options for pgfplotstables
\pgfplotstableset{
string type,header=false,col sep=&,row sep=\\,
every first row/.style={after row=\hline}, % move h rule to first row
every head row/.style={
   output empty row},                      % suppress printing head row
column type={p{.25\textwidth}}
}

\begin{document}

\pgfplotstabletypeset{
header 1 & header 2 $a^2\gamma$ & header 3 \\
col1 & NECL (ppm-m-$^{\circ}$ C) & 10\\
col1 & $\frac{\gamma}{1-\gamma}$ & col3 \\
}
mlh3789
  • 523
  • 5
  • 12
  • I suggest using after row=\midrule (from the booktabs package) for better spacing around the horizontal rule – Raven Jan 13 '18 at 12:26
1

I am not good at pure TeX. (especially the \expandafter stuff.) But this could be a start. (Recall the \section[short]{long long title} syntax.)

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}

\pgfplotstableset{string type,col sep=&,row sep=\\}
\makeatletter
\gdef\pgfplotstabletypeset@fancy@column@name{}
\def\pgfplotstableread@impl@collectcolnames@NEXT@help@@#1#2{
    \g@addto@macro\pgfplotstabletypeset@fancy@column@name{
        \pgfplotstableset{columns/#1/.style={column name=#2}}
    }
}
\def\pgfplotstableread@impl@collectcolnames@NEXT@help@#1[]{
    \expandafter\pgfplotstableread@impl@collectcolnames@NEXT@help@@\expandafter{\pgfplotstable@loc@TMPa}{#1}
}
\def\pgfplotstableread@impl@collectcolnames@NEXT@help#1[#2]{
    \edef\pgfplotstable@loc@TMPb{#1}
    \ifx\pgfplotstable@loc@TMPb\pgfutil@empty
        \edef\pgfplotstable@loc@TMPa{#2}
        \expandafter\pgfplotstableread@impl@collectcolnames@NEXT@help@
    \fi
}
\long\def\pgfplotstableread@impl@collectcolnames@NEXT#1{%
    \edef\pgfplotstable@loc@TMPa{#1}%
    \expandafter\pgfplotstableread@impl@collectcolnames@NEXT@help\pgfplotstable@loc@TMPa[]
    \ifx\pgfplotstable@loc@TMPa\pgfutil@empty
        \edef\pgfplotstable@loc@TMPa{\thepgfplotstableread@curcol}% 
        \pgfplotswarning{empty column name}{\pgfplotstableread@filename}{\pgfplotstable@loc@TMPa'}\pgfeov%
    \fi
    \expandafter\pgfplotstableread@impl@collectcolnames@NEXT@\expandafter{\pgfplotstable@loc@TMPa}%
}
\let\pgfplotstabletypeset@opt@@old\pgfplotstabletypeset@opt@@
\def\pgfplotstabletypeset@opt@@{\pgfplotstabletypeset@fancy@column@name\pgfplotstabletypeset@opt@@old}

\pgfplotstableread{
    [X]$\xi$ & [Y]$\eta$ & [Z]$\zeta$ \\
    0 & 1 & 2\\
}\loadedtable

\pgfplotstabletypeset\loadedtable
\hrule
\pgfplotstabletypeset[columns={X,Y,X,Z,X,Y,X}]\loadedtable

\end{document}
Symbol 1
  • 36,855