我创建了下表。
我需要将“文本”、“此处相同”和“我希望垂直对齐”行垂直对齐。我搜索过类似的问题,并使用了高度可变的表格单元格中的垂直对齐和如何使单元格文本垂直居中?,但他们的解决方案分裂了我的表格。请告诉我我错在哪里。我的 MWE 如下所示:
\documentclass[12pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage[font=scriptsize,
labelfont=bf]{caption}
\usepackage[xcdraw]{xcolor}
\usepackage{tabularray}
\usepackage{pdflscape}
\usepackage{lipsum}
\UseTblrLibrary{siunitx}
\begin{document}
\begin{table}[]
\begin{tabular}{cccccccccccc}
\hline
\multicolumn{4}{|c|}{I want this to align vertically}
& \multicolumn{4}{c|}{I want this to align vertically}
& \multicolumn{4}{c|}{I want this to align vertically}
\\ [20pt] \hline
\multicolumn{4}{|c|}{Text}
& \multicolumn{4}{c|}{Text}
& \multicolumn{4}{c|}{Text}
\\ [15 pt]\hline
\multicolumn{1}{|c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Same\\ here\end{tabular}}} &
\multicolumn{1}{c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Same\\ here\end{tabular}}} &
\multicolumn{2}{c|}{\begin{tabular}[c]{@{}c@{}}Ambient\\ Conditions\end{tabular}} &
\multicolumn{1}{c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Vertically\\ align\end{tabular}}} &
\multicolumn{1}{c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Same\\ here\end{tabular}}} &
\multicolumn{2}{l|}{\begin{tabular}[c]{@{}l@{}}Ambient\\ Conditions\end{tabular}} &
\multicolumn{1}{c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Same\\ here\end{tabular}}} &
\multicolumn{1}{c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Same\\ here\end{tabular}}} &
\multicolumn{2}{c|}{\begin{tabular}[c]{@{}c@{}}Ambient\\ Conditions\end{tabular}} \\ \cline{3-
4} \cline{7-8} \cline{11-12}
\multicolumn{1}{|c|}{}
& \multicolumn{1}{c|}{}
& \multicolumn{1}{c|}{RH} & \multicolumn{1}{c|}{Temp} &
\multicolumn{1}{c|}{}
& \multicolumn{1}{c|}{}
& \multicolumn{1}{l|}{RH} & \multicolumn{1}{l|}{Temp} &
\multicolumn{1}{c|}{}
& \multicolumn{1}{c|}{}
& \multicolumn{1}{c|}{RH} & \multicolumn{1}{c|}{Temp} \\ \hline
\multicolumn{1}{|c|}{A}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} &
\multicolumn{1}{c|}{A}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} &
\multicolumn{1}{c|}{A}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} \\ \hline
\multicolumn{1}{|c|}{B}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} &
\multicolumn{1}{c|}{B}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} &
\multicolumn{1}{c|}{B}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} \\ \hline
\multicolumn{1}{|c|}{C}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} &
\multicolumn{1}{c|}{C}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} &
\multicolumn{1}{c|}{C}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} \\ \hline
\multicolumn{1}{|c|}{D}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} &
\multicolumn{1}{c|}{D}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} &
\multicolumn{1}{c|}{D}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} \\ \hline
\multicolumn{1}{|c|}{E}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} &
\multicolumn{1}{c|}{E}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} &
\multicolumn{1}{c|}{E}
& \multicolumn{1}{c|}{200}
& \multicolumn{1}{c|}{200} & \multicolumn{1}{c|}{200} \\ \hline
\multicolumn{1}{l}{}
& \multicolumn{1}{l}{}
& \multicolumn{1}{l}{} & \multicolumn{1}{l}{} &
\multicolumn{1}{l}{}
& \multicolumn{1}{l}{}
& \multicolumn{1}{l}{} & \multicolumn{1}{l}{} &
\multicolumn{1}{l}{}
& \multicolumn{1}{l}{}
& \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
\multicolumn{1}{l}{}
& \multicolumn{1}{l}{}
& \multicolumn{1}{l}{} & \multicolumn{1}{l}{} &
\multicolumn{1}{l}{}
& \multicolumn{1}{l}{}
& \multicolumn{1}{l}{} & \multicolumn{1}{l}{} &
\multicolumn{1}{l}{}
& \multicolumn{1}{l}{}
& \multicolumn{1}{l}{} & \multicolumn{1}{l}{}
\end{tabular}
\end{table}
\end{document}
我知道这可能是一个重复的问题,对此造成的不便深表歉意,但我对 Latex 还很陌生。提前致谢。
答案1
- 您的表格非常大:12 列!因此它超出了文本区域。
- 在序言中你提到了
tabularray
包,为什么不在设计这个表时使用它呢?这是一个功能强大的包!用它编写的代码非常简单(尽管这与其他(经典)表包中的表设置有所不同,并且需要花费一些精力来学习它) - 由于您加载了
tabularray
包并且已经使用它收到了两个答案(您接受了),我假设您至少对它有一点熟悉,因此我强烈建议您进一步使用它:
\documentclass[12pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage[font=scriptsize,
labelfont=bf]{caption}
\usepackage[xcdraw]{xcolor}
\usepackage{tabularray}
\usepackage{pdflscape}
\UseTblrLibrary{siunitx}
\usepackage{lipsum}
\begin{document}
\begin{table}[ht]
\begin{tblr}{width=\linewidth,
hlines, vlines,
colspec={*{12}{X[c,m]}},
colsep=4pt,
row{1}={ht=7ex},
row{2}={ht=5ex},
hspan=minimal
}
\SetCell[c=4]{c} I want this to align vertically
& & & & \SetCell[c=4]{c} I want this to align vertically
& & & & \SetCell[c=4]{c} I want this to align vertically
& & & \\
\SetCell[c=4]{c} Text
& & & & \SetCell[c=4]{c} Text
& & & & \SetCell[c=4]{c} Text
& & & \\
\SetCell[r=2]{c} {Same\\ here}
& \SetCell[r=2]{c} {Same\\ here}
& \SetCell[c=2]{c} {Ambient\\ Conditions}
& & \SetCell[r=2]{c} {Same\\ here}
& \SetCell[r=2]{c} {Same\\ here}
& \SetCell[c=2]{c} {Ambient\\ Conditions}
& & \SetCell[r=2]{c} {Same\\ here}
& \SetCell[r=2]{c} {Same\\ here}
& \SetCell[c=2]{c} {Ambient\\ Conditions}
& \\
& & RH & Temp & & & RH & Temp & & & RH & Temp \\
A & 200 & 200 & 200 & A & 200 & 200 & 200 & A & 200 & 200 & 200 \\
B & 200 & 200 & 200 & B & 200 & 200 & 200 & B & 200 & 200 & 200 \\
C & 200 & 200 & 200 & C & 200 & 200 & 200 & C & 200 & 200 & 200 \\
D & 200 & 200 & 200 & D & 200 & 200 & 200 & D & 200 & 200 & 200 \\
E & 200 & 200 & 200 & E & 200 & 200 & 200 & E & 200 & 200 & 200 \\
\end{tblr}
\end{table}
\end{document}
笔记,我会考虑在表格中使用\small
字体大小。可以通过在\small
之前添加 来确定\begin{tblr}
。
编辑:改进了表格规范并增加了更多解释。
答案2
像这样?请注意,我使用了两种不同的策略来强制两行或三行垂直居中:(a)插入印刷支柱和(b)使用\multirow
(带有非整数参数)。
我还尝试清理 tablegenerator.com 中最糟糕的编码过剩问题。表格的 LaTeX 代码仍然不太好。特别是,代码仍然没有尝试确保表格实际上适合文本块的宽度。(对于手头的表格,同时执行\setlength\tabcolsep{3pt}
- 默认值6pt
- 以及\small
after\begin{table}
和 before\begin{tabular}
应该可以使表格适合文本块。)
\documentclass[12pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage[font=scriptsize,labelfont=bf]{caption}
\usepackage[xcdraw]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}
\usepackage{pdflscape}
\usepackage{multirow}
% define a typographic strut:
\newcommand\mystrut{\rule[-15pt]{0pt}{38pt}} % choose depth and height suitably
% handy shortcut macro:
\newcommand\mytab[1]{\begin{tabular}{@{}c@{}} #1 \end{tabular}}
\begin{document}
\begin{table}
\begin{tabular}{ | *{12}{c|} }
\hline
\multicolumn{4}{|c|}{I want this to align vertically \mystrut }
& \multicolumn{4}{c|}{I want this to align vertically}
& \multicolumn{4}{c|}{I want this to align vertically}
\\ \hline
\multicolumn{4}{|c|}{Text \mystrut }
& \multicolumn{4}{c|}{Text}
& \multicolumn{4}{c|}{Text}
\\ \hline
\multirow{2.2}{*}{\mytab{Same\\ here}} &
\multirow{2.2}{*}{\mytab{Same\\ here}} &
\multicolumn{2}{c|}{\mytab{Ambient\\ Conditions}} &
\multirow{2.2}{*}{\mytab{Vertically\\ align}} &
\multirow{2.2}{*}{\mytab{Same\\ here}} &
\multicolumn{2}{l|}{\mytab{Ambient\\ Conditions}} &
\multirow{2.2}{*}{\mytab{Same\\ here}} &
\multirow{2.2}{*}{\mytab{Same\\ here}} &
\multicolumn{2}{c|}{\mytab{Ambient\\ Conditions}} \\
\cline{3-4} \cline{7-8} \cline{11-12}
& & RH & Temp & & & RH & Temp & & & RH & Temp \\ \hline
A & 200 & 200 & 200 & A & 200 & 200 & 200 & A & 200 & 200 & 200 \\ \hline
B & 200 & 200 & 200 & B & 200 & 200 & 200 & B & 200 & 200 & 200 \\ \hline
C & 200 & 200 & 200 & C & 200 & 200 & 200 & C & 200 & 200 & 200 \\ \hline
D & 200 & 200 & 200 & D & 200 & 200 & 200 & D & 200 & 200 & 200 \\ \hline
E & 200 & 200 & 200 & E & 200 & 200 & 200 & E & 200 & 200 & 200 \\ \hline
\end{tabular}
\end{table}
\end{document}