问题
关于第 1.2 节第一步的csvsimple手册 [链接:https://ctan.math.washington.edu/tex-archive/macros/latex/contrib/csvsimple/csvsimple.pdf],说明如下,假设文件已存在于当前目录中:
csvautotabular{name-of-file.csv}
但是我无法完成这个任务。以下是 csv 内容和我的 .tex 文件。
CSV 内容:'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 文件
% [ 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]
答案1
csvsimple
respect percent
为此目的提供了选项:
如果设置了该键,则 CSV 内容中的每个百分号“%”都是普通字符
这样,您就可以避免覆盖 @egreg 建议的 % 类别代码。以下是解决方案\csvautotabular
:
\begin{center}
% you can use comments here
\fontsize{8pt}{9pt}\ttfamily
\csvautotabular[respect percent]{\jobname-test.csv}
\end{center}
解决方案\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}
除了使用 @egreg 提出的代码外,似乎没有在环境\csvreader
中使用的可能性(可能是错误?)。但这样做会导致不必要的更复杂的代码:tabular
\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}
平均能量损失
\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
%
您必须禁用作为注释字符的性质。该设置将仅限于center
环境,但您不能%
在其中使用它进行注释。
\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}