和包裹datatool
有一个\DTLiffirstrow
。我正在寻找一个类似的命令来检测最后一行,,\DTLiflastrow
但找不到。我试图创建自己的命令,但没有成功。
如果标题行与数据分开,则可以使用
\DTLiffirstrow{\\\Midrule}{\\\cmidrule{1-2}}
产生如下水平线:
但如果标题是数据的一部分,我不知道如何在没有等效项的情况下做到这一点\DTLiflastrow
。没有等效项,我会在底部得到一个额外的空白行:
如果你取消注释我的尝试,\DTLifLastRow
你将得到:
!缺失数字,视为零。
\dtlrows@
代码:
\documentclass{article}
\usepackage{datatool}
\usepackage{booktabs}
\def\Midrule{\midrule[\heavyrulewidth]}
% Make csv in question
\usepackage{filecontents}
\begin{filecontents*}{scientists2.csv}
name,surname,
First,Last
Albert,Einstein,
Marie,Curie,
Thomas,Edison,
\end{filecontents*}
\newcommand{\DTLifLastRow}[3]{%
% #1 = db
% #2 = code for last row
% #3 = code for all other rows
\dtlifnumeq
{\DTLrowcount{#1}}
{\DTLcurrentindex}% {\value{DTLrowi}}%
{#2}{#3}%
}
\begin{document}
\DTLloaddb{myDB}{scientists2.csv}
%\DTLdisplaydb{myDB}% Useful for debugging.
\begin{tabular}{ll}\toprule
\textbf{First Name} & \textbf{Last Name}%
\DTLforeach*{myDB}{\Name=name,\Surname=surname}{%
\DTLiffirstrow{\\\Midrule}{\\\cmidrule{1-2}}%
\Name & \Surname
}%
\\\bottomrule
\end{tabular}
\bigskip\noindent
But what if the first row of the table was the header row.
How do I produce the above table format without the manual header.
\medskip
\begin{tabular}{ll}\toprule
\DTLforeach*{myDB}{\Name=name,\Surname=surname}{%
\Name & \Surname
\DTLiffirstrow{\\\Midrule}{\\\cmidrule{1-2}}%
% I think I need to replace the above with something like this:
%\DTLiffirstrow{\\\Midrule}{%
% \DTLifLastRow{}{myDB}{\\\cmidrule{1-2}}%
%}%
}%
\\\bottomrule
\end{tabular}
\end{document}
答案1
这似乎是在 内部执行操作时通常会遇到的问题之一tabular
。最好的办法似乎是先用表内容填充令牌寄存器,然后将其传递到 内部tabular
:
\documentclass{article}
\usepackage{datatool}
\usepackage{booktabs}
\def\Midrule{\midrule[\heavyrulewidth]}
% Make csv in question
\usepackage{filecontents}
\begin{filecontents*}{scientists2.csv}
name,surname,
First,Last
Albert,Einstein,
Marie,Curie,
Thomas,Edison,
\end{filecontents*}
\newcommand{\DTLifLastRow}[3]{%
% #1 = db
% #2 = code for last row
% #3 = code for all other rows
\dtlifnumeq
{\DTLrowcount{#1}}
{\DTLcurrentindex}%
{#2}{#3}%
}
\newtoks\grilltoks
\begin{document}
\DTLloaddb{myDB}{scientists2.csv}
%\DTLdisplaydb{myDB}% Useful for debugging.
\grilltoks={}
\DTLforeach*{myDB}{\Name=name,\Surname=surname}
{%
\edef\temp{\Name & \Surname}%
\grilltoks=\expandafter{\the\expandafter\grilltoks\temp}%
\DTLiffirstrow
{\grilltoks=\expandafter{\the\grilltoks\\\Midrule}}
{%
\DTLifLastRow{myDB}{}{\grilltoks=\expandafter{\the\grilltoks\\\cmidrule{1-2}}}%
}%
}
\begin{tabular}{ll}\toprule
\the\grilltoks
\\\bottomrule
\end{tabular}
\end{document}
实际上,datatool
有一个\DTLiflastrow
命令,但它不能按预期工作:确实
\DTLforeach*{myDB}{\Name=name,\Surname=surname}
{\DTLiflastrow{LAST\endgraf}{NOT LAST\endgraf}}
印刷
不是最后
不是最后
最后
不是最后