如何将 CSV 文件放入 LaTeX 中而无需进行太多调整?

如何将 CSV 文件放入 LaTeX 中而无需进行太多调整?

我想要一个 TeX-Snippet,它以文件名作为参数并输出带有 toprule 和 bottomrule 的表格:

\begin{tabular}{lrr} 
    \toprule
    Fach & Dauer & Einkommen\\ 
    \midrule 
    Info & 2 & 12,75 \\
    MST & 6 & 8,20 \\
    VWL & 14 & 10,00\\ 
    \bottomrule
\end{tabular}

在此处输入图片描述

来源:http://www.namsu.de/Extra/pakete/Booktabs.html

会议记录.csv

这是一个示例 csv 文件:

Messreihe #,Winkel,Zähler 1,Zähler 2,Koinzidenzen
1,90,35404,41016,155
,90,31999,38359,155
,135,30618,36095,126
,135,30232,36000,120
,180,30243,33931,118
,180,29762,34080,129
2,90,28979,36801,118
,90,29009,37035,144
,135,29035,35828,115
,135,28330,35599,122
,180,28866,33316,118
,180,28716,33774,99
3,90,29075,37602,139
,90,28793,37495,133
,135,28368,35938,116
,135,28570,35419,129
,180,28632,34177,114
,180,28467,34076,111

我尝试过的方法

pgfplotstable

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{pgfplotstable} % Generates table from .csv

% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}

\begin{table}[h!]
  \begin{center}
    \caption{Autogenerated table from .csv file.}
    \label{table1}
    \pgfplotstabletypeset[
      multicolumn names, % allows to have multicolumn names
      col sep=comma, % the seperator in our .csv file
      display columns/0/.style={
        column name=$Value 1$, % name of first column
        column type={S},string type},  % use siunitx for formatting
      display columns/1/.style={
        column name=$Value 2$,
        column type={S},string type},
      display columns/2/.style={
        column name=$Value 3$,
        column type={S},string type},
      display columns/3/.style={
        column name=$Value 4$,
        column type={S},string type},
      display columns/4/.style={
        column name=$Value 5$,
        column type={S},string type},
      every head row/.style={
        before row={\toprule}, % have a rule at top
        after row={
            Messreihe & Winkel & Zähler 1 & Zähler 2 & Koinzidenzen\\ % the units seperated by &
            \midrule} % rule under units
            },
        every last row/.style={after row=\bottomrule}, % rule at bottom
    ]{messergebnisse.csv} % filename/path to file
  \end{center}
\end{table}

\end{document}

这给了我:

./csv-physik.tex:47: Illegal parameter number in definition of \pgfplotstable@loc@TMPa. [    ]{messergebnisse.csv}]

csvsimple-尝试 1

仅当我删除 CSV 文件内的尖音符号 ( #) 并且格式不符合要求时,此方法才有效:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{csvsimple}

% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}
\csvautotabular{messergebnisse.csv}
\end{document}

在此处输入图片描述

csvsimple-尝试 2

这个不太方便,因为我必须调整标题。此解决方案的另一个缺点是bottomrule缺少:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{csvsimple}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}
\csvreader[tabular=lrrrr,
    table head=\toprule Messwert & Winkel & Zähler 1 & Zähler 2 & Koinzidenzen\\\midrule,
    late after line=\\]%
{messergebnisse.csv}{a=\a,b=\b,c=\c,d=\d,e=\e}%
{\a & \b & \c & \d & \e}%
\end{document}

在此处输入图片描述

datatool-第一次尝试

#这很容易使用,但样式不正确。而且在 CSV 中不可能有尖角:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{datatool}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}
\DTLloaddb{stores}{messergebnisse.csv}
\DTLdisplaydb{stores}
\end{document}

在此处输入图片描述

datatool-第二次尝试

这几乎就是我想要的。我唯一想念的是#在表格标题中写一个升号。

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{datatool}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% start every dtl table with \toprule from booktabs
\renewcommand{\dtldisplaystarttab}{\toprule}

% likewise for \midrule and \bottomrule from booktabs 
\renewcommand{\dtldisplayafterhead}{\midrule}
\renewcommand{\dtldisplayendtab}{\\\bottomrule}

% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}
\DTLloaddb{stores}{messergebnisse.csv}
\DTLdisplaydb{stores}
\end{document}

在此处输入图片描述

答案1

datatool软件包还附带命令,该命令\DTLloadrawdb类似于\DTLloaddb,但将特殊字符(例如#)映射到命令(例如)\#。但是,这里还有一个额外的问题。默认情况下,列标签设置为 CSV 文件中的相应标题,但由于这些标签用于构造控制序列(通过\csname... \endcsname),因此它们不能包含任何活动字符。这意味着不仅会#导致问题,而且ä字符也会出现问题,因为变音符号会被软件包转换为活动字符inputenc

解决方案是不仅使用\DTLloadrawdb来修复#标题文本,而且还使用keys选项来覆盖默认的列标签:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{datatool}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% start every dtl table with \toprule from booktabs
\renewcommand{\dtldisplaystarttab}{\toprule}

% likewise for \midrule and \bottomrule from booktabs 
\renewcommand{\dtldisplayafterhead}{\midrule}
\renewcommand{\dtldisplayendtab}{\\\bottomrule}

% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\DTLloadrawdb[keys={Messreihe,Winkel,Zahler1,Zahler2,Koinzidenzen}]{stores}{messergebnisse.csv}

\begin{document}
\DTLdisplaydb{stores}
\end{document}

得出的结果为:

表格图片

答案2

还有一个解决方案,无需从 中csvsimple删除升号。因此,必须在本地更改 catcode。此外,手动设置列名称以应对变音符号、空格和数字。#Messreihe.csv

使用csvsimple版本1.11 (2014/07/08),特殊字符#被制作普通的使用选项respect sharp

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{csvsimple}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}

\csvloop{file=messergebnisse.csv,
  tabular=lrrrr,
  respect sharp,
  table head=\toprule\bfseries Messwert \#
             &\bfseries Winkel &\bfseries Zähler 1
             &\bfseries Zähler 2 &\bfseries Koinzidenzen\\\midrule,
  command=\csvlinetotablerow,
  table foot=\bottomrule}

\end{document}

第二种方法也适用于旧版本的csvsimple。此外,我使用列的显式命名,如果不是所有列都应出现在结果表中,则需要这样做:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{csvsimple}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}

\csvreader[tabular=lrrrr,
    table head=\toprule\bfseries Messwert \#
             &\bfseries Winkel &\bfseries Zähler 1
             &\bfseries Zähler 2 &\bfseries Koinzidenzen\\\midrule,
    late after line=\\,late after last line=\\\bottomrule,
    before reading={\catcode`\#=12},after reading={\catcode`\#=6}]%
{messergebnisse.csv}{1=\Messreihe,2=\Winkel,3=\ZaehlerI,4=\ZaehlerII,5=\Koinzidenzen}%
{\Messreihe & \Winkel & \ZaehlerI & \ZaehlerII & \Koinzidenzen}%

\end{document}

两个源代码编译后的结果相同:

在此处输入图片描述

答案3

这对我有用(请注意,我删除了inputencXeTeX 的朋友,你可以把它们放回去):

\documentclass[a4paper]{scrartcl}

\usepackage[locale=DE]{siunitx}
\sisetup{
  round-mode          = places,
  round-precision     = 2,
}

\usepackage{booktabs}
\usepackage{pgfplotstable}

\begin{document}
\pgfplotstabletypeset[
col sep=comma,
every head row/.style={
  before row={\toprule},
  after row={\midrule}},
every last row/.style={after row=\bottomrule},
]{data.csv}
\end{document}

输出

有趣的是,似乎在某个地方非 ASCII 字符被删除了。非常烦人,但似乎是一个单独的问题。我不知道是什么原因造成的。

相关内容