2

I would like to center the content of certain cells (actually the whole first row) of my table, while the rest of the table is left-aligned.

It works with multicolumn, but when using multicolumn it does not break the content of the cell and puts all the text in one line.

I wonder if there is not a simple command, such as {\centering} (which I tried and doesn't work), without the "detour" via multicolumn?

\begin{table}[h] 
    \centering
    \label{example}
    \noindent\begin{tabular}{>{\raggedright\arraybackslash}m{3cm}|>{\raggedright\arraybackslash}m{3cm}|>{\raggedright\arraybackslash}m{3cm}|}\hline 
     here I want centering and line breaking  &here too  &here too \\ \hline
     the rest aligned on the left & left1 & left2\\ \hline
     3 & 4 & 5 \\ \hline
     \end{tabular}
     \end{table}
5
  • 1
    See if this could help you, for example: tex.stackexchange.com/a/280006/101651. Otherwise, please add a minimal working example (MWE).
    – CarLaTeX
    Dec 4, 2018 at 16:06
  • 1
    if the column is a p column \centering will work, if it was an l column then line breaking is disabled anyway and using \multicolumn{1}{c} does not change that, so with no example code it is very hard to understand your question. Dec 4, 2018 at 16:42
  • 1
    You wrote, "but when using multicolumn it does not break the content of the cell and puts all the text in one line". That's not necessarily true. Please tell us how the columns are defined at present.
    – Mico
    Dec 4, 2018 at 17:09
  • I tried using: \multicolumn{1}{>{\centering\arraybackslash}m{3cm}}{text}, but I do not understand what is wrong with it?
    – Fraenzine
    Dec 5, 2018 at 16:00
  • 1
    @Fraenzine - There shouldn't be any issue with the command you list, except that you may want to have a vertical bar at the right-hand edge of the cell -- in which case you should write \multicolumn{1}{>{\centering\arraybackslash}m{3cm}|}{text}.
    – Mico
    Dec 5, 2018 at 18:06

2 Answers 2

3

I don't understand your apparent aversion to using \multicolumn to override the default column type on a cell-by-cell basis.

enter image description here

\documentclass{article}
\usepackage{array} % for "\newcolumntype" directive
%% Argument of 'M' and 'N' col. types: usable col. width, in cm
\newcolumntype{M}[1]{>{\raggedright\arraybackslash}m{#1cm}}
\newcolumntype{N}[1]{>{\centering\arraybackslash}m{#1cm}}
%% Two convenient shortcut macros
\newcommand{\mC}[2]{\multicolumn{1}{ N{#1}|}{#2}}
\newcommand{\mD}[2]{\multicolumn{1}{|N{#1}|}{#2}}

\begin{document}
\begin{table}[h] 
\centering
\caption{An example}
    \label{tab:example}
    \begin{tabular}{|M{3}|M{2.5}|M{3.5}|}
     \hline 
       \mD{3}{here I want centering and line breaking} 
     & \mC{2.5}{here too} 
     & \mC{3.5}{here too} \\
     \hline
     the rest aligned on the left, with line breaking 
     & left1 & left2\\ 
     \hline
     3.0 & 2.5 & 3.5 \\ 
     \hline
     \end{tabular}
\end{table}
\end{document}
2
  • This looks good, thank you! Can you please explain me how you managed to make it break the line? Is it the \arraybackslash command that you use for defining the columntype? Another question, what is the \mD or \mC doing? I am relatively new to Latex, so I am a little confused.
    – Fraenzine
    Dec 5, 2018 at 15:46
  • 1
    @Fraenzine - The use of the m column type in the \newcolumntype definitions of N and M allows line breaks. The \arraybackslash directive is there purely to bypass a deep-seated LaTeX issue which would otherwise affect the final (i.e, right-hand-most) column in a table. The macros \mC and \mD, which both take two arguments, are handy shortcuts: They save you from the tedium of having to write \multicolumn{1}{ N{<some number>cm}|}{<some text>} and \multicolumn{1}{ |N{<some number>cm}|}{<some text>}, respectively. Isn't it easier to write, say, \mC{<some number>}{<some text>}?
    – Mico
    Dec 5, 2018 at 18:09
1

Alternatively, you can use the \Centering, \RaggedLeft and\RaggedRight commands from the package ragged2e. Among other things, these commands are modified so that they preserve the present meaning of \\ (which is different if you're inside a tabular environment), unlike their basic LaTeX equivalents \centering, \raggedleft, \raggedright.

\documentclass{article}
\usepackage{array}
\usepackage{ragged2e}

\begin{document}
\begin{table}[h] 
    \centering
    \label{example}
    \noindent
    \begin{tabular}{|>{\RaggedRight}m{3cm}|>{\RaggedRight}m{3cm}|>{\RaggedRight}m{3cm}|}
        \hline 
        \Centering here I want centering and line breaking  & \Centering here too  & \Centering here too \\ \hline
        the rest aligned on the left & left1 & left2\\ \hline
        3 & 4 & 5 \\ \hline
    \end{tabular}
\end{table}
\end{document}

You must log in to answer this question.

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