4

I am making a LaTeX table using a CSV file and the datatools package.

Here is a minimal working example:

\documentclass{minimal}
\usepackage{datatool}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
,$x$,$p_x$,
$x$,$0$,$i \hbar$
\end{filecontents*}

\begin{document}

\DTLloaddb{mytable}{data.csv}
\DTLdisplaydb{mytable}

\end{document}

This will compile cleanly. However, if I change the first row (which will become the header) to include a macro (e.g. ,$x$,$\hbar$,) I get a whole bunch of errors on the \DTLloaddb{mytable}{data.csv} line.

! Missing \endcsname inserted.
<to be read again>
\mathchar
l.12 \DTLloaddb{mytable}{data.csv}
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.
....(and so forth)...

Things I have tried that did not work:

  1. Quoting ("\hbar")
  2. Ensuremath (\ensuremath{\hbar})
  3. Using tabs as delimiters instead of commas.
  4. Unrolling the csv file into equivalent datatool commands.

These are the equivalent commands:

\DTLnewdb{mydata}
\DTLnewrow{mydata}%
\DTLnewdbentry{mydata}{}{$x$}%
\DTLnewdbentry{mydata}{$x$}{$0$}%
\DTLnewdbentry{mydata}{$p_x$}{$i \hbar$}%
\DTLdisplaydb{mydata}

They give the same errors as the csv file.

Manually specifying the headers, however, works fine:

\documentclass{minimal}
\usepackage{datatool}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
$x$,$0$,$i \hbar$
\end{filecontents*}

\begin{document}

\DTLloaddb[noheader,headers={,$x$,$\hbar$}]{mytable}{data.csv}

\DTLdisplaydb{mytable}

\end{document}

I would like to avoid manually specifying the headers, however, if possible.

  • 1
    Welcome to TeX.SX! The culprit is \hbar, which is very fragile. Unfortunately even \protect does not help. :-( – egreg Jul 22 '13 at 21:29
1

The problem is that you're using something unexpandable as an identifying label. The same problem would arise if you tried doing something like \label{\hbar}. If the headers contain commands or special characters you need to explicitly set the keys to simple labels. For example:

\documentclass{article}
\usepackage{datatool}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
$x$,$0$,$i \hbar$
,$x$,$p_x$
\end{filecontents*}

\begin{document}

\DTLloaddb[keys={column1,column2,column3}]{mytable}{data.csv}
\DTLdisplaydb{mytable}

\end{document}

(You'll probably want something more descriptive than column1 etc.)

  • Thanks! I don't suppose there's any way to automatically adjust the number of keys to match the number of columns in the csv file, is there? – Nathaniel M. Beaver Jul 23 '13 at 11:25
  • @bariumbitmap At the moment there isn't an automatic way of doing this, but I might see if I can add an option to do that. – Nicola Talbot Jul 23 '13 at 11:51

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.