27

The \arraybackslash was used on answer table at How to auto adjust my last table column width, and why is there Underfull \vbox badness on this table?

\documentclass[10pt,a5paper,twoside]{memoir}
\usepackage[T1]{fontenc}

\usepackage[utf8]{inputenc}
\usepackage[brazil]{babel}

\usepackage{ragged2e}
\usepackage[showframe,pass]{geometry}

\usepackage{ltablex}
\keepXColumns

\begin{document}
\frenchspacing

\setlength\extrarowheight{2pt}
\begin{tabularx}{\linewidth}{|>{\RaggedRight}p{3cm}|>{\arraybackslash}X|}

    \caption{Formatação do texto}
    \label{tab:a_table_formatacao_de_texto} \\
    \hline
    \endfirsthead

    \multicolumn{2}{c}{\tablename~\thetable: Formatação do texto (continued)} \\
    \hline
    \endhead

    \hline
    \multicolumn{2}{r}{\footnotesize to be continued}
    \endfoot

    \hline
    \endlastfoot

    Cor                          & Branco -                              \\ \hline
    Formato do papel             & A5                                    \\ \hline
    Gramatura                    & 75                                    \\ \hline
    Cabeçalho                    & 0,7                                   \\ \hline
    Impressão                    & Frente e verso                        \\ \hline
    Margens                      & Espelhadas: superior 2, Inferior: 1,5 \\ \hline
    Cabeçalho                    & 0,7                                   \\ \hline
    Rodapé                       & 0,7                                   \\ \hline
    Paginação                    & Externa                               \\ \hline
    Alinhamento vertical         & Superior                              \\ \hline
    Cabeçalho                    & 0,7                                   \\ \hline
    Alinhamento do texto         & Justificado                           \\ \hline
    Cor                          & Branco -                              \\ \hline
    Formato do papel             & A5                                    \\ \hline
    Cabeçalho                    & 0,7                                   \\ \hline
    Gramatura                    & 75                                    \\ \hline
    Impressão                    & Frente e verso                        \\ \hline
    Margens                      & Espelhadas: superior 2, Inferior: 1,5 \\ \hline
    Rodapé                       & 0,7                                   \\ \hline
    Paginação                    & Externa                               \\ \hline
    Alinhamento vertical         & Superior                              \\ \hline
    Alinhamento do texto         & Justificado                           \\ \hline

\end{tabularx}

\end{document}

enter image description here

On the question Difference between \\ and \tabularnewline there is some explaining about it:

\arraybackslash: Restore \\ for use in array and tabular environment (after \raggedright etc.).

 \def\arraybackslash{\let\\\tabularnewline}

Why \\ is broken by \raggedright and these etc other commands?

Reading the line \def\arraybackslash{\let\\\tabularnewline} seems the \\ is being redefined as \tabularnewline, does \raggedright and these etc other commands redefine it as something else, why?

Does I need to put \arraybackslash only on my last table column? What happens if I do not put it or put on/only a middle column or put on every column?

user
  • 4,745
  • 4
    \raggedright redefines \ to be \@centercr, which is itself different from the regular definition of \ in regular text (which is different from its definition inside a tabular environment.) You might want to read the documentation for the LaTeX kernel. (texdoc source2e will bring up the file.) – Alan Munn Aug 22 '17 at 22:23

2 Answers2

26

As with almost everything in TeX, control sequences (macros and control words) can be redefined, and because local grouping allows for local redefinition, the definitions of macros within a particular environment can be different from the definitions outside of that environment. This is the case of \\, which has multiple definitions depending on the environment.

In normal text, \\ is defined as:

\let \reserved@e \relax \let \reserved@f \relax \@ifstar 
 {\let \reserved@e \vadjust \let \reserved@f \nobreak \@xnewline }
 \@xnewline

Inside a tabular environment, it is defined as:

 \@tabularcr

which expands to:

 {\ifnum 0=`}\fi \@ifstar \@xtabularcr \@xtabularcr

Inside a raggedright environment, \\ is redefined to be

 \@centercr

which expands to:

 \ifhmode \unskip \else \@nolnerr \fi \par \@ifstar {\nobreak \@xcentercr }\@xcentercr

Since these definitions are different, after using a \raggedright environment in a tabular you need to use \arraybackslash to restore it to its tabular definition.

In addition to the question you linked to, see also:

Alan Munn
  • 218,180
3

Some more explanation is probably useful in this case. As with most things, usually you have to look into the source code of LaTeX to figure things out.

Why must \centering redefine \\ in the first place?

To answer this we need:

  • the main reason why \centering works is that it sets \leftskip=0pt plus 1fil and \rightskip being the same value. (related read; source code)
  • in normal text \\ is implemented to basically do \hfil\break where \break is basically \penalty-10000. (source code).
  • in \centering \\ basically do \par (technically \unskip\par\addvspace{-\parskip}). (source code)

We will just try to use the normal definition of \hfil\break and see what goes wrong.

%! TEX program = lualatex
\documentclass{article}
\usepackage{lua-visual-debug}
\begin{document}

{ \centering Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget,\hfil\break consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium \par quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci

} \end{document}

Output:

output

The line right before \hfil\break has the space on the right twice that of the space on the left, because the total skip on the left is \leftskip=0pt plus 1fil and the total skip on the right is \rightskip=0pt plus 1fil plus 1fil added by the \hfil, so 0pt plus 2fil.


It probably would have been better for \centering to check if it's inside tabular before redefining \\, but it probably won't be changed now because of historical reason.

Another note, \centering etc. does not redefine \newline (which is the same as no-star \\ in normal text), so \newline in centering environment will have the issue above. How to create fixed width table columns with text raggedright/centered/raggedleft?

user202729
  • 7,143