NiceMatrix,旋转单元格和行高

NiceMatrix,旋转单元格和行高

我正在尝试在NiceTabularX环境中创建一个表格,并使用命令旋转第一行的单元格\rotate。但是,旋转后的行的高度似乎不取决于其内容的长度,而实际上接近其所包含的最宽列的宽度。因此,第一行的旋转内容要么超出表格,要么挤在几行中才能容纳进去(这不是我想要的)。如何确保调整第一行的高度NiceTabularX以使其内容适合它?

非常感谢你的帮助!

这是一个简化的例子:

\documentclass[12pt,a4paper]{book}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{nicematrix}
\begin{document}
If my table is wide enough, I have no problem to make the content of the first row, once it's rotated, fit in the cell:\par
\begin{NiceTabularX}{15cm}{*{4}{X[c]}}[hvlines]
    \RowStyle{\rotate}
    Short & Verylong & Tiny & Medium \\
    E & F & G & H \\
    I & J & K & L \\
    M & N & O & P\\
\end{NiceTabularX}

However, as soon as I want to reduce the size of the table, the content of the row stops fitting. It seems that, instead of adjusting the height of first row, it takes the width of its widest element:\par
\vspace{1cm}
\begin{NiceTabularX}{5cm}{*{4}{X[c]}}[hvlines]
    \RowStyle{\rotate}
    Short & Verylong & Tiny & Medium \\
    E & F & G & H \\
    I & J & K & L \\
    M & N & O & P\\
\end{NiceTabularX}

I would like to have a table that has the same size as the second one, but where the top row would be high enough for all the text of the cells to fit in it, and without having to add the \verb!\vspace! to avoid overlapping the preceding text.
\end{document}

在此处输入图片描述

答案1

选项1这种方法是使用包tabularray

b

\documentclass[12pt,a4paper]{book}
\usepackage[T1]{fontenc}

\usepackage{graphicx} % for rotatebox
\usepackage{tabularray}

\begin{document}
    
    
\begin{tblr}{width= 15cm,
        vlines,
        hlines,
        colspec=*{4}{X[c]}, 
        row{1} = {abovesep=4pt}
    }
    \rotatebox{90}{Short} & \rotatebox{90}{Verylong}  &  \rotatebox{90}{Tiny} &  \rotatebox{90}{Medium} \\
    E & F & G & H \\
    I & J & K & L \\
    M & N & O & P\\ 
    \end{tblr}  

\bigskip
    
\begin{tblr}{width= 5cm,
        vlines,
        hlines,
        colspec=*{4}{X[c]},
        row{1} = {abovesep=4pt}
    }
    \rotatebox{90}{Short} & \rotatebox{90}{Verylong}  &  \rotatebox{90}{Tiny} &  \rotatebox{90}{Medium} \\
    E & F & G & H \\
    I & J & K & L \\
    M & N & O & P\\
\end{tblr}

\end{document}

选项 2使用nicematrix,但使用\rotatebox与选项 1 类似的包graphicx。(而不是\RowStyle{\rotate}

\documentclass[12pt,a4paper]{book}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{nicematrix}

\usepackage{graphicx} % for rotatebox   
\newcommand{\rotnd}[1]{\rotatebox{90}{#1}}

\begin{document}

\begin{NiceTabularX}{15cm}{*{4}{X[c]}}[hvlines,cell-space-limits=3pt]
    \rotnd{Short} & \rotnd{Verylong} & \rotnd{Tiny} & \rotnd{Medium} \\
    E & F & G & H \\
    I & J & K & L \\
    M & N & O & P\\
\end{NiceTabularX}

\bigskip

\begin{NiceTabularX}{5cm}{*{4}{X[c]}}[hvlines, cell-space-limits=3pt]
    \rotnd{Short} & \rotnd{Verylong} & \rotnd{Tiny} & \rotnd{Medium} \\
    E & F & G & H \\
    I & J & K & L \\
    M & N & O & P\\
\end{NiceTabularX}

\end{document}

相关内容