我正在尝试获取如下简单的表格:
我正在使用,longtable
因为我在文件中有数据.dat
。
您能帮助我通过从文件导入数据成功生成表格吗?
以下是我目前所掌握的信息:
\documentclass{book}
\usepackage{calc}
\usepackage[left=1.5cm, right=1.5cm, bindingoffset=1.5cm, headheight=30pt, top={120pt+10mm}]{geometry}
\usepackage{pgfplotstable}
\usepackage{longtable}
\usepackage{filecontents}
\begin{filecontents}{data133.dat}
X 2,7 3,6 2,5 3,5 3,3 2,2 5,1 6,2 8,1 6,4 4,8 5,8 9,5 9,9 9,4 8,9 8,8 6,9 7,4 4,4
Y +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
\end{filecontents}%
\begin{document}
%%% Code from Dr. Christian ------ 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
}
}
}
}
\pgfplotstableset{
begin table=\bgroup\small\setlength{\tabcolsep}{10pt}\begin{longtable}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|},
end table=\end{longtable}\egroup,
}
\pgfplotstabletypeset[col sep=space,empty header, rows = {X,Y},
]{data133.dat}
\end{document}
答案1
一个要求是,字段宽度包含的\specialtable
列数大于或等于数据文件中预期的最大列数。
我擅自以数学模式设置了表格内容。
已编辑(2016 年 12 月),删除了每个文件记录末尾的空格要求。已升级为 2016-11-07readarray
版本的首选语法。
\documentclass{book}
\usepackage[left=1.5cm, right=1.5cm, bindingoffset=1.5cm, headheight=30pt,
top={\dimexpr120pt+10mm}]{geometry}
\usepackage{readarray}[2016-11-07]
\usepackage{filecontents}
\begin{filecontents*}{data133.dat}
X 2,7 3,6 2,5 3,5 3,3 2,2 5,1 6,2 8,1 6,4 4,8 5,8 9,5 9,9 9,4 8,9 8,8 6,9 7,4 4,4
Y +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
\end{filecontents*}%
\newcommand\digest[1]{%
\edef\tmp{\mydata[#1]}%
\expandafter\digestaux\tmp{} \relax%
}
\def\digestaux#1 #2\relax{$#1$\ifx\relax#2\relax\else\digestauxB#2\relax\fi}
\def\digestauxB#1\relax{&\digestaux#1\relax}
\newcommand\specialtable[1]{%
\noindent
\tabcolsep=4pt\relax%
\readrecordarray{#1}\mydata%
% THE FOLLOWING LINE MUST ALLOW FOR THE MAX. NUMBER OF COLUMNS EXPECTED
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
\hline\digest{1}\\\hline\digest{2}\\\hline
\end{tabular}%
}
\begin{document}
\specialtable{data133.dat}
\end{document}
答案2
如果您可以添加虚拟标头,则以下内容似乎有效。
\documentclass{standalone}
\usepackage{calc}
% \usepackage[left=1.5cm, right=1.5cm, bindingoffset=1.5cm, headheight=30pt,
% top={120pt+10mm}]{geometry}
\usepackage{pgfplotstable}
\usepackage{longtable}
\usepackage{filecontents}
\begin{filecontents*}{data133.dat}
Z x x x x x x x x x x x x x x x x x x x x
X 2,7 3,6 2,5 3,5 3,3 2,2 5,1 6,2 8,1 6,4 4,8 5,8 9,5 9,9 9,4 8,9 8,8 6,9 7,4 4,4
Y +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
\end{filecontents*}%
\begin{document}
%%% Code from Dr. Christian ------ 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
}
}
}
}
% \pgfplotstableset{
% begin table={\bgroup\small\setlength{\tabcolsep}{10pt}\begin{longtable}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}},
% end table={\end{longtable}\egroup},
% }
% \pgfplotstableset{
% begin table=\bgroup\small\setlength{\tabcolsep}{10pt}\begin{longtable},
% end table=\end{longtable}\egroup,
% }
\pgfplotstabletypeset[col sep=space, empty header, read comma as period,
outfile=\jobname.out.tex,
columns/Z/.style={
column name={},
assign cell content/.code={%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{##1}
},
},
]{data133.dat}
\end{document}