0

I would like to have an automatic line break in the title of a table or figure if the title is too long so that the title has a justified alignment and the two line are exactly the same length. If possible it should be a general environment and I can choose which tables it is applied to so that I don't have to do it manually for each table. Preferably I would also like to set the length of the title. Here is an example where I manually try to do a two-line title with each line having the same length:

\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}
2
  • 2
    Please don't post code fragments. Instead put them into a compilable document that people can play with.
    – Alan Munn
    Aug 6, 2022 at 20:25
  • 2
    I've taken the liberty of making your code compilable by adding a preamble to load the minimally required package as well as a \documentclass directive. Feel free to revert and/or augment.
    – Mico
    Aug 6, 2022 at 20:29

1 Answer 1

2

Try this code.

The command \SetTwoLinesCaption will make a (local) two-line caption of a long caption. Short captions will remain in one line. Long captions to fit in two (hang) lines will split in more lines.

e

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .