[Tex/LaTex] csvreader and special characters in the csv header

csvsimple

I have a question quite similar to this question. But I do have the underscore character in the heading which causes issure. One column has the name q_u. When I try:

\csvreader[
        respect all,
        head to column names
    ]{data.csv}{}
        {\\\hline\objId & \q_u}

It does not work because \q_u is not legal.

An example for data.csv:

objId,q_u
587722982832013381,-0.1237466
587724241230495806,0.04632737

What can I do about this without having to change the csv file?

Best Answer

You can use the second mandatory argument for setting aliases for the column names:

\begin{filecontents*}{\jobname.csv}
objId,q_u
587722982832013381,-0.1237466
587724241230495806,0.04632737
\end{filecontents*}

\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}

\begin{document}

\begin{tabular}{>{$}r<{$} >{$}r<{$}}
\csvreader[
  respect all,
  head to column names,
]{\jobname.csv}{q_u=\qu}{\\\hline\objId & \qu }
\\\hline
\end{tabular}

\end{document}

enter image description here