我正在尝试使用 csvsimple-l3 在表格的最后一行和最后一个元素之前的一行之间添加一行。重申一下,我想要 9 和 10 之间的一行 - 有没有什么巧妙的解决方案?
我的csv文件:
101.6,0.08,0.08
101.4,-0.12,0.12
101.5,-0.02,0.02
101.4,-0.12,0.12
101.6,0.08,0.08
101.5,-0.02,0.02
101.6,0.08,0.08
101.5,-0.02,0.02
101.6,0.08,0.08
101.5,-0.02,0.02
101.52,,0.07
我的 .tex 表格:
\section{Tabuľka: }
\begin{center}
\csvreader[
tabular = cccc,
table head = & $\displaystyle\frac{a}{mm}$ & $\displaystyle\frac{\Delta a}{mm}$ & $\di
splaystyle\frac{|\Delta a|}{mm}$\vspace{5pt}\\\hline,
table foot = \hline
]{Tabulky/a-dimenzia.csv}{}
{\thecsvrow & \csvcoli & \csvcolii & \csvcoliii }
\end{center}
\end{document}
桌子图片:
答案1
一旦知道行数,就可以添加条件\hline
。
您如何自动执行此操作?您可以读取 csv 文件而无需排版任何内容并存储行数。
\begin{filecontents*}{\jobname.csv}
101.6,0.08,0.08
101.4,-0.12,0.12
101.5,-0.02,0.02
101.4,-0.12,0.12
101.6,0.08,0.08
101.5,-0.02,0.02
101.6,0.08,0.08
101.5,-0.02,0.02
101.6,0.08,0.08
101.5,-0.02,0.02
101.52,,0.07
\end{filecontents*}
\documentclass{article}
\usepackage{amsmath}
\usepackage{csvsimple-l3}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\csvreader[
tabular = cccc,
table head = & $\displaystyle\frac{a}{mm}$ & $\displaystyle\frac{\Delta a}{mm}$ &
$\displaystyle\frac{|\Delta a|}{mm}$\vspace{5pt}\\\hline,
table foot = \hline
]{\jobname.csv}{}
{\ifnum\thecsvrow=10 \hline\fi \thecsvrow & \csvcoli & \csvcolii & \csvcoliii }
%%
\qquad
%%
\csvreader[
before reading = \sisetup{table-number-alignment=center},
tabular = cccc,
table head = \toprule & {$\dfrac{a}{\mathrm{mm}}$} & {$\dfrac{\Delta a}{\mathrm{mm}}$} &
{$\dfrac{|\Delta a|}{\mathrm{mm}}$}\\\addlinespace\midrule,
table foot = \bottomrule
]{\jobname.csv}{}
{%
\ifnum\thecsvrow=10 \midrule\fi
\thecsvrow &
\tablenum[table-format=3.2]{\csvcoli} &
\tablenum[table-format=-1.2]{\csvcolii} &
\tablenum[table-format=1.2]{\csvcoliii}
}
\end{document}
我给出了表格的两种实现方式,但我还建议删除“mm”分母,并在表格标题中添加“长度以毫米为单位”。
答案2
这是一个{NiceTabular}
使用 的解决方案nicematrix
。
数组最后一行之前的规则是 Tikz 在构建数组后,使用单元格、列和行下创建的 PGF/Tikz 节点绘制的nicematrix
。
\begin{filecontents*}[overwrite]{\jobname.csv}
101.6,0.08,0.08
101.4,-0.12,0.12
101.5,-0.02,0.02
101.4,-0.12,0.12
101.6,0.08,0.08
101.5,-0.02,0.02
101.6,0.08,0.08
101.5,-0.02,0.02
101.6,0.08,0.08
101.5,-0.02,0.02
101.5,,0.07
\end{filecontents*}
%%%
\documentclass{article}
\usepackage{csvsimple}
\usepackage{nicematrix}
\usepackage{tikz}
\usepackage{siunitx}
\begin{document}
\section{Tabuľka: }
$\begin{NiceArray}{ccSc}[cell-space-limits=2pt]
& \displaystyle\frac{a}{mm}
& \displaystyle\frac{\Delta a}{mm}
& \displaystyle\frac{|\Delta a|}{mm} \\ \hline
\csvreader[late after line = \\]
{\jobname.csv}{}
{\thecsvrow & \csvcoli & \csvcolii & \csvcoliii}
\hline
\CodeAfter
\tikz \draw (\arabic{iRow}-|1) -- (\arabic{iRow}-|last) ;
\end{NiceArray}$
\end{document}
由于 PGF/Tikz 节点,您需要进行多次编译。
答案3
以下解决方案\createtable
使用两个参数来制作宏:csv
文件的名称和表头。此宏csv
首先通过\read
原始文件读取文件,将数据保存到\csvdata
并计算行数。然后\csvdata
由宏再次读取\prepcsvdata
,用替换行尾\cr
,用替换逗号&
并在最后一行之前插入\noalign{\cvsrule}
。最后,通过打印表格\halign
。
\newread\csvin
\newcount\csvlines
\def\csvcount{%
\ifeof\csvin \else
\read\csvin to\csvline
\if^\csvline^\else \global\advance\csvlines by1 \fi
\global\expandafter\addto\expandafter\csvdata\expandafter{\csvline\^^M}%
\expandafter \csvcount
\fi
}
\def\prepcsvdata #1\^^M{%
\advance\csvlines by-1
\edef\replline{\the\numexpr\csvtotlines-\csvlines&}%
\ifnum\csvlines=0 \def\replline{&}\fi
\replcommas#1,\relax
\expandafter\addto\expandafter\csvdata\expandafter{\replline\cr}%
\ifnum\csvlines=1 \addto\csvdata{\noalign{\csvrule}}\fi
\ifnum\csvlines=0 \else \expandafter\prepcsvdata \fi
}
\def\replcommas #1,#2{\addto\replline{#1}%
\ifx\relax#2 \else \addto\replline{&}\expandafter\replcommas\fi #2}
\def\csvrule{\kern1ex\hrule\kern1ex}
\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\createtable#1#2{%
\openin\csvin=#1
\ifeof\csvin \errmessage{file "#1" not found}\fi
\csvlines=0 \def\csvdata{}%
{\endlinechar=-1 \csvcount}
\closein\csvin
\chardef\csvtotlines=\csvlines
\edef\csvdata{\expandafter}\expandafter\prepcsvdata\csvdata
% table itself:
\vbox{\halign{&\kern.5em \hfil$\displaystyle{##}$\hfil\kern.5em \cr
\cr \noalign{\csvrule}
\csvdata
}}%
}
% test:
\createtable {dimenzia.csv}
{a\over\rm mm & \Delta\over\rm mm & |\Delta a|\over\rm mm}
该宏仅使用 TeX 基元和基本纯 TeX 宏。您可以在纯 TeX、LaTeX 或其他任何环境中使用它。
答案4
一种方法是手动创建表格,包括带有规则的页眉和页脚(最后一行),然后csvreader
仅循环遍历数据
\begin{filecontents*}{\jobname.csv}
101.6,0.08,0.08
101.4,-0.12,0.12
101.5,-0.02,0.02
101.4,-0.12,0.12
101.6,0.08,0.08
101.5,-0.02,0.02
101.6,0.08,0.08
101.5,-0.02,0.02
101.6,0.08,0.08
101.5,-0.02,0.02
\end{filecontents*}
%%%
\documentclass{article}
\usepackage{csvsimple}
\begin{document}
\section{Tabuľka: }
\begin{tabular}{cccc}
& $\displaystyle\frac{a}{mm}$
& $\displaystyle\frac{\Delta a}{mm}$
& $\displaystyle\frac{|\Delta a|}{mm}$\vspace{5pt} \\ \hline
\csvreader[late after line = \\,no head]
{\jobname.csv}{}
{\thecsvrow & \csvcoli & \csvcolii & \csvcoliii}
\hline
\stepcounter{csvrow}%
\thecsvrow & 101.5 & & 0.07 \\ \hline
\end{tabular}
\end{document}