7

I want to put the content of a csv file in a tabular environment. However, this includes text with underscores "_". Because this triggers the math environment, it gives the error: Missing $ inserted.

Using escape underscore to show the actual text does not work, since then the csv reader does not work:

\documentclass{article}
\usepackage{csvsimple}
\usepackage{url}
\DeclareUrlCommand\UScore{\urlstyle{rm}}
% Make csv in question
\begin{filecontents*}{scientists3.csv}
name
Albert_Einstein,
Marie_Curie,
Thomas_Edison
\end{filecontents*}

\begin{document}
    \csvreader[tabular=|l|,
  table head=\textbf{ Reference} \\\hline,
  late after line = \\\hline]%
    {scientists3.csv}{name=\name}%
    {\UScore{\name}}%
\end{document} 

The result is obviously: "\name" instead of the content of the .csv file.

2 Answers2

6

You have to expand twice \name:

\begin{filecontents*}{\jobname.csv}
name
Albert_Einstein
Marie_Curie
Thomas_Edison
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple}
\usepackage{url}

\DeclareUrlCommand\UScore{\urlstyle{rm}}
\newcommand{\expUScore}{%
  \expandafter\expandafter\expandafter
  \UScore
  \expandafter\expandafter\expandafter
}

\begin{document}
\csvreader[
  tabular=|l|,
  table head=\hline\textbf{Reference} \\\hline,
  late after line = \\\hline
]{\jobname.csv}{name=\name}
 {\expUScore{\name}}

\end{document}

enter image description here

I used \jobname to avoid clobbering my files.

egreg
  • 1,121,712
4

The simplest solution is to use respect underscore=true option:

\csvreader[respect underscore=true,...]

See the csvsimple's documentation:

If this key is set, every underscore sign "_" inside the CSV content is a normal character.

uzsolt
  • 1,431