向表中添加列

向表中添加列

我有一个使用以下代码生成的表格,我想扩展它,添加另一列,其中包含 {"Group1" 和 "Group2"} 的名称,这样,在这些数据的左侧,它将显示 "Condition1"、"Condition2"、"Condition3"。也就是说,每个条件都有两行,一行包含 "Group1",另一行包含 "Group2" 数据。我是一个新手 LaTeX:er,用 LaTeX 生成的表格非常棒。虽然,开始使用它很难。那么,我该如何回答这个问题呢?有人建议使用例如:

 \begin{tabular}{rlrrrrr}
 \end{tabular}%

 \begin{tabular}{r} % here is the added column 

 \end{tabular}

尽管如此,在上面的内容中,它并不像我的具有许多不同级别的表那样复杂。

 \documentclass{article}
 \usepackage{booktabs}

 \usepackage{caption}                      
 \captionsetup[table]{singlelinecheck=false}

 \usepackage{array}
 \usepackage{changepage} % remove?
 \newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
 \newcommand{\hf}[2]{%HeaderFormat
   \multicolumn{#1}{c}{#2}%
 }
 \begin{document}
 \SweaveOpts{concordance=TRUE}
 \begin{table*}
 \centering
 \ra{1.3}
 \begin{adjustwidth}{-2cm}{}
 \setlength{\tabcolsep}{4pt}
 \setcounter{table}{0} % This makes the caption title 'Table 1:'
 \caption{Mean (sd) before}
 \begin{tabular}{%
   @{}>{}l% Remove space from edge, Set first column in italice
   *{4}{% 4 columns of the following
     r@{\,}% right column for the mean value, and set the space between the two numbers to be exactly `\,`-space
>{(}r<{)}% right column for the standard deviation, add paratheses before and after
}%
   @{}l@{\hspace{2em}}% Need to control the space before and after the column with @{}, so that no `\tabcolsep` is inserted, and then we add the exact space we need.
   *{4}{r@{\,}>{(}r<{)}}% Same as above
   }

 \toprule
 & \hf{8}{Obj1} &&\hf{8}{Obj2}\\

 \cmidrule{2-9} \cmidrule{11-18}
 & \hf{4}{Objectx} & \hf{4}{Objecty} && \hf{4}{Objectx} & \hf{4}{Objecty}\\
 \midrule
 &\hf{2}{CSX} & \hf{2}{CSY} & \hf{2}{CSX} &  \hf{2}{CSY} && \hf{2}{CSX} & \hf{2}{CSY} & \hf{2}{CSX} & \hf{2}{CSY}\\
 \midrule
 Group1 &0.50&0.23 & 0.55&0.25 & 0.34&0.16 & 0.37&0.20 && 0.33&0.18 & 0.38&0.16 & 0.27&0.17 & 0.23&0.17 \\
 Group2 &0.43&0.17 & 0.44&0.14 & 0.39&0.18 & 0.35&0.15 && 0.32&0.20 & 0.31&0.18 & 0.25&0.19 & 0.26&0.22 \\
 Group1 &0.50&0.23 & 0.55&0.25 & 0.34&0.16 & 0.37&0.20 && 0.33&0.18 & 0.38&0.16 & 0.27&0.17 & 0.23&0.17 \\
 Group2 &0.43&0.17 & 0.44&0.14 & 0.39&0.18 & 0.35&0.15 && 0.32&0.20 & 0.31&0.18 & 0.25&0.19 & 0.26&0.22 \\
 Group1 &0.50&0.23 & 0.55&0.25 & 0.34&0.16 & 0.37&0.20 && 0.33&0.18 & 0.38&0.16 & 0.27&0.17 & 0.23&0.17 \\
 Group2 &0.43&0.17 & 0.44&0.14 & 0.39&0.18 & 0.35&0.15 && 0.32&0.20 & 0.31&0.18 & 0.25&0.19 & 0.26&0.22 \\
 \bottomrule
 \end{tabular}
 \end{adjustwidth}
 \end{table*}
 \end{document}

答案1

将其放在\begin{document}

\usepackage{multirow}
\newcommand{\cn}[1]{\multirow{2}{*}{#1}}
\usepackage{calc}
\newlength\midrulecompensation
\setlength\midrulecompensation{\aboverulesep+\belowrulesep+\lightrulewidth}

并将其放在表格之前(确保中间没有空格):

 \begin{tabular}{%
   @{}l % Remove space from edge
   }%
   \toprule
   \\[\midrulecompensation]
   \\
   \midrule
   \\
   \midrule
   \cn{Condition1} \\
   \\[\midrulecompensation]
   \cn{Condition 2} \\
   \\
   \cn{Condition 3} \\
   \\[\midrulecompensation] 
   \bottomrule
 \end{tabular}%

\midrulecompensation为了弥补\midrule在另一个表格中占用的空间,否则它们就不会排队。

答案2

不确定为什么要添加单独的tabular而不是编辑现有的。您需要l在开头添加一个新列,&在每行的开头添加一个,将 s 中的列号增加\cmidrule1,然后添加Condition N相应的行。

我还减少了中间(Obj“组”之间)的空间,并\small减小了字体大小,因为表格相当宽。

 \documentclass{article}
 \usepackage{booktabs}

 \usepackage{caption}                      
 \captionsetup[table]{singlelinecheck=false}

 \usepackage{array}
 \usepackage{changepage} % remove?
 \newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
 \newcommand{\hf}[2]{%HeaderFormat
   \multicolumn{#1}{c}{#2}%
 }

 \begin{document}
% \SweaveOpts{concordance=TRUE}

 \begin{table*}
 \centering\small % <-- I addded \small
 \ra{1.3}
 \begin{adjustwidth}{-2cm}{}
 \setlength{\tabcolsep}{4pt}
 \setcounter{table}{0} % This makes the caption title 'Table 1:'
 \caption{Mean (sd) before}
 \begin{tabular}{%
   @{}l>{}l% Remove space from edge, Set first column in italice
   *{4}{% 4 columns of the following
     r@{\,}% right column for the mean value, and set the space between the two numbers to be exactly `\,`-space
>{(}r<{)}% right column for the standard deviation, add paratheses before and after
}%
   @{}l@{\hspace{1em}}% Need to control the space before and after the column with @{}, so that no `\tabcolsep` is inserted, and then we add the exact space we need.
   *{4}{r@{\,}>{(}r<{)}}% Same as above
   }

 \toprule
 && \hf{8}{Obj1} &&\hf{8}{Obj2}\\

 \cmidrule{3-10} \cmidrule{12-19}
 && \hf{4}{Objectx} & \hf{4}{Objecty} && \hf{4}{Objectx} & \hf{4}{Objecty}\\
 \midrule
 &&\hf{2}{CSX} & \hf{2}{CSY} & \hf{2}{CSX} &  \hf{2}{CSY} && \hf{2}{CSX} & \hf{2}{CSY} & \hf{2}{CSX} & \hf{2}{CSY}\\
 \midrule
Condition 1 & Group1 &0.50&0.23 & 0.55&0.25 & 0.34&0.16 & 0.37&0.20 && 0.33&0.18 & 0.38&0.16 & 0.27&0.17 & 0.23&0.17 \\
 & Group2 &0.43&0.17 & 0.44&0.14 & 0.39&0.18 & 0.35&0.15 && 0.32&0.20 & 0.31&0.18 & 0.25&0.19 & 0.26&0.22 \\
Condition 2 & Group1 &0.50&0.23 & 0.55&0.25 & 0.34&0.16 & 0.37&0.20 && 0.33&0.18 & 0.38&0.16 & 0.27&0.17 & 0.23&0.17 \\
 & Group2 &0.43&0.17 & 0.44&0.14 & 0.39&0.18 & 0.35&0.15 && 0.32&0.20 & 0.31&0.18 & 0.25&0.19 & 0.26&0.22 \\
Condition 3 & Group1 &0.50&0.23 & 0.55&0.25 & 0.34&0.16 & 0.37&0.20 && 0.33&0.18 & 0.38&0.16 & 0.27&0.17 & 0.23&0.17 \\
 & Group2 &0.43&0.17 & 0.44&0.14 & 0.39&0.18 & 0.35&0.15 && 0.32&0.20 & 0.31&0.18 & 0.25&0.19 & 0.26&0.22 \\
 \bottomrule
 \end{tabular}
 \end{adjustwidth}
 \end{table*}
 \end{document}

或者,如果您想Condition N位于两行单元格的中间,请添加\usepackage{multirow},并使用\multirow{2}{*}{Condition 1}而不是Condition 1

 \documentclass{article}
 \usepackage{booktabs}

 \usepackage{caption}                      
 \captionsetup[table]{singlelinecheck=false}

 \usepackage{array}
 \usepackage{changepage} % remove?
 \newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
 \newcommand{\hf}[2]{%HeaderFormat
   \multicolumn{#1}{c}{#2}%
 }
 \usepackage{multirow}

 \begin{document}
% \SweaveOpts{concordance=TRUE}

 \begin{table*}
 \centering\small % <-- I addded \small
 \ra{1.3}
 \begin{adjustwidth}{-2cm}{}
 \setlength{\tabcolsep}{4pt}
 \setcounter{table}{0} % This makes the caption title 'Table 1:'
 \caption{Mean (sd) before}
 \begin{tabular}{%
   @{}l>{}l% Remove space from edge, Set first column in italice
   *{4}{% 4 columns of the following
     r@{\,}% right column for the mean value, and set the space between the two numbers to be exactly `\,`-space
>{(}r<{)}% right column for the standard deviation, add paratheses before and after
}%
   @{}l@{\hspace{1em}}% Need to control the space before and after the column with @{}, so that no `\tabcolsep` is inserted, and then we add the exact space we need.
   *{4}{r@{\,}>{(}r<{)}}% Same as above
   }

 \toprule
 && \hf{8}{Obj1} &&\hf{8}{Obj2}\\

 \cmidrule{3-10} \cmidrule{12-19}
 && \hf{4}{Objectx} & \hf{4}{Objecty} && \hf{4}{Objectx} & \hf{4}{Objecty}\\
 \midrule
 &&\hf{2}{CSX} & \hf{2}{CSY} & \hf{2}{CSX} &  \hf{2}{CSY} && \hf{2}{CSX} & \hf{2}{CSY} & \hf{2}{CSX} & \hf{2}{CSY}\\
 \midrule
\multirow{2}{*}{Condition 1} & Group1 &0.50&0.23 & 0.55&0.25 & 0.34&0.16 & 0.37&0.20 && 0.33&0.18 & 0.38&0.16 & 0.27&0.17 & 0.23&0.17 \\
 & Group2 &0.43&0.17 & 0.44&0.14 & 0.39&0.18 & 0.35&0.15 && 0.32&0.20 & 0.31&0.18 & 0.25&0.19 & 0.26&0.22 \\
\multirow{2}{*}{Condition 2} & Group1 &0.50&0.23 & 0.55&0.25 & 0.34&0.16 & 0.37&0.20 && 0.33&0.18 & 0.38&0.16 & 0.27&0.17 & 0.23&0.17 \\
 & Group2 &0.43&0.17 & 0.44&0.14 & 0.39&0.18 & 0.35&0.15 && 0.32&0.20 & 0.31&0.18 & 0.25&0.19 & 0.26&0.22 \\
\multirow{2}{*}{Condition 3} & Group1 &0.50&0.23 & 0.55&0.25 & 0.34&0.16 & 0.37&0.20 && 0.33&0.18 & 0.38&0.16 & 0.27&0.17 & 0.23&0.17 \\
 & Group2 &0.43&0.17 & 0.44&0.14 & 0.39&0.18 & 0.35&0.15 && 0.32&0.20 & 0.31&0.18 & 0.25&0.19 & 0.26&0.22 \\
 \bottomrule
 \end{tabular}
 \end{adjustwidth}
 \end{table*}
 \end{document}

相关内容