如何使列宽适应表格中的倾斜文本?

如何使列宽适应表格中的倾斜文本?

在下表中,我试图使表格第三行的长文本与 Werner 的回答相一致这里

\documentclass[border = 2mm]{standalone}
\usepackage{booktabs}
\usepackage{array}
\usepackage{graphicx}

\NewDocumentCommand{\rot}{O{45} O{1em} m}{\makebox[#2][l]{\rotatebox{#1}{#3}}}%

\begin{document}    
    \begin{tabular}{@{\extracolsep{6pt}}ccccc@{}}
        \toprule  
        Strategies&\multicolumn{4}{c}{Types}\\
        \cmidrule{1-1}\cmidrule{2-5}    
        &\multicolumn{2}{c}{Type A}&\multicolumn{2}{c}{Type B}\\
        \cmidrule{2-3}\cmidrule{4-5}
        &\rot{A long text here}&\rot{A very very very long text here}&\rot{A very very long text here}&\rot{A very very very very long text here}\\
        \cmidrule{2-3}\cmidrule{4-5}
        An strategy & 0 &1&2&3\\
        \bottomrule     
    \end{tabular}
\end{document}

但柱子的设置并不适合倾斜的设置,导致以下的结果:

在此处输入图片描述

是否有一种基于倾斜值自动调整列的方法?

答案1

根据内容调整列宽是默认设置。但是,您通过将倾斜文本放入 来隐藏其宽度\makebox。因此,您不必使用\makebox或为其提供足够的可选width参数。例如:\rot[45][\width]{A very very very very long text here}

\documentclass[border = 2mm]{standalone}
\usepackage{booktabs}
\usepackage{array}
\usepackage{graphicx}

\NewDocumentCommand{\rot}{O{45} O{1em} m}{\makebox[#2][l]{\rotatebox{#1}{#3}}}%

\begin{document}    
    \begin{tabular}{@{\extracolsep{6pt}}ccccc@{}}
        \toprule  
        Strategies&\multicolumn{4}{c}{Types}\\
        \cmidrule{1-1}\cmidrule{2-5}    
        &\multicolumn{2}{c}{Type A}&\multicolumn{2}{c}{Type B}\\
        \cmidrule{2-3}\cmidrule{4-5}
        &\rot{A long text here}&\rot{A very very very long text here}&\rot{A very very long text here}&\rot[45][\width]{A very very very very long text here}\\
        \cmidrule{2-3}\cmidrule{4-5}
        An strategy & 0 &1&2&3\\
        \bottomrule     
    \end{tabular}
\end{document}

在此处输入图片描述

相关内容