为了编写一系列包含复杂长表的工业报告,我结合了在网站上找到的代码样本,这看起来很有希望。在我的领域,尽管通常推荐这种做法,但单元格需要用垂直线和水平线分隔。在 MWE(与实际需要相比非常简化)中,我仍然面临两个问题:数据行 1 和 2 之间没有水平线。此外,我无法在两列之间放置垂直线(以及其他要添加的垂直线)。我也欢迎任何改进代码(我不清楚在哪里最好地格式化列并定义每行之间的水平线)和/或视觉外观的提示。MWE 中的垂直线不是连续的。
\documentclass[a5paper]{article}
\usepackage{pgfplotstable}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{array}
\usepackage[top=8 cm,bottom=8cm]{geometry} %For demo purposes
\usepackage{filecontents}
\begin{filecontents}{data1.csv}
column1,column2
5001,102
5002,75
5003,115
5004,45
5005,97
5036,110
5037,77
5038,147
5039,89
5040,62
5041,160
5042,102
\end{filecontents}%
\begin{document}
%%% Code from Dr. Christian Feuersanger ------ for not using headers.----------------------
\pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{
% upcoming releases offer this more convenient option:
\pgfplotstableset{
empty header/.style={
every head row/.style={output empty row},
}
}
}{
% versions up to and including 1.5.1 need this:
\pgfplotstableset{
empty header/.style={
typeset cell/.append code={%
\ifnum\pgfplotstablerow=-1 %
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
}
}
}
}
%%%-----------------------------------------------
\newcommand\MyHead[2]{%
\multicolumn{1}{|> {\centering}m{#1}|}{#2}
}
\pgfplotstabletypeset[
empty header,
outfile = test.dat,
begin table=\begin{longtable},
every first column/.style ={%
column type/.add={|}{}
},
every last column/.style ={%
column type/.add={}{|}
},
every head row/.style={output empty row},
every nth row={1}{after row=\midrule},
every first row/.append style={before row={%
\caption{This is a long table spreading over several pages.}%
\label{tab:DataTable}\\\toprule
\MyHead{4cm} {column with long header} & \MyHead{4cm} {Other column with very long header} \\
\hline
\endfirsthead
%
\multicolumn{2}{c}%
{{\bfseries Table \thetable\ Continued from previous page}} \\
\toprule
%
\MyHead{4cm} {column with long header} & \MyHead{4cm} {Other column with very long header} \\
\hline
\endhead
%
\midrule \multicolumn{2}{r}{{Continued on next page}} \\ \bottomrule
\endfoot
%
\midrule
\multicolumn{2}{r}{{End of table}} \\ \bottomrule
\endlastfoot
}},%
%
end table=\end{longtable},
col sep=comma,
string type,
]{data1.csv}
\end{document}
答案1
让我们从问题的简单方面开始:
- 如果你喜欢使用垂直线,那么不要使用 中的规则
\booktabs
。相反,使用\hline
对于垂直线我建议执行以下操作:
every odd column/.style ={% column type/.add={|}{} }, every even column/.style ={% column type/.add={|}{} }, every last column/.style ={% column type/.add={}{|} },
对我的口味来说,最好这样说
every nth row={1}{before row=\hline}
。表格下方的文字(下页继续)没有行。
完整代码:
\documentclass[a5paper]{article}
\usepackage{pgfplotstable}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{array}
\usepackage[top=8 cm,bottom=8cm]{geometry} %For demo purposes
\usepackage{filecontents}
\begin{filecontents}{data1.csv}
column1,column2
5001,102
5002,75
5003,115
5004,45
5005,97
5036,110
5037,77
5038,147
5039,89
5040,62
5041,160
5042,102
\end{filecontents}%
\begin{document}
%%% Code from Dr. Christian Feuersanger ------ for not using headers.----------------------
\pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{
% upcoming releases offer this more convenient option:
\pgfplotstableset{
empty header/.style={
every head row/.style={output empty row},
}
}
}{
% versions up to and including 1.5.1 need this:
\pgfplotstableset{
empty header/.style={
typeset cell/.append code={%
\ifnum\pgfplotstablerow=-1 %
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
}
}
}
}
%%%-----------------------------------------------
\newcommand\MyHead[2]{%
\multicolumn{1}{|> {\centering}m{#1}|}{#2}
}
\pgfplotstabletypeset[
empty header,
outfile = test.dat,
begin table=\begin{longtable},
every odd column/.style ={%
column type/.add={|}{}
},
every even column/.style ={%
column type/.add={|}{}
},
every last column/.style ={%
column type/.add={}{|}
},
every head row/.style={output empty row},
every nth row={1}{before row=\hline},
every first row/.append style={before row={%
\caption{This is a long table spreading over several pages.}%
\label{tab:DataTable}\\\hline
\MyHead{4cm} {column with long header} & \MyHead{4cm} {Other column with very long header} \\
\hline
\endfirsthead
%
\multicolumn{2}{c}%
{Table \thetable\ Continued from previous page} \\
\hline
%
\MyHead{4cm} {column with long header} & \MyHead{4cm} {Other column with very long header} \\
\endhead
%
\multicolumn{2}{r}{{Continued on next page}} \\
\endfoot
%
\hline
\multicolumn{2}{r}{{End of table}} \\
\endlastfoot
}},%
%
end table=\end{longtable},
col sep=comma,
string type,
]{data1.csv}
\end{document}