
我想使用該datatool
套件來讀取 csv 檔案並將它們解析為 LaTeX 表。此外,我想用來siunitx
格式化 csv 檔案中的數字。看一下下面的例子:
\documentclass{article}
\usepackage{datatool}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\usepackage{colortbl}
%% table data
\begin{filecontents*}{scientists.csv}
name,surname,age,IQ
Albert,Einstein,133,210.12
Marie,Curie,145,220.12
\end{filecontents*}
%%% table design
\colorlet{tableheadcolor}{black!60}
\newcommand\tableheadfont{
\sffamily\bfseries
\slshape
\color{white}
}
\begin{document}
\DTLloaddb{table}{scientists.csv}
\sisetup{round-mode=places,
table-number-alignment = center-decimal-marker
}
\rowcolors{1}{gray!15}{white!100}
\begin{table}
\begin{tabular}{l
l
S[table-format = 3.0 ,round-precision=0]
S[table-format = 3.2 ,round-precision=2]
@{}l}
\rowcolor{tableheadcolor}
\tableheadfont name & \tableheadfont surname & \tableheadfont age & \tableheadfont iq & \tabularnewline
\hline
\DTLforeach*{table}%
{\name=name, \surname=surname, \age=age, \iq=IQ}%
{\DTLiffirstrow{}{\tabularnewline}%
\name & \surname & \age & \iq &
}
\end{tabular}
\end{table}
\end{document}
最後一列包含一個數值,其格式應為siunitx
(S 列)。這就是為什麼我必須附加一個空白行(看看表格、siunitx 和輸入 - `額外 },或忘記 $。了解詳情)。
這是結果:
該解決方案有兩個問題:
- 由於彩色標題,最後一個標題單元格不完整(缺少 q)
- 第一個資料單元中的「Albert」向右移動。
有人知道我該如何解決這些問題嗎?
答案1
都是簡單的問題。如同評論中所提到的,由於缺失,「Albert」的位置錯位%
:
\DTLiffirstrow{}{\tabularnewline}%
奇怪的事情q
是由於它位於& \tabularnewline
標題行的末尾,導致內容放錯了位置。嘗試改為\\
:
\documentclass{article}
\usepackage{datatool}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage{colortbl}
%% table data
\begin{filecontents*}{scientists.csv}
name,surname,age,IQ
Albert,Einstein,133,210.12
Marie,Curie,145,220.12
\end{filecontents*}
%% table design
\colorlet{tableheadcolor}{black!60}
\newcommand\tableheadfont{%
\sffamily\bfseries
\slshape
\color{white}
}
\begin{document}
\DTLloaddb{table}{scientists.csv}
\sisetup{round-mode=places,
table-number-alignment = center-decimal-marker
}
\begin{tabular}{l
l
S[table-format = 3.0 ,round-precision=0]
S[table-format = 3.2 ,round-precision=2]
@{}l}
\rowcolor{tableheadcolor}
\tableheadfont name & \tableheadfont surname & \tableheadfont age & \tableheadfont the long iq \\
\hline
\DTLforeach*{table}%
{\name=name, \surname=surname, \age=age, \iq=IQ}%
{\DTLiffirstrow{}{\tabularnewline}%
\name & \surname & \age & \iq &
}
\end{tabular}
\end{document}