使用行数超过 1 行的单元格进行多行垂直对齐

使用行数超过 1 行的单元格进行多行垂直对齐

我想垂直对齐(最好是居中)下表左列(黄色)的文本(“TEST”)。当每行的高度恰好为一行时,多行可以正常工作:

\documentclass[ngerman,a4paper,12pt,pdftex]{report}
\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{colortbl}

\begin{document}
\begin{longtable}{|p{0.025\textwidth}|p{0.775\textwidth}|>{\centering arraybackslash}m{0.2\textwidth}|}
 & \textbf{foo} & \textbf{bar} \\
\hline
\hline 
\endfirsthead
 & \textbf{foo} & \textbf{bar} \\
\hline
\hline
\endhead
\cellcolor{yellow}& short text & 1 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 2 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 3 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 4 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 5 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 6 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 7 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 8\\
\cline{2-3} 
\cellcolor{yellow} & short text & 9 \\
\cline{2-3} 
\multirow{-10}{*}{\cellcolor{yellow}\begin{sideways}TEST\end{sideways}}  & short text &  10 \\
\hline
\end{longtable}

\end{document}

在此处输入图片描述

当某个单元格中有较长的文本时,左侧单元格(“TEST”)的文本会在某处对齐:

\documentclass[ngerman,a4paper,12pt,pdftex]{report}
\usepackage[utf8]{inputenc}

\usepackage{rotating}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{colortbl}

\begin{document}
\begin{longtable}{|p{0.025\textwidth}|p{0.775\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|}
 & \textbf{foo} & \textbf{bar} \\
\hline
\hline 
\endfirsthead
 & \textbf{foo} & \textbf{bar} \\
\hline
\hline
\endhead
\cellcolor{yellow}& short text & 1 \\
\cline{2-3} 
\cellcolor{yellow} & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 2 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 3 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 4 \\
\cline{2-3} 
\cellcolor{yellow} & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 5 \\
\cline{2-3} 
\cellcolor{yellow} & very long text ....... ......... ........ ....... ....... ....... ........ ............. ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... needs lots of lines & 6 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 7 \\
\cline{2-3} 
\cellcolor{yellow} & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 8\\
\cline{2-3} 
\cellcolor{yellow} & short text & 9 \\
\cline{2-3} 
\multirow{-10}{*}{\cellcolor{yellow}\begin{sideways}TEST\end{sideways}}  & short text &  10 \\
\hline
\end{longtable}

\end{document}

在此处输入图片描述

在第二个示例中,如何对齐左列的文本?

答案1

根据我的经验multirow,如果跨越多行单元格,则无法正确进行垂直对齐。但是,您可以使用 -parameter 手动调整对齐方式fixup

% \multirow{nrows}[bigstruts]{width}[fixup]{text}
\multirow{-10}{*}[3em]{\cellcolor{yellow}\begin{sideways}TEST\end{sideways}}

解决该问题的其他可能性在“在单元格包含多行文本的表格中将多行文本居中“ 和 ”我怎样才能为 \multirow 内的长文本块实现适当的垂直间距?“。

答案2

我也用一个快速解决方案解决了这个问题:将 parbox 与多行内的 vspace 结合起来

\multirow{5}{*}{\parbox{1\linewidth}{\vspace{1cm} something something}}

答案3

该表使用起来简单直接卡路里. 没什么特别的,只有标准命令:

\documentclass[a4paper, 11pt]{article}
\usepackage{cals, xcolor, rotating}
\usepackage[margin=2.5cm]{geometry}


\let\nc=\nullcell                                                  % Shortcuts
\let\sc=\spancontent

\begin{document}

\begin{calstable}

% Defining 3 column relativ to each other and relativ to the margins
\colwidths{{\dimexpr(\columnwidth)/1000*100\relax}
            {\dimexpr(\columnwidth)/1000*775\relax}
            {\dimexpr(\columnwidth)/1000*125\relax}
            }
% The tabular fills the text area

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.4pt}
\def\cals@bodyrs@width{0.4pt}
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0.4pt}        % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.4pt}
\def\cals@bgcolor{}

\def\yellow{\ifx\cals@bgcolor\empty % Define colour on-off switch
\def\cals@bgcolor{yellow!30}
\else \def\cals@bgcolor{} \fi}

\def\tb{\ifx\cals@borderT\relax     % Top border switch (off-on)
    \def\cals@borderT{0pt}
\else \let\cals@borderT\relax\fi}

\def\bb{\ifx\cals@borderB\relax     % Botton border switch (off-on)
    \def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}

\def\rb{\ifx\cals@borderR\relax     % Right border switch (off-on)
    \def\cals@borderR{0pt}
\else \let\cals@borderR\relax\fi}

\def\lb{\ifx\cals@borderL\relax     % Left border switch (off-on)
    \def\cals@borderL{0pt}
\else \let\cals@borderL\relax\fi}

% H1
\thead{%
\bfseries
\brow
    \yellow\nc{tlr}\yellow
    \alignL\cell{Foo}
    \alignC\cell{Bar}
\erow
\mdseries
}
% R1
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{1}
\erow
% R2 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{2}
\erow
% R3 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{3}
\erow
% R4 Body
\brow
    \yellow \nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{4}
\erow
% R5 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{5}
\erow
% R6 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{6}
\erow
% R7 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{7}
\erow
% R8 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{8}
\erow
% R9 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{9}
\erow
% R10 Body
\brow
    \yellow\nc{blr}\alignC\sc{\vfil\begin{sideways}TEST\end{sideways}}\yellow
    \alignL\cell{short text}
    \alignC\cell{10}
\erow
\makeatletter
\end{calstable}\par % \par needed to align the tabular

\end{document}

在此处输入图片描述

在此处输入图片描述

答案4

使用新的 LaTeX3 软件包提供更好的解决方案tabularray

\documentclass[ngerman,a4paper,12pt,pdftex]{report}

\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{tabularray}
\usepackage{xcolor}

\begin{document}
\begin{longtblr}[
  caption = {Long Table},
]{
  colspec={|c|X[5,m,l]|X[1,m,c]|},
  rowhead = 1,
  row{1} = {font=\bfseries},
  cell{2}{1} = {r=10}{yellow7}, % multirow
}
\hline[1pt]
 & Foo & Bar \\
\hline[0.8pt]
 \begin{sideways}TEST\end{sideways} & short text & 1 \\
\cline{2-3} 
 & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 2 \\
\cline{2-3} 
 & short text & 3 \\
\cline{2-3} 
 & short text & 4 \\
\cline{2-3} 
 & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 5 \\
\cline{2-3} 
 & very long text ....... ......... ........ ....... ....... ....... ........ ............. ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... needs lots of lines & 6 \\
\cline{2-3} 
 & short text & 7 \\
\cline{2-3} 
 & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 8\\
\cline{2-3} 
 & short text & 9 \\
\cline{2-3} 
 & short text &  10 \\
\hline[1pt]
\end{longtblr}

\end{document}

在此处输入图片描述

相关内容