# Unable to typeset LaTeX maths characters in pgfplotstable table header

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=\\,
column type={c}
}

\begin{document}

\pgfplotstabletypeset{
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=\\,
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}

• – erik Apr 8 '15 at 19:37
• If you have access to your data then use a non-TeX header and use column name for TeX output of that column header appearance. Headers are used for naming the columns internally. – percusse Apr 8 '15 at 19:56
• @percusse Thanks, I would like to leave my formatting routine as generic as possible, and flexible enough to accommodate adding columns to the input data if necessary. This means I don't want to manually format individual columns by name. – mlh3789 Apr 9 '15 at 13:45
• See updated question with possible alternative answers. – mlh3789 Apr 9 '15 at 14:23

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{
every first row/.style={after row=\hline}, % move h rule to first row
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 \\
}

• I suggest using after row=\midrule (from the booktabs package) for better spacing around the horizontal rule – Raven Jan 13 '18 at 12:26

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{}
\pgfplotstableset{columns/#1/.style={column name=#2}}
}
}
}
\edef\pgfplotstable@loc@TMPb{#1}
\ifx\pgfplotstable@loc@TMPb\pgfutil@empty
\edef\pgfplotstable@loc@TMPa{#2}
\fi
}
\edef\pgfplotstable@loc@TMPa{#1}%
\ifx\pgfplotstable@loc@TMPa\pgfutil@empty
\fi
}
\let\pgfplotstabletypeset@opt@@old\pgfplotstabletypeset@opt@@
\def\pgfplotstabletypeset@opt@@{\pgfplotstabletypeset@fancy@column@name\pgfplotstabletypeset@opt@@old}

[X]$\xi$ & [Y]$\eta$ & [Z]$\zeta$ \\