
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 및 입력 - `Extra } 또는 잊어버린 $.`자세한 내용은).
결과는 다음과 같습니다.
이 솔루션에는 두 가지 문제가 있습니다.
- 색상이 지정된 헤더로 인해 마지막 헤더 셀이 완전하지 않습니다(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}