1

Problem

On section 1.2 First Steps of the csvsimple manual [link: https://ctan.math.washington.edu/tex-archive/macros/latex/contrib/csvsimple/csvsimple.pdf], the instructions are to use the following, assuming the file already exists in the current directory:

csvautotabular{name-of-file.csv}

However I am unable to achieve this task. Below are the csv contents and my .tex file.


CSV content: 'gdp--gdp-per-grwth-data-truncated.csv'

country-name,country-code,indicator-name,2018,2019
Aruba,ABW,{GDP growth (annual %)},na,na
Afghanistan,AFG,{GDP growth (annual %)},1.189228129,3.911603419
Angola,AGO,{GDP growth (annual %)},-2.003629727,-0.624644318
Albania,ALB,{GDP growth (annual %)},4.071301295,2.240070268
Andorra,AND,{GDP growth (annual %)},1.58876549,1.849238257
Arab World,ARB,{GDP growth (annual %)},2.079469006,1.487633017
United Arab Emirates,ARE,{GDP growth (annual %)},1.189855642,1.677720371
Argentina,ARG,{GDP growth (annual %)},-2.565351876,-2.088014864
Armenia,ARM,{GDP growth (annual %)},5.2,7.599999999
American Samoa,ASM,{GDP growth (annual %)},2.18487395,na
Antigua and Barbuda,ATG,{GDP growth (annual %)},7.389197162,4.658649157
Australia,AUS,{GDP growth (annual %)},2.949285772,2.160956295
Austria,AUT,{GDP growth (annual %)},2.580120733,1.418734272
Azerbaijan,AZE,{GDP growth (annual %)},1.500401707,2.218704277
Uzbekistan,UZB,{GDP growth (annual %)},5.446444862,5.563723953
St. Vincent and the Grenadines,VCT,{GDP growth (annual %)},2.163128944,0.494924137
"Venezuela, RB",VEN,{GDP growth (annual %)},na,na
British Virgin Islands,VGB,{GDP growth (annual %)},na,na
Virgin Islands (U.S.),VIR,{GDP growth (annual %)},na,na
Vietnam,VNM,{GDP growth (annual %)},7.075788617,7.017434882
Vanuatu,VUT,{GDP growth (annual %)},2.900230273,3.264589732
World,WLD,{GDP growth (annual %)},2.976776266,2.343377536
Samoa,WSM,{GDP growth (annual %)},-2.063273066,3.580625558
Kosovo,XKX,{GDP growth (annual %)},3.815620226,4.171659256
"Yemen, Rep.",YEM,{GDP growth (annual %)},0.752598679,2.097414361
South Africa,ZAF,{GDP growth (annual %)},0.78705557,0.152583317
Zambia,ZMB,{GDP growth (annual %)},4.034378235,1.441784548
Zimbabwe,ZWE,{GDP growth (annual %)},4.829673591,-8.100000199

.tex file

% [ ltx-test.tex ]

% [ load packages ] - document format
\documentclass[10pt]{extarticle}
\usepackage{extsizes}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}

% [ load packages ] - page format
\usepackage{geometry}
\usepackage{titling, titlesec, fancyhdr}
\geometry{
    a4paper,
    total={170mm,257mm},
    textwidth=420pt,
    top=38mm,
    bottom=38mm,
}
\titleformat{\section}[hang]{\filcenter\scshape\normalsize}{}{1pt}{}
\titleformat{\subsection}[hang]{\filcenter\scshape\LARGE}{}{1pt}{}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\thepage}
\fancyhead[R]{\leftmark}
\renewcommand{\headrulewidth}{0pt}

% [ load packages ] - data presentation
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{csvsimple}
\usepackage{pgfkeys}



% [ document ]
\begin{document}
%% [title] block ...
%% -------------------------------------------------------------------------------------------[S]
\begin{center}
    \rmfamily
    \Large{LaTeX Example Document}
    {\fontsize{9pt}{10pt}\selectfont \tableofcontents}
    \newpage
\end{center}
%% -------------------------------------------------------------------------------------------[E]



%% [A-sub-section] block: DATA
%% -------------------------------------------------------------------------------------------[S]
\begin{center}
    \subsection{Data Example}
\end{center}
%% -------------------------------------------------------------------------------------------[E]

%% [B-paragraph] block
%% -------------------------------------------------------------------------------------------[S]
\noindent
\blindtext[1]
\bigskip
%% -------------------------------------------------------------------------------------------[E]

%% [F-table] block
%% -------------------------------------------------------------------------------------------[S]
\begin{center}
    \ttfamily
    \fontsize{8pt}{9pt}\selectfont
    \csvautotabular{gdp--gdp-per-grwth-data-truncated.csv}
    \begin{tabularx}{\linewidth}%{ m{14.3cm} }
        \rowcolors{2}{gray!20}{gray!5} % define header row color
    \end{tabularx}
\end{center}
%% -------------------------------------------------------------------------------------------[E]

2 Answers 2

1

csvsimple provides the respect percent option for this purpose:

If this key is set, every percent sign "%" inside the CSV content is a normal character

In this way you can avoid overriding the category code of % as suggested by @egreg. Here is a solution with \csvautotabular:

\begin{center}
% you can use comments here
  \fontsize{8pt}{9pt}\ttfamily
  \csvautotabular[respect percent]{\jobname-test.csv}
\end{center}

A solution using \csvreader is this:

 \csvreader[
   respect percent=true,
   before reading=\fontsize{8pt}{9pt}\ttfamily,
   tabular=|*{5}{l|},
   table head={\hline country-name & country-code & indicator-name & 2018 & 2019\\\hline},
   table foot={\hline}
  ]
  {\jobname-test.csv}
  {}
  {\csvcoli& \csvcolii &\csvcoliii &\csvcoliv & \csvcolv}

There seems to be no possibility of using \csvreader within a tabular environment (maybe a bug?) except by using the code proposed by @egreg. But doing so results in unnecessarily more complicated code:

\begin{center}
  \begin{tabular}{|l|l|l|l|l|}
  \hline
   country-name & country-code &indicator-name&2018&2019\\\hline
  \csvreader[
    before reading=\fontsize{8pt}{9pt}\ttfamily,
    respect percent,
    head to column names,
    after line={\catcode`\%=12},
    after first line={\catcode`\%=12},
    late after line={\\},
    late after last line={},
  ]{\jobname-test.csv}{}
   {\csvcoli& \csvcolii &\csvcoliii &\csvcoliv & \csvcolv }
\\\hline  
\end{tabular}
\end{center}

MWE

\begin{filecontents}[overwrite,noheader]{\jobname-test.csv}
country-name,country-code,indicator-name,2018,2019
Aruba,ABW,{GDP growth (annual %)},na,na
Afghanistan,AFG,{GDP growth (annual %)},1.189228129,3.911603419
Angola,AGO,{GDP growth (annual %)},-2.003629727,-0.624644318
Albania,ALB,{GDP growth (annual %)},4.071301295,2.240070268
Andorra,AND,{GDP growth (annual %)},1.58876549,1.849238257
Arab World,ARB,{GDP growth (annual %)},2.079469006,1.487633017
United Arab Emirates,ARE,{GDP growth (annual %)},1.189855642,1.677720371
Argentina,ARG,{GDP growth (annual %)},-2.565351876,-2.088014864
\end{filecontents}

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

\begin{document}

\section{csvautotabular}

\begin{center}
% you can use comments here
  \fontsize{8pt}{9pt}\ttfamily
  \csvautotabular[respect percent]{\jobname-test.csv}
\end{center}

\section{csvreader}

 \csvreader[
   respect percent=true,
   before reading=\fontsize{8pt}{9pt}\ttfamily,
   tabular=|*{5}{l|},
   table head={\hline country-name & country-code & indicator-name & 2018 & 2019\\\hline},
   table foot={\hline}
  ]
  {\jobname-test.csv}
  {}
  {\csvcoli& \csvcolii &\csvcoliii &\csvcoliv & \csvcolv}

\section{tabular+csvreader}

\begin{center}
  \begin{tabular}{|l|l|l|l|l|}
  \hline
   country-name & country-code &indicator-name&2018&2019\\\hline
  \csvreader[
    before reading=\fontsize{8pt}{9pt}\ttfamily,
    respect percent,
    head to column names,
    after line={\catcode`\%=12},
    after first line={\catcode`\%=12},
    late after line={\\},
    late after last line={},
  ]{\jobname-test.csv}{}
   {\csvcoli& \csvcolii &\csvcoliii &\csvcoliv & \csvcolv }
\\\hline  
\end{tabular}
\end{center}

\end{document}
2
  • How would one implement your solution with \csvreader ?
    – Maestro
    May 20, 2021 at 23:49
  • 1
    I edited the answer.
    – Ivan
    May 21, 2021 at 15:49
1

You have to disable the nature of % as a comment character. The setting will be local to the center environment, but you can't use % for comments in it.

\begin{filecontents*}{\jobname-test.csv}
country-name,country-code,indicator-name,2018,2019
Aruba,ABW,{GDP growth (annual %)},na,na
Afghanistan,AFG,{GDP growth (annual %)},1.189228129,3.911603419
Angola,AGO,{GDP growth (annual %)},-2.003629727,-0.624644318
Albania,ALB,{GDP growth (annual %)},4.071301295,2.240070268
Andorra,AND,{GDP growth (annual %)},1.58876549,1.849238257
Arab World,ARB,{GDP growth (annual %)},2.079469006,1.487633017
United Arab Emirates,ARE,{GDP growth (annual %)},1.189855642,1.677720371
Argentina,ARG,{GDP growth (annual %)},-2.565351876,-2.088014864
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple,tabularx}
\usepackage[table]{xcolor}

\begin{document}

\begin{center}
  \catcode`\%=12
  \fontsize{8pt}{9pt}\ttfamily
  \csvautotabular{\jobname-test.csv}
\end{center}

\end{document}

enter image description here

You must log in to answer this question.

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