自动调整表格中的多行高度

自动调整表格中的多行高度

我正在尝试创建一个包含(多个)具有一定宽度的多行单元格的表格,并自动换行文本。但是,当文本太长时,它会溢出到下一个单元格。

我知道我可以使用\\[1cm]来手动解决这个问题(另请参阅自动调整表格行高),但我想自动执行此操作。这可能吗?

以下是 MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}

\begin{document}

\noindent\begin{tabular}{p{4cm}|c}
     A & B  \\\hline
     \multirow{2}{4cm}{My long text here is ok, not long enough to spill} & 1 \\\cline{2-2}
      & 2 \\\hline
     \multirow{2}{4cm}{Other text here} & 3 \\\cline{2-2}
      & 4 \\\hline
\end{tabular}

\vspace{3cm}

\noindent\begin{tabular}{p{4cm}|c}
     A & B  \\\hline
     \multirow{2}{4cm}{My super long text here will spill over to the next line. How do I avoid this?} & 1 \\\cline{2-2}
      & 2 \\\hline
     \multirow{2}{4cm}{Other text here} & 3 \\\cline{2-2}
      & 4 \\\hline
\end{tabular}

\end{document}

输出:

MWE 输出

我将接受使用其他环境/包的答案。

答案1

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}

\begin{document}

With multirow and a manually addd empty line: 


\noindent\begin{tabular}{p{4cm}|c}
     A & B  \\\hline
     \multirow{2}{=}{My super long text here will spill over to the next line. How do I avoid this?} & 1 \\\cline{2-2}
      & 2 \\ 
      \\ \hline
     \multirow{2}{=}{Other text here} & 3 \\\cline{2-2}
      & 4 \\\hline
\end{tabular}

\vspace{1cm}

With nested tabulars:

\noindent\begin{tabular}{p{4cm}|@{}c@{}}
     A & B  \\
     \hline
     My super long text here will spill over to the next line. How do I avoid this? 
       & \begin{tabular}[t]{c} 1 \\ \hline 2 \end{tabular} \\ 
     \hline
     Other text here & \begin{tabular}[t]{c} 3 \\ \hline 4 \end{tabular} \\
      \hline
\end{tabular}


\end{document}

对于垂直居中的单元格,您可以使用cellspace包。如果第一列中的文本长度或第二列中的行数发生变化,则\cellspacetoplimit必须相应调整的值。

在此处输入图片描述

\documentclass{article} \usepackage[utf8]{inputenc} \usepackage{array}

\usepackage[column=0]{cellspace} \setlength{\cellspacetoplimit}{0.5\baselineskip} \setlength{\cellspacebottomlimit}{\cellspacetoplimit} \begin{document}

\noindent\begin{tabular}{m{4cm}|@{}c@{}}
     A & B  \\
     \hline
     My super long text here will spill over to the next line. How do I avoid this? 
       & \begin{tabular}{0c} 1 \\ \hline 2 \end{tabular} \\ 
     \hline
     Other text here & \begin{tabular}{c} 3 \\ \hline 4 \end{tabular} \\
      \hline \end{tabular}


\end{document}

答案2

这个(不是真正自动的)黑客攻击怎么样?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow, makecell, booktabs}

\begin{document}

\noindent
\begin{tabular}{m{4cm}|c}
     A & B \\\hline
    My super long text here will spill over to the next line. How do I avoid this? & \makecell{1 \\[0.5ex] \cmidrule(l{-7pt}r{-7pt}){1-1} 2}\\\hline
     \multirow{2}{=}{Other text here} & 3 \\\cline{2-2}
      & 4 \\\hline
\end{tabular}

\end{document} 

在此处输入图片描述

答案3

使用tabularray包:

\documentclass{article}
\usepackage{tabularray}
\begin{document}


\SetTblrInner{rowsep=0pt}
\noindent\begin{tblr}{p{4cm}|c}
     A & B  \\\hline
     \SetCell[r=2]{} My super long text here will spill over to the next line. How do I avoid this? & 1 \\\cline{2-2}
      & 2 \\\hline
     \SetCell[r=2]{} Other text here & 3 \\\cline{2-2}
      & 4 \\\hline
\end{tblr}

\end{document}

输出

相关内容