自动调整行高

自动调整行高

非常简单的 MWE 是:

\documentclass[12pt,a4paper]{article}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{unicode-math}
\usepackage{graphicx}
\usepackage[left=2.00cm, right=2.00cm, top=3.00cm, bottom=2.00cm]{geometry}
\usepackage{nicematrix, tikz}
\begin{document}

\centering

\begin{NiceTabular}{c|l|r|}
\cline{2-3}
\Block{5-1}{\rotate Long enough text} & Text 1 & 1 \\
\cline{2-3}
& Text 2 & 2\\
\cline{2-3}
& Text 3 & 3\\
\cline{2-3}
& Text 4 & 4\\
\cline{2-3}
& Text 5 & 5\\
\cline{2-3}
\end{NiceTabular}   
    
\end{document}

是否可以自动调整(此处增加)第 2 列和第 3 列的行高?

在此处输入图片描述

答案1

nicematrix可以改变所有单元格的高度。

要进行手动调整,请使用 \begin{NiceTabular}{c|l|r|}[cell-space-limits=<number>pt] 并更改,number直到垂直文本适合。您发布的示例\begin{NiceTabular}{c|l|r|}[cell-space-limits=5pt]就可以了。

也有可能计算通过测量要旋转的文本行的宽度和高度来确定所需的额外高度NiceTabular

然后将这两个值的差除以行,这NiceTabular是一个很好的近似值。(单行则失败)

\fitVcell需要三个参数来完成这项工作:#1 NiceTabular;#2 要旋转的文本;以及 #3 行数。输出将打印一个NiceTabular,现在已扩展以适合垂直文本。

A

(在最后一个表格中,水平线被延长,并且添加了一个 x 以验证对齐。)

\documentclass[12pt,a4paper]{article}
%\usepackage[english]{babel}
%\usepackage{amsmath}
%\usepackage{unicode-math}
%\usepackage{graphicx}
%\usepackage[left=2.00cm, right=2.00cm, top=3.00cm, bottom=2.00cm]{geometry}
%\usepackage{tikz}

\usepackage{nicematrix} % only package needed <<<<<<<<<<<<<<
%%********************** added 
\newlength{\myNT}   
\newlength{\WofT}
\newcommand{\fitVcell}[3]{% #1 NiceTabular %2 vertical text #3 number of rows
 {\NiceMatrixOptions{cell-space-limits = 0pt}   
\settoheight{\myNT}{#1} 
\settowidth{\WofT}{#2}  
\addtolength{\WofT}{-\myNT}
\divide\WofT by#3
\divide\WofT by 2
\NiceMatrixOptions{cell-space-limits = \WofT}
#1}
}
%%**********************

\begin{document}
\centering  
    
\textbf{Original MWE, 5 rows} and   \texttt{Long enough text}\bigskip

\fitVcell{%
    \begin{NiceTabular}{c|l|r|}
        \cline{2-3}
        \Block{5-1}{\rotate Long enough text} & Text 1 & 1 \\
        \cline{2-3}
        & Text 2 & 2\\
        \cline{2-3}
        & Text 3 & 3\\
        \cline{2-3}
        & Text 4 & 4\\
        \cline{2-3}
        & Text 5 & 5\\
        \cline{2-3}
    \end{NiceTabular}  
}
{Long enough text}{5}
        
\bigskip
\textbf{Longer text, 5 rows} and    \texttt{Long enough text much longer}\bigskip

\fitVcell{%
        \begin{NiceTabular}{c|l|r|}
        \cline{2-3}
        \Block{5-1}{\rotate Long enough text much longer} & Text 1 & 1 \\
        \cline{2-3}
        & Text 2 & 2\\
        \cline{2-3}
        & Text 3 & 3\\
        \cline{2-3}
        & Text 4 & 4\\
        \cline{2-3}
        & Text 5 & 5\\
        \cline{2-3}
    \end{NiceTabular}  
}
{Long enough text much longer}{5}

\bigskip

\textbf{Longer text, 2 rows} and    \texttt{Long enough text much longer x}\bigskip

\fitVcell{% Two rows
    \begin{NiceTabular}{c|l|r|}
        \cline{1-3} %changed to test alignment
        \Block{2-1}{\rotate Long enough text much longer x} & Text 1 & 1 \\ % Block 2 x 1 <<<<
        \cline{2-3}
        & Text 2 & 2\\
        \cline{1-3} %changed to test alignment
%       & Text 3 & 3\\
%       \cline{2-3}
%       & Text 4 & 4\\
%       \cline{2-3}
%       & Text 5 & 5\\
%       \cline{2-3}
    \end{NiceTabular}
}
{Long enough text much longer x}{2}
    
\end{document}

答案2

非自动解决方案:

姆韦

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\extrarowheight1em
\begin{NiceTabular}{c|l|r|}
\cline{2-3}
\Block{5-1}{\rotate Long enough text} & Text 1 & 1 \\[1em]
\cline{2-3}
& Text 2 & 2\\[1em]
\cline{2-3}
& Text 3 & 3\\[1em]
\cline{2-3}
& Text 4 & 4\\[1em]
\cline{2-3}
& Text 5 & 5\\[1em]
\cline{2-3}
\end{NiceTabular}   
\end{document}

相关内容