2

I have the code

 \begin{center}
    \begin{tabular}{p{0.4\linewidth}p{0.4\linewidth}}
        \toprule
        \multicolumn{1}{c}{column 1} & \multicolumn{1}{c}{column 2} \\ \midrule
        should be centered &  should be centered\\
        should be centered & should be centered\\
        \\
        Should be left aligned
        & 
        Should be left aligned\\
        \bottomrule
    \end{tabular}
\end{center}

This gives me the following table

enter image description here

How do I center some lines but not others?

  • 1
    You already use the syntax \multicolumn{1}{c}{column 1}, which you can apply to other entries as well. – user121799 Sep 16 '18 at 5:44
  • Please indicate whether automatic line breaking should be allowed in the centered and left-aligned cells. – Mico Sep 16 '18 at 8:21
6

A very simple way is to repeat what you have done in the first row. Or, since there are more centered than left-aligned rows, you could also just use \multicolumn for the left-aligned rows.

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{center}
    \begin{tabular}{p{0.4\linewidth}p{0.4\linewidth}}
        \toprule
        \multicolumn{1}{c}{column 1} & \multicolumn{1}{c}{column 2} \\ \midrule
        \multicolumn{1}{c}{should be centered} &  \multicolumn{1}{c}{should be
        centered}\\
        \multicolumn{1}{c}{should be centered} & \multicolumn{1}{c}{should be
        centered}\\
        \\
        Should be left aligned
        & 
        Should be left aligned\\
        \bottomrule
    \end{tabular}
\end{center}
\begin{center}
    \begin{tabular}{cc}
        \toprule
        column 1 & column 2 \\ \midrule
        should be centered &  should be
        centered\\
        should be centered & should be
        centered\\
        \\
        \multicolumn{1}{p{0.4\linewidth}}{Should be left aligned}
        & 
        \multicolumn{1}{p{0.4\linewidth}}{Should be left aligned}\\
        \bottomrule
    \end{tabular}
\end{center}
\end{document}

enter image description here

6

I'm assuming that since you're using the p column type for both main columns, automatic line breaking should be allowed regardless of whether the lines are left-aligned or centered. The following solution achieves this formatting goal.

Note that the \RaggedRight directive, which is used in the definition of the L column type, induces left-alignment (while still allowing hyphenation, if needed). In contrast, the basic p column type performs full justification, not left-alignment.

enter image description here

\documentclass{article}
\usepackage{booktabs,array,ragged2e}
% define 2 column types that perform centering and left-alignment, resp.
\newcolumntype{C}[1]{>{\Centering\arraybackslash}p{#1\linewidth}}
\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash}p{#1\linewidth}}
\newcommand\mC[2]{\multicolumn{1}{C{#1}}{#2}} % handy shortcut macro

\begin{document}
\begin{center}
    \begin{tabular}{L{0.4}L{0.4}}
      \toprule
      \multicolumn{1}{c}{Column 1} & \multicolumn{1}{c}{Column 2} \\ 
      \midrule
      \mC{0.4}{Should be centered while allowing line breaks} & 
      \mC{0.4}{Should be centered while allowing line breaks}\\
      \mC{0.4}{Should be centered while allowing line breaks} & 
      \mC{0.4}{Should be centered while allowing line breaks}\\
      \\
      Should be left-aligned while allowing line breaks & 
      Should be left-aligned while allowing line breaks\\
    \bottomrule
  \end{tabular}
\end{center}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.