如何创建具有多列的表?

如何创建具有多列的表?

我是 Latex 世界的新手。有没有办法制作如下表格?

在此处输入图片描述

我尝试使用以下代码利用和multirow遵循此参考的软件包siunitxbooktabshttps://tex.stackexchange.com/a/174328/233121但结果并不如预期。

\documentclass[11pt]{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{multirow}


\begin{document}

\begin{table}[ht]
\begin{tabular}{
  l
  *{6}{S[table-format=1.4]}
  S[table-format=5]
  S[table-format=3.2]
}
\toprule
\multicolumn{1}{c|}{\textbf{Program}} &
\multicolumn{1}{c|}{\textbf{Problem Size}} &
\multicolumn{4}{c|}{\textbf{Instructions (Billions)}}  &
\multicolumn{3}{c}{\textbf{Synchronization Primitives}} \\
\cmidrule(lr){3-6} \cmidrule(lr){7-9}
&& {\textbf{Total}} & {\textbf{FLOPS}} & {\textbf{Reads}} & {\textbf{Writes}} & {\textbf{Locks}} & {\textbf{Barriers}} & {\textbf{Conditions}} \\
\midrule
\texttt{blackscholes}  & 65,536 options & 2.67 & 1.14 & 0.68 & 0.19 & 0 & 8 & 0 \\
\texttt{freqmine} & 990,000 transactions & 33.45 & 0.00 & 11.31 & 5.24 & 990,025 & 0 & 0 \\
\texttt{swaptions}  & 64 swaptions, 20,000 simulations & 14.11 & 2.62 & 5.08 & 1.16 & 23 & 0 & 0 \\
\texttt{bodytrack} & 4 frames, 4,000 particles & 14.03 & 4.22 & 3.63 & 0.95 & 114,621 & 619 & 2,042 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

我想找到一个解决方案来自动换行过多的文本但却想不出来。

答案1

这是制作该表格的一种方法(以横向模式,否则它太宽)。

\documentclass[landscape]{article}
\usepackage{geometry}
\usepackage{nicematrix}
\usepackage{siunitx}

\begin{document}

\begin{table}[ht]
\setlength{\tabcolsep}{5pt}
\begin{NiceTabular}{ll*{5}{S}S[table-format=4.0]S}[hvlines]
\Block{2-1}{\textbf{Program}} &
\Block{2-1}{\textbf{Problem Size}} &
{\Block{1-4}{\textbf{Instructions (Billions)}}}  &&&&
{\Block{1-3}{\textbf{Synchronization Primitives}}} \\
&& {\textbf{Total}} & {\textbf{FLOPS}} & {\textbf{Reads}} & {\textbf{Writes}} & {\textbf{Locks}} & {\textbf{Barriers}} & {\textbf{Conditions}} \\
\texttt{blackscholes}  & 65,536 options & 2.67 & 1.14 & 0.68 & 0.19 & 0 & 8 & 0 \\
\texttt{freqmine} & 990,000 transactions & 33.45 & 0.00 & 11.31 & 5.24 & 990,025 & 0 & 0 \\
\texttt{swaptions}  & 64 swaptions, 20,000 simulations & 14.11 & 2.62 & 5.08 & 1.16 & 23 & 0 & 0 \\
\texttt{bodytrack} & 4 frames, 4,000 particles & 14.03 & 4.22 & 3.63 & 0.95 & 114,621 & 619 & 2,042 \\
\end{NiceTabular}
\end{table}

\end{document}

您需要多次编译。

第一个代码的输出

如果只使用以下水平规则,输出效果肯定会更好bookmarks

\documentclass[landscape]{article}
\usepackage{geometry}
\usepackage{nicematrix}
\usepackage{siunitx}
\usepackage{booktabs}

\begin{document}

\begin{table}[ht]
\setlength{\tabcolsep}{5pt}
\begin{NiceTabular}{@{}ll*{5}{S}S[table-format=4.0]S@{}}
\toprule
\Block{2-1}{\textbf{Program}} &
\Block{2-1}{\textbf{Problem Size}} &
{\Block{1-4}{\textbf{Instructions (Billions)}}}  &&&&
{\Block{1-3}{\textbf{Synchronization Primitives}}} \\
\cmidrule(lr){3-6} \cmidrule(l){7-9}
&& {\textbf{Total}} & {\textbf{FLOPS}} & {\textbf{Reads}} & {\textbf{Writes}} & {\textbf{Locks}} &
{\textbf{Barriers}} & {\textbf{Conditions}} \\
\midrule
\texttt{blackscholes}  & 65,536 options & 2.67 & 1.14 & 0.68 & 0.19 & 0 & 8 & 0 \\
\texttt{freqmine} & 990,000 transactions & 33.45 & 0.00 & 11.31 & 5.24 & 990,025 & 0 & 0 \\
\texttt{swaptions}  & 64 swaptions, 20,000 simulations & 14.11 & 2.62 & 5.08 & 1.16 & 23 & 0 & 0 \\
\texttt{bodytrack} & 4 frames, 4,000 particles & 14.03 & 4.22 & 3.63 & 0.95 & 114,621 & 619 & 2,042 \\
\bottomrule
\end{NiceTabular}
\end{table}

\end{document}

第二段代码的输出

答案2

  • 您的表格太宽,无法放入页面的文本区域
  • 尽可能的解决方案是将第二列中的文本拆分为多行
  • 也将有助于使用较小的字体并减小尺寸\tabcolsep
\documentclass[10pt,twoside,openright,english,italian]{book}
\usepackage{geometry}  % added
\usepackage{siunitx}
\usepackage{booktabs, multirow, 
            tabularx} % added 
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\NewExpandableDocumentCommand\mcc{O{1}m}{\multicolumn{#1}{c}{#2}}

\begin{document}
    \begin{table}[ht]
    \centering
    \setlength\tabcolsep{3pt}
    \small
\begin{tabularx}{\linewidth}{@{} 
  l L
    S[table-format=2.2]
    S[table-format=1.2]
    S[table-format=2.2]
    S[table-format=1.2]
%
    S[table-format=3.3]
    S[table-format=3.0]
    S[table-format=1.3]
                @{}}
    \toprule
\textbf{Program}    
    & \textbf{Problem Size}
        & \mcc[4]{\textbf{Instructions (Billions)}}  
            & \mcc[3]{\textbf{Synchronization Primitives}} \\
    \cmidrule(lr){3-6} \cmidrule(lr){7-9}
    &   & {\textbf{Total}} 
            & {\textbf{FLOPS}} 
                & {\textbf{Reads}} 
                    & {\textbf{Writes}} 
                        & {\textbf{Locks}} 
                            & {\textbf{Barriers}} 
                                & {\textbf{Conditions}} \\
\midrule
\texttt{blackscholes}  & 65,536 options & 2.67 & 1.14 & 0.68 & 0.19 & 0 & 8 & 0 \\
\texttt{freqmine} & 990,000 transactions & 33.45 & 0.00 & 11.31 & 5.24 & 990,025 & 0 & 0 \\
\texttt{swaptions}  & 64 swaptions, 20,000 simulations & 14.11 & 2.62 & 5.08 & 1.16 & 23 & 0 & 0 \\
\texttt{bodytrack} & 4 frames, 4,000 particles & 14.03 & 4.22 & 3.63 & 0.95 & 114,621 & 619 & 2,042 \\
\bottomrule
\end{tabularx}
    \end{table}
\end{document}

在此处输入图片描述

附录:X使用列 的地方有小的变化\renewcommand\tabularxcolumn[1]{m{#1}},并且扩大了表格的拉伸:

\documentclass[10pt,twoside,openright,english,italian]{book}
\usepackage{geometry}
\usepackage{siunitx}
\usepackage{booktabs, multirow, tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\NewExpandableDocumentCommand\mcc{O{1}m}{\multicolumn{#1}{c}{#2}}

\begin{document}
    \begin{table}[ht]
    \centering
    \setlength\tabcolsep{3pt}
    \renewcommand\arraystretch{1.2}
    \renewcommand\tabularxcolumn[1]{m{#1}}
    \small
\begin{tabularx}{\linewidth}{@{} 
  l L
    S[table-format=2.2]
    S[table-format=1.2]
    S[table-format=2.2]
    S[table-format=1.2]
%
    S[table-format=3.3]
    S[table-format=3.0]
    S[table-format=1.3]
                @{}}
    \toprule
\textbf{Program}    
    & \textbf{Problem Size}
        & \mcc[4]{\textbf{Instructions (Billions)}}  
            & \mcc[3]{\textbf{Synchronization Primitives}} \\
    \cmidrule(lr){3-6} \cmidrule(lr){7-9}
    &   & {\textbf{Total}} 
            & {\textbf{FLOPS}} 
                & {\textbf{Reads}} 
                    & {\textbf{Writes}} 
                        & {\textbf{Locks}} 
                            & {\textbf{Barriers}} 
                                & {\textbf{Conditions}} \\
    \midrule
\texttt{blackscholes}  & 65,536 options & 2.67 & 1.14 & 0.68 & 0.19 & 0 & 8 & 0 \\
\texttt{freqmine} & 990,000 transactions & 33.45 & 0.00 & 11.31 & 5.24 & 990,025 & 0 & 0 \\
\texttt{swaptions}  & 64 swaptions, \newline
                      20,000 simulations & 14.11 & 2.62 & 5.08 & 1.16 & 23 & 0 & 0 \\
\texttt{bodytrack} & 4 frames, \newline
                     4,000 particles & 14.03 & 4.22 & 3.63 & 0.95 & 114,621 & 619 & 2,042 \\
    \bottomrule
\end{tabularx}
    \end{table}
\end{document}

在此处输入图片描述

相关内容