4

I'm using csvsimple to import some columns from a csv file into a tabular environment. The CSV file contains a column where all the values are dollar amounts, so just including it directly gives errors like the following:

  • Extra }, or forgotten $. ...tNum & \Reference & \UnitPriceAtOneKUnits}
  • Missing $ inserted. ...tNum & \Reference & \UnitPriceAtOneKUnits}
  • Missing } inserted. ...tNum & \Reference & \UnitPriceAtOneKUnits}

After reading through the manual for csvsimple, I found there are respect commands that should be able to be added. However it doesn't seem to respect either the respect dollar or respect all options. I've also tried respect dollar=true, respect all=true, /csv/respect dollar=true, and /csv/respect all=true. I don't mind if it respects all since I'm not planning on putting LaTeX in any of the fields - I just want it to display the data for the selected columns.

Here is the code that is currently not working:

\documentclass[12pt,letterpaper,landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in,bottom=1in,top=1in]{geometry}
\usepackage{csvsimple}
\begin{document}
    \begin{table}[h]
    \begin{tabular}{c|l|l|l|r}%
        \bfseries Qty & \bfseries Part Description & \bfseries Manufacturer Part \# & \bfseries Reference & \bfseries Unit Price At 1K Units
        \csvreader[respect dollar,head to column names,separator=pipe]{BOM.csv}{}%
        {\\\hline\Qty & \PartDescription & \ManufacturerPartNum & \Reference & \UnitPriceAtOneKUnits}%
    \end{tabular}
\end{table}
\end{document}

Here is some sample data for the csv file (with only revelant columns):

Qty|PartDescription|ManufacturerPartNum|Reference|UnitPriceAtOneKUnits
5|Part 1|Option|Z1,Z2,Z3| $0.003
1|Part 2|Option|D1,D2,D3,D4| $0.004 
2|Part 3|Option|U1,U2,U3,U4| $0.008

Any help would be appreciated.

Software Versions:

  • TeXStudio 2.12.6
  • MiKTeX 2.9.6350
  • csvsimple 1.21
6
  • Welcome to TeX-SE! Try replacing all $ in your cvs file by \$, i.e. add a backslash. A $ is a special character in LaTeX that starts/ends math mode.
    – user121799
    Commented Apr 23, 2019 at 18:21
  • I get that that is a possible solution, however isn't respect dollar supposed to make it so you don't have to do that?
    – techdude
    Commented Apr 23, 2019 at 18:24
  • Maybe. To check it would be great if you could post a complete minimal working example that starts with \documentclass and ends with \end{document}.
    – user121799
    Commented Apr 23, 2019 at 18:29
  • Ok, I updated it.
    – techdude
    Commented Apr 23, 2019 at 18:45
  • 2
    Seems to be a bug for me. Can you please write an bug report for the maintainer?
    – Mensch
    Commented Apr 23, 2019 at 20:31

2 Answers 2

4

It's certainly a bug, a workaround could be to deactivate the $ sign before the table and reactivate it after.

\documentclass[12pt,letterpaper,landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in,bottom=1in,top=1in]{geometry}
\usepackage{csvsimple}

% this code is only to create BOM.csv, you don't need it:
\usepackage{filecontents}
\begin{filecontents*}{BOM.csv}
Qty|PartDescription|ManufacturerPartNum|Reference|UnitPriceAtOneKUnits
5|Part 1|Option|Z1,Z2,Z3| $0.003
1|Part 2|Option|D1,D2,D3,D4| $0.004
2|Part 3|Option|U1,U2,U3,U4| $0.008
\end{filecontents*}


\begin{document}
Before the table \$ works as usual $a+b$
\catcode`\$=12% deactivate $ sign
\begin{table}[h]
    \begin{tabular}{c|l|l|l|r}%
        \bfseries Qty & \bfseries Part Description & \bfseries Manufacturer Part \# & \bfseries Reference & \bfseries Unit Price At 1K Units
        \csvreader[
        head to column names,separator=pipe]{BOM.csv}{}%
        {\\\hline\Qty & \PartDescription & \ManufacturerPartNum & \Reference & \UnitPriceAtOneKUnits}%
    \end{tabular}
\end{table}
\catcode`\$=3% reactivate $ sign

After the table \$ works as usual $a+b$
\end{document}

enter image description here

1
  • Oops, yeah I forgot to swap those to pipes when I updated the question. That deactivating and reactivating seems like just the trick!
    – techdude
    Commented Apr 24, 2019 at 16:07
3

Actually, respect dollar also sets the \catcode of $ to 12 at the begin of \csvreader. But this setting gets lost from one cell of the table to the next, because cell contents are inside a TeX group.

Therefore, $ has to be deactivated before the table starts. CarLaTeX showed one way to do it.

The other way is to use the built-in tabular functions from csvsimple:

\documentclass[12pt,letterpaper,landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in,bottom=1in,top=1in]{geometry}
\usepackage{csvsimple}
\begin{document}
  \begin{table}[h]
    \csvreader[respect dollar,head to column names,separator=pipe,
      tabular={c|l|l|l|r},
      table head={\bfseries Qty & \bfseries Part Description & \bfseries Manufacturer Part \# & \bfseries Reference & \bfseries Unit Price At 1K Units\\\hline},
      late after line=\\\hline,
      late after last line=,
    ]{BOM.csv}{}%
    {\Qty & \PartDescription & \ManufacturerPartNum & \Reference & \UnitPriceAtOneKUnits}%
  \end{table}
\end{document}

Here, respect dollar works for the whole table and not for the first line only.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .