长表格表头垂直对齐

长表格表头垂直对齐

考虑这个例子,一个表中有两个长标题(一个较长):

\documentclass{article}
\usepackage{graphicx}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}
    \begin{table}
        \begin{tabular}{|C{2em}|C{2em}|}\hline
            \rotatebox{90}{Long long heading 1~} & \rotatebox{90}{Long long longer longer heading 2~}\\ \hline %%%%%%%%%%% `~` is inserted at end of headings to keep the margin away
            Text 1 & Text 2\\ \hline
        \end{tabular}
    \end{table}
\end{document}

请注意,两个标题在 y 轴上居中对齐。我想要的是,表格标题在 y 轴上对齐在底部。换句话说,突出显示的空间不会存在。

答案1

正如我在评论中建议的那样,这是一个更简单的解决方案,基于eqparbox

  \documentclass{article}
\usepackage{graphicx}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage{eqparbox}

\begin{document}

    \begin{table}
\centering
        \begin{tabular}{|C{2em}|C{2em}|}\hline
            \multicolumn{1}{|p{2em}}{\centering\rotatebox[origin = l]{90}{Long long heading 1}~} &\multicolumn{1}{|p{2em}|}{\centering \rotatebox[origin = l]{90}{Long long longer longer heading 2~}}\\ \hline %%%%%%%%%%% `~` is inserted at end of headings to keep the margin away
            Text 1 & Text 2\\ \hline
        \end{tabular}
\qquad
        \begin{tabular}{|C{2em}|C{2em}|}\hline
            \rotatebox{90}{\eqmakebox[H][l]{Long long heading 1}~} & \rotatebox{90}{\eqmakebox[H][l]{Long long longer longer heading 2}~}\\ \hline %%%%%%%%%%% `~` is inserted at end of headings to keep the margin away
            Text 1 & Text 2\\ \hline
        \end{tabular}
    \end{table}

\end{document} 

在此处输入图片描述

答案2

使用rotatingmakecell包的两种情况:

\documentclass[12pt]{article}
\usepackage{rotating}
\usepackage{makecell}
\renewcommand\theadfont{\normalsize}

\begin{document}
    \begin{table}[ht]
    \centering
    \settowidth\rotheadsize{Long Long longer longer heading 2}
\begin{tabular}{|c|c|}
    \hline
\rothead{Long Long heading} 
                    & \rothead{Long Long longer longer heading 2} \\ 
    \hline
\makecell{Text\\ 1} & \makecell{Text\\ 2}   \\ 
    \hline
\end{tabular}
    \end{table}
or table design which I more prefer:
    \begin{table}[ht]
    \centering
    \settowidth\rotheadsize{Long Long heading}
\begin{tabular}{|c|c|}
    \hline
\rothead{Long Long heading}
                    & \rothead{Long Long longer longer heading 2} \\
    \hline
\makecell{Text\\ 1} & \makecell{Text\\ 2}   \\
    \hline
\end{tabular}
    \end{table}
\end{document} 

在此处输入图片描述

答案3

我的建议....

\documentclass[12pt]{article}
\usepackage{amsmath}

\usepackage{graphicx}
\begin{document}
\begin{table}[h]
\centering 
\begin{tabular}{|c|c|}
\hline
\rotatebox{90}{Long Long heading~} & \rotatebox{90}{Long Long longer longer heading 2~} \\ \hline
\multicolumn{1}{|c|}{\begin{tabular}[c]{@{}c@{}}Text\\ 1\end{tabular}} & \begin{tabular}[c]{@{}c@{}}Text\\ 2\end{tabular} \\ \hline
\end{tabular}
\end{table}
\end{document} 

在此处输入图片描述

答案4

{NiceTabular}nicematrix,您有一个内置命令\rotate可以执行此操作。但是,您必须使用 重建c列,multicolumn因为您想C在其他地方使用您的类型列。

\documentclass{article}
\usepackage{nicematrix}

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\NiceMatrixOptions{cell-space-top-limit=2pt}

\begin{document}
\begin{table}
\begin{NiceTabular}{C{2em}C{2em}}[hvlines]
\multicolumn{1}{c}{\rotate Long long heading 1} 
& \multicolumn{1}{c}{\rotate Long long longer longer heading 2}\\ 
Text 1 & Text 2\\ 
\end{NiceTabular}
\end{table}
\end{document}

上述代码的输出

相关内容