4

I have a problem with the packages ragged2e, caption and tabularx. Maybe someone can help me?

tabularx is used to create tables that should be horizontally centred, display centred table cells and sometimes have line breaks in the cells.

\documentclass{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{tabularx}
\usepackage[format=hang,singlelinecheck=false]{caption}

\usepackage[newcommands]{ragged2e}  % comment in / out

\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}
    \centering  % also \Centering (package ragged2e) is not working

    \begin{tabularx}{0.8\textwidth}{CC}\hline
    
        Column 1\newline More text  & Column 2\par More text    \\\hline\hline
        
        A   & B \\
        C   & D \\\hline
        
    \end{tabularx}
    \caption[]{A long table caption that goes over several lines so that we can see the error of weirdly indented captions\\ This line stays to the left\par This line is fine}
\end{table}

\end{document}
  1. If ragged2e is used, captions are not displayed correctly: Lines after a line break using \\ are not indented as desired (according to the hang option in the caption package), instead start at the left. This happens when the table is centred; if \centering is not used, the caption is correct. The problem also exists with figures. In the past, the captions were displayed correctly, I can't say when the error of the caption not being indented started.

  2. If ragged2e is not used, the table or figure caption is correct, but text wrapped with \newline in table cells is no longer correctly centred, see column 1.

I could solve 1) by using \par instead of \\ to wrap rows of the caption.

I could solve 2) by using \par instead of \newline to wrap rows of the table cells.

Both solutions require a lot of customisation in various documents, so I would like to avoid this if possible. If unavoidable, which solution would make more sense?

I would like to continue using the ragged2e package, as it makes ragged text more attractive due to possible word separations.

Does anyone have any idea how I can correct the problem? Am I perhaps doing something wrong? Am I using the wrong commands?

Thank you very much!

(I also posted this question on golatex.de)

9
  • 2
    Welcome to TeX.SE.
    – Mico
    Commented May 29 at 2:05
  • 2
    Welcome! Why do you have \usepackage[ansinew]{inputenc}? Are you really saving your files in this encoding? If so, why?
    – cfr
    Commented May 29 at 3:08
  • 4
    Don't use \\ to end lines except in special environments. tabular is one for ends of rows. center is another. \caption is not. Why are you inserting line breaks manually? You should let the caption wrap naturally.
    – cfr
    Commented May 29 at 3:15
  • 1
    @Seban You don't need inputenc for utf8. It's been obsolete for quite some time. Do you really want all that information in the caption? But \par is probably better. I'm not sure as I don't tend to put this information in captions. fontenc you do need since you're not using a Unicode engine and T1 is not default. Otherwise the Umlauts won't be right.
    – cfr
    Commented May 29 at 14:58
  • 1
    @cfr Thanks, I will look into the encoding. Regarding the captions: It's been like that for ages and we actually try to reduce text bodies, so this information being in the captions seems plausible.
    – Seban
    Commented May 29 at 15:22

4 Answers 4

5

Your posting raises several issues, and it's not clear to me which ones are (most) important to you.

  • If you don't like the "weird indentation" of long captions, don't specify the option hang when loading the caption package. By "long caption", I mean a caption whose length exceeds some limit (usually, but not necessarily, \linewidth).

  • Virtually all the world's (normal) editing software has been using utf8 as the default input encoding for a decade or more by now. Have you really reset your editor to generate ansinew-encoded text? If so, why?

  • You've (re)discovered that loading the ragged2e package with the option newcommands can be somewhat perilous, as specifying this option redefines \centering to act like \Centering, etc. I wouldn't specify this option; instead, I'd just replace all instances of \centering and \centering\arraybackslash with \Centering.

  • If you wish to limit the width of the caption to that of the associated tabularx environment, I suggest you load the threeparttable package in the preamble and encase both the tabularx environment and the \caption{...} in a threeparttable environment. (Aside: The third formal component of a threeparttable environment is the optional tablenotes environment.) See the second table below for an application of this idea.

enter image description here

\documentclass{scrreprt}

\usepackage[T1]{fontenc}
%%%\usepackage[ansinew]{inputenc} % huh???
\usepackage{tabularx}
\usepackage[format=hang,
      singlelinecheck=false % irrelevant for long captions
       ]{caption}
\newcolumntype{C}{>{\Centering}X}
\usepackage{ragged2e}  % 'newcommands' is the default
\usepackage{threeparttable} % see https://ctan.org/pkg/threeparttable

\begin{document}

\begin{table}
\centering  

  \begin{tabularx}{0.6\textwidth}{CC}
  \hline
  Column 1\newline More text & Column 2\newline More text \\   
  \hline\hline
  A   & B \\
  C   & D \\
  \hline   
  \end{tabularx}
  \caption{A long table caption that goes over several lines so that we can see the error of weirdly indented captions. This line is fine. This line is fine.}
  
\vspace{1cm}

  \begin{threeparttable}
  \begin{tabularx}{0.6\textwidth}{CC}
  \hline
  Column 1\newline More text & Column 2\newline More text \\   
  \hline\hline
  A   & B \\
  C   & D \\
  \hline   
  \end{tabularx}
  \caption{A long table caption that goes over several lines so that we can see the error of weirdly indented captions. This line is fine. This line is fine.}
  \end{threeparttable}

\end{table}

\end{document}
5
  • Thanks for this great answer. I answer in several comments to keep it structured a bit.
    – Seban
    Commented May 29 at 13:37
  • 1
    Most important issue is the indentation of the caption. It has always been indented, so this is what's preferred (hence hang option). I will try your recommendations regarding ragged2e package. I'm also thinking right now, maybe that package has to go. In other document types we have, it's not used. Maybe it's easier to wait and see if problems arise (bad optics in tables for example) by not having it than fixing lots now for one package I'm not even 100% sure we need it.
    – Seban
    Commented May 29 at 13:41
  • 1
    Regarding ansinew I stated above: In our document is a comment "The two packages belong together in order to be able to enter umlauts and similar German ‘special characters’ directly" (regarding inputenc and fontenc). However I checked another document from another team and they use \usepackage[utf8]{inputenc} while also using umlauts. Since I have problems with partial commits in VS Code because of encoding, I might switch to utf8 given I find time.
    – Seban
    Commented May 29 at 13:43
  • 1
    The fix width of caption to width of table feature is lovely, I keep that in mind, thanks
    – Seban
    Commented May 29 at 13:44
  • 1
    I found a solution thanks to your advice regarding the newcommands option for raggede2 and using \Centering. I post an answer to my question
    – Seban
    Commented May 29 at 15:02
2

As supplement to nice @Mico answer (+1).

With use of the talltblr of `tabularray code and considering that caption of tables are usually above table, the MWE is simple and short:

\documentclass{scrreprt}
\usepackage[T1]{fontenc}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
    \begin{table}
    \centering
\begin{talltblr}[
caption={A long table caption that goes over several lines so that we can see the error of weirdly indented captions. This line is fine. This line is fine.}
                ]{width=0.6\textwidth,
                  colspec = { X[c]X[c]}
                 }
    \toprule
{Column 1\\ More text}  &   {Column 2\\ More text} \\   
    \midrule
  A   & B \\
  C   & D \\
    \bottomrule
\end{talltblr}
    \end{table}
\end{document}

As you can see, with this solution you not need to load caption package. Also design of table is a bit changes. For horizontal lines are used rules defined in the booktabs package (which gives to table more "professional" looks 😊).

enter image description here

In the case, that table caption had to be below table, than the table code is a bit longer (for instructions, which move caption below table):

\documentclass{scrreprt}
\usepackage[T1]{fontenc}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
    \begin{table}
\DefTblrTemplate{firsthead,middlehead,lasthead}{default}{}  % <--- 
\DefTblrTemplate{firstfoot,middlefoot}{default}{}           % <---
\DefTblrTemplate{lastfoot}{default}{\UseTblrTemplate{caption}{default}}

    \centering
\begin{talltblr}[
caption={A long table caption that goes over several lines so that we can see the error of weirdly indented captions. This line is fine. This line is fine.}
                ]{width=0.6\textwidth,
                  colspec = { X[c]X[c]}
                 }
    \toprule
{Column 1\\ More text}  &   {Column 2\\ More text} \\   
    \midrule
  A   & B \\
  C   & D \\
    \bottomrule
\end{talltblr}
    \end{table}
\end{document}

enter image description here

1
  • 1
    Thank you for this helpful answer. However it seems to be too many changes to be made to lots of tables and figures, especially since I can't just change layout related things of a document that looks like it does for a long time now. The fix width of caption to width of table feature is lovely though, I keep that in mind, thanks
    – Seban
    Commented May 29 at 13:33
1

Thanks to everyone's help I found a solution:

  • Change \usepackage[newcommands]{ragged2e} to \usepackage{ragged2e}
  • Change \newcolumntype{C}{>{\centering\arraybackslash}X} to \newcolumntype{C}{>{\Centering}X}
\documentclass{scrreprt}

\usepackage[T1]{fontenc}
%\usepackage[ansinew]{inputenc}
\usepackage{tabularx}
\usepackage[format=hang,singlelinecheck=false]{caption}

\usepackage{ragged2e}  % comment in / out

\newcolumntype{C}{>{\Centering}X}

\begin{document}

\begin{table}
    \centering  % \Centering here makes indentation weird again

    \begin{tabularx}{0.8\textwidth}{CC}\hline
    
        Column 1\newline More text  & Column 2\par More text    \\\hline\hline
        
        A   & B \\
        C   & D \\\hline
        
    \end{tabularx}
    \caption[]{A long table caption that goes over several lines so that we can see the error of weirdly indented captions\\ This line stays to the left\par This line is fine}
\end{table}

\end{document}
3
  • 3
    As Mico has shown in his answer, you don't need \arraybackslash with \Centering. So IMHO there is nothing new in this answer. Sorry.
    – cabohah
    Commented May 29 at 15:45
  • 2
    BTW: \usepackage[ansinew]{inputenc} is wrong at TeX.SX, because TeX.SX encodes the code in UTF8, as most LaTeX editors also do by default and which is also the default of LaTeX with every engine for more than 10 years.
    – cabohah
    Commented May 29 at 15:47
  • @cabohah Yes I just realised while fixing my main document, I should have taken out the \arraybackslash as well, fixed it. The \centering within the table environment has to stay a \centering, \Centering there messes with the indentation of the caption.
    – Seban
    Commented May 29 at 15:50
1

Just for fun:

\documentclass{scrreprt}

%\usepackage[T1]{fontenc}
%\usepackage[ansinew]{inputenc}
\usepackage{tabularx}

\usepackage[format=hang,singlelinecheck=false]{caption}

\usepackage[newcommands]{ragged2e}  % comment in / out

\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}
    \centering  % also \Centering (package ragged2e) is not working

    \begin{tabularx}{0.8\textwidth}{CC}\hline
    
        Column 1\newline More text  & Column 2\par More text    \\\hline\hline
        
        A   & B \\
        C   & D \\\hline
        
    \end{tabularx}
    \caption[]{\parbox[t]{\dimexpr \linewidth-\hangindent}{A long table caption that goes over several lines so that we can see the error of weirdly indented captions\\ This line stays to the left\par This line is fine\hrule}}
2
  • It works after fixing the width of the parbox. Seems a bit quick and dirty though
    – Seban
    Commented May 29 at 13:30
  • 1
    @Mico - I was trying to get rid of error message while using pdflatex. Commented May 29 at 17:37

You must log in to answer this question.

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