如何垂直对齐表格中的某些文本?

如何垂直对齐表格中的某些文本?

编辑

我获得了以下表格模板代码(已进行修改以考虑到下面 David 的评论):

\documentclass[12pt, a4paper, final]{article} 
\usepackage{array}
\usepackage{multirow}
\usepackage{booktabs}

% This command computes and creates a vertical space
% depending on the number of rows to compensate for.
% It makes use of the counter verticalcompensationrows
% and the length \verticalcompensationlength which equals
% \aboverulesep plus \belowrulesep
\newlength{\verticalcompensationlength}
\setlength{\verticalcompensationlength}{\aboverulesep}
\addtolength{\verticalcompensationlength}{\belowrulesep}
\newcounter{verticalcompensationrows}
\newcommand{\verticalcompensation}[1]{%
\setcounter{verticalcompensationrows}{#1}%
\addtocounter{verticalcompensationrows}{-1}%
\vspace*{-\value{verticalcompensationrows}\verticalcompensationlength}%
}

% This command reimplements \multirow to compensate
% for the vertical offset, but looses some functionality
% of the \multirow command (not discussed here).
\newcommand{\multirowbt}[3]{%
\multirow{#1}{#2}{\verticalcompensation{#1}#3}%
}

\begin{document}
\begin{tabular}{lp{13cm}}
\toprule
\multicolumn{2}{c}{Table title or something else ...}  \\
\midrule
\multirowbt{4}{*}{My name} & A bit of text  \\
\cmidrule{2-2}
 & A bit more text A bit more text A bit more text A bit more text  \\
\cmidrule{2-2}
 & A bit more text A bit more text A bit more text A bit more text \\
\cmidrule{2-2}
 & A bit more text A bit more text A bit more text A bit more text \\
\midrule
\multirowbt{3}{*}{Your name} & Some text \\
\cmidrule{2-2}
 & More text More text More text \\
\cmidrule{2-2}
 & More text More text More text \\
\bottomrule
\end{tabular}
\end{document}

它输出如下图1所示的表格:

在此处输入图片描述

但是,当我有一些覆盖多行的文本时,第 1 列的垂直对齐不再正常,如表 2 所示。

在下面的回答中,David 使用 \raisebox 给出了此问题的解决方案。这可行,但它要求我更改每个表格的每一列的框必须移动的量。

我想知道是否有人知道如何让 Latex 自动将内容居中。

答案1

如果您将内容框在第 2 列中,m-column 规范将使其锚点垂直居中,从而提供您想要的对齐方式。我将其框起来的方法是将其放在tabular已知/固定宽度中,并确保它适合文本块:

在此处输入图片描述

\documentclass[12pt]{article} 
\usepackage{booktabs,array}% http://ctan.org/pkg/{booktabs,array}
\begin{document}

\noindent
\setbox0=\hbox{Your name}% Store the widest left-column entry
\begin{tabular}{lm{\dimexpr\textwidth-3\tabcolsep-\wd0}@{}}
  \toprule
  \multicolumn{2}{c}{Table title or something else ...}  \\
  \midrule
  My name & \begin{tabular}{p{\dimexpr\linewidth-\tabcolsep}@{}}
    A bit of text  \\
    \midrule
    A bit more text A bit more text A bit more text A bit more text  \\
    \midrule
    A bit more text A bit more text A bit more text A bit more text \\
    \midrule
    A bit more text A bit more text A bit more text A bit more text
  \end{tabular} \\
  \midrule
  Your name & \begin{tabular}{p{\dimexpr\linewidth-\tabcolsep}@{}}
    Some text \\
    \midrule
    More text More text More text \\
    \midrule
    More text More text More text
  \end{tabular} \\
  \bottomrule
\end{tabular}

\end{document}

最宽的左列条目被存储并用于右列的宽度计算m

答案2

在此处输入图片描述

\documentclass[12pt, a4paper, final]{article} 


\usepackage{booktabs}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}



\begin{document}

\noindent
\begin{tabularx}{\textwidth}{lX}
\toprule
\multicolumn{2}{c}{Table title or something else ...}  \\
\midrule
\smash{\raisebox{-50pt}{My name}} & A bit of text  \\
\cmidrule{2-2}
 & A bit more text A bit more text A bit more text A bit more text  \\
\cmidrule{2-2}
 & A bit more text A bit more text A bit more text A bit more text \\
\cmidrule{2-2}
 & A bit more text A bit more text A bit more text A bit more text \\
\midrule
\smash{\raisebox{-20pt}{Your name}} & Some text \\
\cmidrule{2-2}
 & More text More text More text \\
\cmidrule{2-2}
 & More text More text More text \\
\bottomrule
\end{tabularx}

\end{document}

答案3

当然还有始终存在的基于 tikz 的过度解决方案:

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{tikz}
\begin{document}

\def\tikzmark#1{\tikz[overlay, remember picture]\node[coordinate] (#1) {};}
\def\textbetween#1#2#3{ % #1 #2 row ids, #3 content
\tikz[overlay, remember picture]\path ([yshift=1ex] #1)--(#2) node[midway,right] {#3};% 
}

\begin{tabular}{p{2cm}p{10cm}}
\toprule
\multicolumn{2}{c}{Table title or something else ...}  \\
\midrule
\tikzmark{1} & A bit of text  \\
\cmidrule{2-2}
             & This is what happens when I write too much text in one line: it creates
               an extra line and the item in the first column is no longer aligned!  \\
\cmidrule{2-2}
             & A bit more text A bit more text A bit more text A bit more text \\
\cmidrule{2-2}
\tikzmark{2} & A bit more text A bit more text A bit more text A bit more text \\
\midrule
\textbetween{1}{2}{My Name}%
\tikzmark{1} & Some text \\
\cmidrule{2-2}
             & More text More text More text \\
\cmidrule{2-2}
\tikzmark{2} & More text More text More text \\
\bottomrule
\textbetween{1}{2}{Your Name}%
\end{tabular}

\end{document}

结果

用法:将\tikzmark{x}多行填充的第一行和最后一行作为x行的任意标识符,然后使用宏\textbetween将文本放在两个标记之间的中心。

请注意,您还必须为第一列指定固定宽度。在这种情况下,我使用了 2cm。还请记住编译两次。

相关内容