这是对精彩问答的后续这里。我为此苦苦挣扎了很久,非常感谢您的帮助。假设我有两个相关但规模不同的数据集。为它们制作两个表格是不合理的,但将数据集放在同一个置信区间图中也是行不通的。理想的解决方案是将数据显示在一个表格中,并带有如下子标题:
然而,我在尝试对齐这些表格时遇到了麻烦。理想的做法是让它们dec sep align
对齐,但使用我目前基于以下方法无法做到这一点这个问题。我认为我可以通过直接将数据输入tabular
环境来获得更好的结果,但这样我就会失去@Jake 的天才动态解决方案。有人知道如何让这些表格统一或对齐,以便它们看起来漂亮且足够专业,适合科学出版物吗?
这是我用来生成上表的修改后的代码:
\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{xspace}
\usepackage{tabularx}
%% The data files
\begin{filecontents}{dataA.txt}
name z p mean lci uci
{\textbf{Variable A}} {} {} {} {} {}
Afear -0.96 0.33 -0.42 -1.28 0.44
Anofear 0.09 0.93 0.04 -0.85 0.94
B+2 0.29 0.78 0.10 -0.59 0.79
B+1 0.84 0.40 0.30 -0.40 1.00
B1:1 2.19 0.03 0.80 0.08 1.52
B-1 1.02 0.31 0.37 -0.33 1.07
B-2 -0.10 0.92 -0.03 -0.72 0.65
C+2 -1.11 0.27 -0.30 -0.83 0.23
C+1 1.15 0.25 0.32 -0.22 0.86
C1:1 -1.34 0.18 -0.38 -0.93 0.17
C-1 0.43 0.67 0.12 -0.42 0.66
\end{filecontents}
\begin{filecontents}{dataB.txt}
name z p mean lci uci
{\textbf{Variable B}} {} {} {} {} {}
C-2 -0.37 0.71 -10 -63 43
D+2 0.41 0.68 12 -44 67
D+1 -0.69 0.49 -20 -77 37
D1:1 -1.33 0.18 -39 -97 19
D-1 -1.21 0.23 -35 -92 22
D-2 0.32 0.75 9 -46 65
E+2 -1.89 0.06 -53 -109 2
E+1 0.78 0.44 23 -34 79
E1:1 0.62 0.53 18 -39 76
E-1 0.17 0.86 5 -52 62
E-2 0.06 0.95 2 -54 57
\end{filecontents}
% Read data files, create new column ``upper CI boundary - mean''
\pgfplotstableread{dataA.txt}\dataA
\pgfplotstableset{create on use/error/.style={
create col/expr={\thisrow{uci}-\thisrow{mean}
}
}
}
\pgfplotstableread{dataB.txt}\dataB
\pgfplotstableset{create on use/error/.style={
create col/expr={\thisrow{uci}-\thisrow{mean}
}
}
}
% Define the command for the plot (I had to do this separately for each dataset)
\newcommand{\errplotA}{%
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[y=-\baselineskip,
scale only axis,
width=6.5cm,
enlarge y limits={abs=0.5},
axis y line*=middle,
y axis line style=dashed,
ytick=\empty,
axis x line*=bottom]
%
% ``mean'' must be present in the datafile, ``error'' is the newly generated column
\addplot+[only marks][error bars/.cd,x dir=both, x explicit]
table [x=mean,y expr=\coordindex,x error=error]{\dataA};
\end{axis}
\end{tikzpicture}%
}
% Exactly the same than above but different dataset
\newcommand{\errplotB}{%
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[y=-\baselineskip,
scale only axis,
width=6.5cm,
enlarge y limits={abs=0.5},
axis y line*=middle,
y axis line style=dashed,
ytick=\empty,
axis x line*=bottom]
%
% ``mean'' must be present in the datafile, ``error'' is the newly generated column
\addplot+[only marks][error bars/.cd,x dir=both, x explicit]
table [x=mean,y expr=\coordindex,x error=error]{\dataB};
\end{axis}
\end{tikzpicture}%
}
\begin{document}
% Get number of rows in datafile
\pgfplotstablegetrowsof{\dataA}
\let\numberofrows=\pgfplotsretval
\begin{table}
\caption{Table caption.}
% Print the table
\pgfplotstabletypeset[column type=,
begin table={\begin{tabularx}{\textwidth}{l X c c c}},
end table={\end{tabularx}},
columns={name,mean,error,z,p},
% Booktabs rules
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=[1ex]},
% Set header name
columns/name/.style={string type,column name={}},
% Use the ``error'' column to call the \errplot command in a multirow cell in the first row, keep empty for all other rows
columns/error/.style={
column name={$\bar{x} \pm$ 95\% CI},
assign cell content/.code={% use \multirow for Z column:
\ifnum\pgfplotstablerow=1
\pgfkeyssetvalue{/pgfplots/table/@cell content}
{\multirow{\numberofrows}{6.5cm}{\errplotA}}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
}
},
% Format numbers and titles
columns/mean/.style={column name=$\bar{x}$,fixed,fixed zerofill},
columns/z/.style={column name=$z$,fixed,fixed zerofill},
columns/p/.style={column name=$p$,fixed,fixed zerofill},
]{\dataA}
% Second table
% Get number of rows in datafile
\pgfplotstablegetrowsof{\dataB}
\let\numberofrows=\pgfplotsretval
% Print the table
\pgfplotstabletypeset[column type=,
begin table={\begin{tabularx}{\textwidth}{l X c c c}},
end table={\end{tabularx}},
columns={name,mean,error,z,p},
% Booktabs rules
every head row/.style={after row=[1ex]},
every last row/.style={after row=[3ex]\bottomrule},
% Set header name
columns/name/.style={string type,column name={}},
% Use the ``error'' column to call the \errplot command in a multirow cell in the first row, keep empty for all other rows
columns/error/.style={
column name={},
assign cell content/.code={% use \multirow for Z column:
\ifnum\pgfplotstablerow=1
\pgfkeyssetvalue{/pgfplots/table/@cell content}
{\multirow{\numberofrows}{6.5cm}{\errplotB}}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
}
},
% Format numbers and titles
columns/mean/.style={column name=,fixed,fixed zerofill},
columns/z/.style={column name=,fixed,fixed zerofill},
columns/p/.style={column name=,fixed,fixed zerofill},
]{\dataB}
\end{table}
\end{document}
答案1
在这种情况下,我建议使用包S
提供的列类型siunitx
来处理对齐。您可以通过将列类型设置为来指定对齐的数字格式S[table-format=-2.2]
,这指示siunitx
假设列中的数字有一个符号,并且在小数点符号前后各有两位数字:
\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{xspace}
\usepackage{tabularx}
\usepackage{siunitx}
%% The data files
\begin{filecontents}{dataA.txt}
name z p mean lci uci
{\textbf{Variable A}} {} {} {} {} {}
Afear -0.96 0.33 -0.42 -1.28 0.44
Anofear 0.09 0.93 0.04 -0.85 0.94
B+2 0.29 0.78 0.10 -0.59 0.79
B+1 0.84 0.40 0.30 -0.40 1.00
B1:1 2.19 0.03 0.80 0.08 1.52
B-1 1.02 0.31 0.37 -0.33 1.07
B-2 -0.10 0.92 -0.03 -0.72 0.65
C+2 -1.11 0.27 -0.30 -0.83 0.23
C+1 1.15 0.25 0.32 -0.22 0.86
C1:1 -1.34 0.18 -0.38 -0.93 0.17
C-1 0.43 0.67 0.12 -0.42 0.66
\end{filecontents}
\begin{filecontents}{dataB.txt}
name z p mean lci uci
{\textbf{Variable B}} {} {} {} {} {}
C-2 -0.37 0.71 -10 -63 43
D+2 0.41 0.68 12 -44 67
D+1 -0.69 0.49 -20 -77 37
D1:1 -1.33 0.18 -39 -97 19
D-1 -1.21 0.23 -35 -92 22
D-2 0.32 0.75 9 -46 65
E+2 -1.89 0.06 -53 -109 2
E+1 0.78 0.44 23 -34 79
E1:1 0.62 0.53 18 -39 76
E-1 0.17 0.86 5 -52 62
E-2 0.06 0.95 2 -54 57
\end{filecontents}
% Read data files, create new column ``upper CI boundary - mean''
\pgfplotstableread{dataA.txt}\dataA
\pgfplotstableset{create on use/error/.style={
create col/expr={\thisrow{uci}-\thisrow{mean}
}
}
}
\pgfplotstableread{dataB.txt}\dataB
\pgfplotstableset{create on use/error/.style={
create col/expr={\thisrow{uci}-\thisrow{mean}
}
}
}
% Define the command for the plot (I had to do this separately for each dataset)
\newcommand{\errplot}[1]{%
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[y=-\baselineskip,
scale only axis,
width=6.5cm,
enlarge y limits={abs=0.5},
axis y line*=middle,
y axis line style=dashed,
ytick=\empty,
axis x line*=bottom]
%
% ``mean'' must be present in the datafile, ``error'' is the newly generated column
\addplot+[only marks][error bars/.cd,x dir=both, x explicit]
table [x=mean,y expr=\coordindex,x error=error]{#1};
\end{axis}
\end{tikzpicture}%
}
\begin{document}
% Get number of rows in datafile
\pgfplotstablegetrowsof{\dataA}
\let\numberofrows=\pgfplotsretval
\begin{table}
\caption{Table caption.}
% Print the table
\pgfplotstabletypeset[
columns={name,mean,error,z,p},
% Booktabs rules
every head row/.style={before row=\toprule, after row=\midrule},
every last row/.style={after row=[1ex]},
% Set header name
columns/name/.style={string type,column name={}},
% Use the ``error'' column to call the \errplot command in a multirow cell in the first row, keep empty for all other rows
columns/error/.style={
column name={$\bar{x} \pm$ 95\% CI},
assign cell content/.code={% use \multirow for Z column:
\ifnum\pgfplotstablerow=1
\pgfkeyssetvalue{/pgfplots/table/@cell content}
{\multirow{\numberofrows}{6.5cm}{\errplot{\dataA}}}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
}
},
% Format numbers and titles
columns/mean/.style={column name=$\bar{x}$, string type, column type={S[table-format=-2.2]}},
columns/z/.style={column name=$z$, string type, column type={S[table-format=-1.2]}},
columns/p/.style={column name=$p$, string type, column type={S[table-format=1.2]}},
]{\dataA}
\pgfplotstabletypeset[
columns={name,mean,error,z,p},
every head row/.style={output empty row, after row=\\},
every last row/.style={after row=[3ex]\bottomrule},
% Set header name
columns/name/.style={string type,column name={}},
% Use the ``error'' column to call the \errplot command in a multirow cell in the first row, keep empty for all other rows
columns/error/.style={
column name={$\bar{x} \pm$ 95\% CI},
assign cell content/.code={% use \multirow for Z column:
\ifnum\pgfplotstablerow=1
\pgfkeyssetvalue{/pgfplots/table/@cell content}
{\multirow{\numberofrows}{6.5cm}{\errplot{\dataB}}}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
}
},
% Format numbers and titles
columns/mean/.style={column name=$\bar{x}$, string type, column type={S[table-format=-2.2]}},
columns/z/.style={column name=$z$, string type, column type={S[table-format=-1.2]}},
columns/p/.style={column name=$p$, string type, column type={S[table-format=1.2]}},
]{\dataB}
\end{table}
\end{document}