我使用 pgfplotstable 以以下方式从 .csv 文件创建表格:
Pgfplotstable 首先通过合并输入表的两列并$\pm$
在两个初始列的条目之间放置一个来创建一些列。
NaN
字段替换为\ensuremath{-}
我遇到的问题是,我需要调整其中一些表格的大小,因此将它们包装在语句中\resizebox
。但是,它\resizebox
并没有按预期工作 - 可能是因为语句create on use
。看起来好像\resizebox
在实际的第一列前面看到了额外的“不可见”列,因此使表格比需要的要小。因此,表格看起来好像是右对齐的,而不是居中的。
以下是 MWE:
\documentclass{article}
\usepackage{multirow}
\usepackage{pgfplotstable}
%Make rows higher:
\setlength\extrarowheight{1.4pt}
%GLOBAL FORMATING:
\pgfkeys{/pgf/number format/.cd,
use comma,
1000 sep={}}
\usepackage{lipsum}
\usepackage{filecontents}
\begin{filecontents}{data.csv}
Name,1,1_err,2,2_err,3,3_err,4,4_err,5
A,10.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,3.42
B,11.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
C,12.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
D,13.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
E,14.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
F,15.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
G,nan,nan,nan,nan,nan,nan,23.3,90.23,nan
\end{filecontents}
\begin{document}
\lipsum[1]
\begin{table}[!htb]
\centering
\caption{Some text describing the table}
\pgfplotstablegetrowsof{data.csv}
\pgfmathtruncatemacro{\N}{\pgfplotsretval-1}% <---"\N" equals the index of the last row
\pgfmathtruncatemacro{\M}{\pgfplotsretval}% <---"\M" equals the total number of rows
\resizebox*{\textwidth}{!}{\pgfplotstabletypeset[col sep=comma,
%------------------CREATING NEW COLUMNS:-------------------------------
create on use/one/.style={% <---create and typeset new column "one" out of columns 1 and 1_err of the input file
create col/assign/.code={%
\edef\entry{\noexpand\pgfmathprintnumber[fixed, zerofill, precision=2]{\thisrow{1}}$\pm$\noexpand\pgfmathprintnumber[fixed, zerofill, precision=2]{\thisrow{1_err}}}
\pgfkeyslet{/pgfplots/table/create col/next content}{\entry}%
},
},
create on use/two/.style={% <---create and typeset new column "two" out of columns 2 and 2_err of the input file
create col/assign/.code={%
\edef\entry{\noexpand\pgfmathprintnumber[fixed, zerofill, precision=2]{\thisrow{2}}$\pm$\noexpand\pgfmathprintnumber[fixed, zerofill, precision=2]{\thisrow{2_err}}}
\pgfkeyslet{/pgfplots/table/create col/next content}{\entry}%
},
},
create on use/three/.style={% <---create and typeset new column "three" out of columns 3 and 3_err of the input file
create col/assign/.code={%
\edef\entry{\noexpand\pgfmathprintnumber[sci, zerofill, precision=2]{\thisrow{3}}$\pm$\noexpand\pgfmathprintnumber[sci, zerofill, precision=2]{\thisrow{3_err}}}
\pgfkeyslet{/pgfplots/table/create col/next content}{\entry}%
},
},
create on use/four/.style={% <---create and typeset new column "four" out of columns 4 and 4_err of the input file
create col/assign/.code={%
\edef\entry{\noexpand\pgfmathprintnumber[fixed, zerofill, precision=1]{\thisrow{4}}$\pm$\noexpand\pgfmathprintnumber[fixed, zerofill, precision=1]{\thisrow{4_err}}}
\pgfkeyslet{/pgfplots/table/create col/next content}{\entry}%
},
},
%
%------------------END CREATING NEW COLUMNS-------------------------------
columns={Name, one, two, three, four, 5},% <---these columns will appear in the table
%
%------------------TYPESETTING COLUMNS:-------------------------------
columns/Name/.style={% <---style column "Name"
string type,%
column type/.add={|}{|},%
},
%
columns/one/.style={% <---style column "one"
column name=I,
string type, column type/.add={}{|},%
postproc cell content/.append code={% <---replaces last cell with "-"
\ifnum\pgfplotstablerow=\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi
},
},
%
columns/two/.style={% <---style column "two"
column name=II,
string type, column type/.add={}{|},%
postproc cell content/.append code={% <---replaces last cell with "-"
\ifnum\pgfplotstablerow=\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi
},
},
%
columns/three/.style={% <---style column "three"
column name=III,
string type, column type/.add={}{|},%
postproc cell content/.append code={% <---replaces last cell with "-"
\ifnum\pgfplotstablerow=\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi
},
},
%
columns/four/.style={% <---style column "four"
column name=IV,
string type, column type/.add={}{|},%
postproc cell content/.append code={% <---replaces every cell with "-" except of the last cell
\ifnum\pgfplotstablerow<\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi
},
},
%
columns/5/.style={% <---style column "5"
column name=V,%
column type/.add={}{|},%
assign cell content/.code={% <---first cell is put inside multirow by the length of \M hence the length of the table
\ifnum\pgfplotstablerow=0
\pgfkeyssetvalue{/pgfplots/table/@cell content}
{\multirow{\M}{*}{\pgfkeys{/pgf/number format/.cd, fixed, zerofill, precision=2}\pgfmathprintnumber{##1}}}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi},
},
%
%------------------TYPESETTING ROWS-------------------------------
every head row/.style={before row=\hline, after row=\hline\hline},%
every last row/.style={after row=\hline}%
]%
{data.csv}}
\end{table}
\lipsum[2]
\end{document}
输出如下所示:
如果我没有将新创建的列放入表中,则resizebox
其工作方式如下:
生成此输出的代码是:
\documentclass{article}
\usepackage{multirow}
\usepackage{pgfplotstable}
%Make rows higher:
\setlength\extrarowheight{1.4pt}
%GLOBAL FORMATING:
\pgfkeys{/pgf/number format/.cd,
use comma,
1000 sep={}}
\usepackage{lipsum}
\usepackage{filecontents}
\begin{filecontents}{data.csv}
Name,1,1_err,2,2_err,3,3_err,4,4_err,5
A,10.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,3.42
B,11.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
C,12.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
D,13.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
E,14.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
F,15.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
G,nan,nan,nan,nan,nan,nan,23.3,90.23,nan
\end{filecontents}
\begin{document}
\lipsum[1]
\begin{table}[!htb]
\centering
\caption{Some text describing the table}
\pgfplotstablegetrowsof{data.csv}
\pgfmathtruncatemacro{\N}{\pgfplotsretval-1}% <---"\N" equals the index of the last row
\pgfmathtruncatemacro{\M}{\pgfplotsretval}% <---"\M" equals the total number of rows
\resizebox*{\textwidth}{!}{\pgfplotstabletypeset[col sep=comma,
columns={Name, 1, 2, 3, 4, 5},% <---these columns will appear in the table
%
columns/Name/.style={% <---style column "Name"
string type,%
column type/.add={|}{|},%
},
%
columns/1/.style={% <---style column "1"
column name=I,%
column type/.add={}{|},%
postproc cell content/.append code={% <---replaces last cell with "-"
\ifnum\pgfplotstablerow=\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi
},
},
%
columns/2/.style={% <---style column "2"
column name=II,%
column type/.add={}{|},%
postproc cell content/.append code={% <---replaces last cell with "-"
\ifnum\pgfplotstablerow=\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi
},
},
%
columns/3/.style={% <---style column "3"
column name=III,%
column type/.add={}{|},%
postproc cell content/.append code={% <---replaces every cell with "-" except of the last cell
\ifnum\pgfplotstablerow=\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi
},
},
%
columns/4/.style={% <---style column "4"
column name=IV,%
column type/.add={}{|},%
postproc cell content/.append code={%
\ifnum\pgfplotstablerow<\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi
},
},
%
columns/5/.style={% <---style column "5"
column name=V,%
column type/.add={}{|},%
assign cell content/.code={% <---first cell is put inside multirow by the length of \M hence the length of the table
\ifnum\pgfplotstablerow=0
\pgfkeyssetvalue{/pgfplots/table/@cell content}
{\multirow{\M}{*}{\pgfkeys{/pgf/number format/.cd, fixed, zerofill, precision=2}\pgfmathprintnumber{##1}}}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi},
},
%
every head row/.style={before row=\hline, after row=\hline\hline},%
every last row/.style={after row=\hline}%
]%
{data.csv}}
\end{table}
\lipsum[2]
\end{document}
非常感谢帮助!
答案1
这是那些最小的事情会导致最令人失望的调试问题的情况之一。
无论如何,它需要更多的%
-cowbells(无论您在何处看到/.code
处理程序,请查看行尾。我添加了大约 10 个位置。最简单的方法是进行 diff 查看)。然后它就可以正常工作了
\documentclass{article}
\usepackage{multirow}
\usepackage{pgfplotstable}
%Make rows higher:
\setlength\extrarowheight{1.4pt}
%GLOBAL FORMATING:
\pgfkeys{/pgf/number format/.cd,
use comma,
1000 sep={}}
\usepackage{lipsum}
\usepackage{filecontents}
\begin{filecontents}{data.csv}
Name,1,1_err,2,2_err,3,3_err,4,4_err,5
A,10.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,3.42
B,11.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
C,12.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
D,13.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
E,14.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
F,15.23,3.23,23.32,13.23,0.000123,0.00000123,nan,nan,nan
G,nan,nan,nan,nan,nan,nan,23.3,90.23,nan
\end{filecontents}
\begin{document}
\lipsum[1]
\begin{table}[!htb]
\centering
\caption{Some text describing the table}
\pgfplotstablegetrowsof{data.csv}
\pgfmathtruncatemacro{\N}{\pgfplotsretval-1}% <---"\N" equals the index of the last row
\pgfmathtruncatemacro{\M}{\pgfplotsretval}% <---"\M" equals the total number of rows
\resizebox*{\textwidth}{!}{\pgfplotstabletypeset[col sep=comma,
%------------------CREATING NEW COLUMNS:-------------------------------
create on use/one/.style={% <---create and typeset new column "one" out of columns 1 and 1_err of the input file
create col/assign/.code={%
\edef\entry{\noexpand\pgfmathprintnumber[fixed, zerofill, precision=2]{\thisrow{1}}$\pm$\noexpand\pgfmathprintnumber[fixed, zerofill, precision=2]{\thisrow{1_err}}}%
\pgfkeyslet{/pgfplots/table/create col/next content}{\entry}%
},
},
create on use/two/.style={% <---create and typeset new column "two" out of columns 2 and 2_err of the input file
create col/assign/.code={%
\edef\entry{\noexpand\pgfmathprintnumber[fixed, zerofill, precision=2]{\thisrow{2}}$\pm$\noexpand\pgfmathprintnumber[fixed, zerofill, precision=2]{\thisrow{2_err}}}%
\pgfkeyslet{/pgfplots/table/create col/next content}{\entry}%
},
},
create on use/three/.style={% <---create and typeset new column "three" out of columns 3 and 3_err of the input file
create col/assign/.code={%
\edef\entry{\noexpand\pgfmathprintnumber[sci, zerofill, precision=2]{\thisrow{3}}$\pm$\noexpand\pgfmathprintnumber[sci, zerofill, precision=2]{\thisrow{3_err}}}%
\pgfkeyslet{/pgfplots/table/create col/next content}{\entry}%
},
},
create on use/four/.style={% <---create and typeset new column "four" out of columns 4 and 4_err of the input file
create col/assign/.code={%
\edef\entry{\noexpand\pgfmathprintnumber[fixed, zerofill, precision=1]{\thisrow{4}}$\pm$\noexpand\pgfmathprintnumber[fixed, zerofill, precision=1]{\thisrow{4_err}}}%
\pgfkeyslet{/pgfplots/table/create col/next content}{\entry}%
},
},
%
%------------------END CREATING NEW COLUMNS-------------------------------
columns={Name, one, two, three, four, 5},% <---these columns will appear in the table
%
%------------------TYPESETTING COLUMNS:-------------------------------
columns/Name/.style={% <---style column "Name"
string type,%
column type/.add={|}{|},%
},
%
columns/one/.style={% <---style column "one"
column name=I,
string type, column type/.add={}{|},%
postproc cell content/.append code={% <---replaces last cell with "-"
\ifnum\pgfplotstablerow=\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi%
},
},
%
columns/two/.style={% <---style column "two"
column name=II,
string type, column type/.add={}{|},%
postproc cell content/.append code={% <---replaces last cell with "-"
\ifnum\pgfplotstablerow=\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi%
},
},
%
columns/three/.style={% <---style column "three"
column name=III,
string type, column type/.add={}{|},%
postproc cell content/.append code={% <---replaces last cell with "-"
\ifnum\pgfplotstablerow=\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi%
},
},
%
columns/four/.style={% <---style column "four"
column name=IV,
string type, column type/.add={}{|},%
postproc cell content/.append code={% <---replaces every cell with "-" except of the last cell
\ifnum\pgfplotstablerow<\N%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}\fi%
},
},
%
columns/5/.style={% <---style column "5"
column name=V,%
column type/.add={}{|},%
assign cell content/.code={% <---first cell is put inside multirow by the length of \M hence the length of the table
\ifnum\pgfplotstablerow=0%
\pgfkeyssetvalue{/pgfplots/table/@cell content}%
{\multirow{\M}{*}{\pgfkeys{/pgf/number format/.cd, fixed, zerofill, precision=2}\pgfmathprintnumber{##1}}}%
\else%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi},
},
%
%------------------TYPESETTING ROWS-------------------------------
every head row/.style={before row=\hline, after row=\hline\hline},%
every last row/.style={after row=\hline}%
]%
{data.csv}}
\end{table}
\lipsum[2]
\end{document}