6

I'm using package csvsimple to read .csv files. The problem is, in some of the cells I have percentages, like:

a,b
1,10%
3,16%

If I leave the %s, everything after the first % is omitted, that is, it's behaving like a comment. If I escape % with \% I get compilation errors.

Example:

\documentclass{minimal}
\usepackage{csvsimple}
\begin{document}
\csvautotabular{tab.csv}
\end{document}

where tab.csv:

a,b
1,2%
2,5%

The same problem happens with datatool package.

2
  • Welcome to TeX.SX! Could you please add a small example?
    – egreg
    Commented Oct 3, 2012 at 19:37
  • Without an example it is untested but probably {\catcode`\%=12 \csvreader{...}} Commented Oct 3, 2012 at 19:38

1 Answer 1

4

You can add the possibility to evaluate some keys, which is not allowed with the default \csvautotabular command.

\documentclass{article}
\usepackage{csvsimple}

\newcommand{\csvloopx}[2][]{\csvloop{#1,#2}}
\newcommand{\csvautotabularx}[2][]{\csvloopx[#1]{autotabular={#2}}}
\newcommand{\respectpercent}{\catcode`\%=12\relax}

\begin{document}
\csvautotabularx[before table=\respectpercent]{tab.csv}
\end{document}

enter image description here

You get a similar effect with datatool by using the \DTLloadrawdb command:

\documentclass{article}
\usepackage{datatool}
\begin{document}
\DTLloadrawdb{tab}{tab.csv}
\DTLdisplaydb{tab}
\end{document}
5
  • isnt there a simpler way with either datatool or csvsimple? Commented Oct 3, 2012 at 20:47
  • @FernandoAntonioNogueira I think that this is the simplest way for csvsimple. Not sure about datatool, but \DTLloadrawdb seems the way to go. May you add also an example of what you're trying with datatool?
    – egreg
    Commented Oct 3, 2012 at 20:55
  • \documentclass{minimal} \usepackage{datatool} \begin{document} \DTLloaddb{ash}{ash.csv} \DTLdisplaydb{ash} \end{document} Commented Oct 3, 2012 at 21:02
  • @Fernando: IMHO the simplest and easiest way is to process the .csv beforehand and escape all occurrences of % to \%. Another approach is to remove the percent sign from the .csv and add it later while in the .tex document. Commented Oct 3, 2012 at 22:16
  • if you mean putting a \ in every occurrence of the % in the csv file, I have done it and it wont work. Commented Oct 4, 2012 at 0:01

You must log in to answer this question.

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