标题自动换行

标题自动换行

如果标题太长,我希望在表格或图形的标题中自动换行,以便标题具有对齐方式,并且两行长度完全相同。如果可能的话,它应该是一个通用环境,我可以选择将它应用于哪些表格,这样我就不必为每个表格手动执行此操作。最好我还想设置标题的长度。下面是一个例子,我手动尝试做一个两行标题,每行长度相同:

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage[flushleft]{threeparttable}
\begin{document}

\begin{table}[!htbp]
\caption{\\ This Tile is Too Long So I Would Like to Split it Automati- \\ cally in Two Equal Lines And Have a Justified Alignment}
\footnotesize
\centering
\begin{threeparttable}
\begin{tabularx}{\textwidth}{lYYY} \hline \hline
& & & \\
& \multicolumn{3}{c}{Outcome Variable A} \\  \cline{2-4}
& & & \\
& All & Group 1  & Group 2 \\
& & & \\
Treatment               &  BBBB  &  BBBB  &  BBBB  \\
                        & (SSSS) & (SSSS) & (SSSS) \\
Observations            &  NNNN  &  NNNN  &  NNNN  \\
Adjusted $ R^{2} $      &  RRRR  &  RRRR  &  RRRR  \\
Mean Y - Baseline       &  MMMM  &  MMMM  &  MMMM  \\
Mean Y - Overall        &  MMMM  &  MMMM  &  MMMM  \\
& & & \\
& \multicolumn{3}{c}{Outcome Variable B} \\  \cline{2-4}
& & & \\
& All & White  & Non-White \\
& & & \\
Treatment               &  BBBB  &  BBBB  &  BBBB  \\
                        & (SSSS) & (SSSS) & (SSSS) \\
Observations            &  NNNN  &  NNNN  &  NNNN  \\
Adjusted $ R^{2} $      &  RRRR  &  RRRR  &  RRRR  \\
Mean Y - Baseline       &  MMMM  &  MMMM  &  MMMM  \\
Mean Y - Overall        &  MMMM  &  MMMM  &  MMMM  \\
& & & \\
Mean Treatment          &  MMMM  &  MMMM  &  MMMM \\
& & & \\ \hline \hline
\end{tabularx}
\end{threeparttable}
\end{table}

\end{document}

答案1

尝试一下这个代码。

该命令\SetTwoLinesCaption将长标题制作成(本地)两行标题。短标题将保留在一行中。长标题若要适合两行(悬挂),将分成多行。

埃

\documentclass[12pt,a4paper]{article}

\usepackage{caption}% added <<<<<<<<<<<<<<
\usepackage{tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage{threeparttable}

\newlength{\Lcaption}
\newlength{\Tcaption}
\newlength{\Mcaption}
\newlength{\tmpcaption} 
\newlength{\tmpcaptioni}    

    \newcommand{\SetTwoLinesCaption}[1]{% <<<<<<<<<<<<<<<<
    \captionsetup[table]{format=hang,margin=0.5\Mcaption,labelsep=colon}       
    \settowidth{\Tcaption}{Table~\thetable:\space}
    \settowidth{\Lcaption}{#1}      
    \setlength{\tmpcaption}{\dimexpr\Lcaption+\Tcaption}
    \setlength{\tmpcaptioni}{\dimexpr\Lcaption+2\Tcaption}
    \setlength{\Mcaption}{\dimexpr\linewidth- 0.5\Lcaption-\Tcaption}%
    \ifdim\tmpcaption<\linewidth\setlength{\Mcaption}{0pt}\fi%
    \ifdim\tmpcaptioni>2\linewidth\setlength{\Mcaption}{\dimexpr\linewidth- 0.333\Lcaption-\Tcaption}\fi%
    \captionof{table}{#1}
}
    
\begin{document}
        
    \begin{table}[!htbp]
        \SetTwoLinesCaption{This Tile is Too Long So I Would Like to Split it Automatically in Two Equal Lines And Have a Justified Alignment}
        \SetTwoLinesCaption{This Tile is Too Long So I Would Like to Split it Automatically in Two}
        \SetTwoLinesCaption{This Tile is Too  Too  Too Too Long So I Would Like to Split it Automatically in More Equal Lines And Have a Justified Alignment Too  Too Too Long Too  Too Too Long}
        \SetTwoLinesCaption{This Tile is Too short to make two lines }
        \footnotesize
        \centering
        \begin{threeparttable}
            \begin{tabularx}{\textwidth}{lYYY} \hline \hline
                & & & \\
                & \multicolumn{3}{c}{Outcome Variable A} \\  \cline{2-4}
                & & & \\
                & All & Group 1  & Group 2 \\
                & & & \\
                Treatment               &  BBBB  &  BBBB  &  BBBB  \\
                & (SSSS) & (SSSS) & (SSSS) \\
                Observations            &  NNNN  &  NNNN  &  NNNN  \\
                Adjusted $ R^{2} $      &  RRRR  &  RRRR  &  RRRR  \\
                Mean Y - Baseline       &  MMMM  &  MMMM  &  MMMM  \\
                Mean Y - Overall        &  MMMM  &  MMMM  &  MMMM  \\
                & & & \\
                & \multicolumn{3}{c}{Outcome Variable B} \\  \cline{2-4}
                & & & \\
                & All & White  & Non-White \\
                & & & \\
                Treatment               &  BBBB  &  BBBB  &  BBBB  \\
                & (SSSS) & (SSSS) & (SSSS) \\
                Observations            &  NNNN  &  NNNN  &  NNNN  \\
                Adjusted $ R^{2} $      &  RRRR  &  RRRR  &  RRRR  \\
                Mean Y - Baseline       &  MMMM  &  MMMM  &  MMMM  \\
                Mean Y - Overall        &  MMMM  &  MMMM  &  MMMM  \\
                & & & \\
                Mean Treatment          &  MMMM  &  MMMM  &  MMMM \\
                & & & \\ \hline \hline
            \end{tabularx}
        \end{threeparttable}
    \end{table}
\end{document}

相关内容