1

How can I create tables with multiple alignments as shown? The first row (i.e. the headings) is centre aligned and the subsequent rows are left aligned?

\documentclass[a4paper]{book}
\usepackage{multicol} 
\usepackage{float}


\begin{document}

\begin{table}[H]
\centering

\begin{tabular}{|l|l|}
 \hline
 \multicolumn{1}{|c}{Compressible} & \multicolumn{1}{|c|}{Incompressible}  \\  \hline

aaaaaaaaaaaaaaaaaaaaaaaa & bbbbbbb bbbbbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbbbbbb bbbbbbbbbb bbbbbbbbbb bbbbbb \\
cccccccccccccccccccccccccccc & ddddddddddddddddddddddddddddd  \\  \hline
\end{tabular}


\end{table}


\end{document}

enter image description here

14
  • Welcome to TeX.SE. Please tell us what you've tried so far.
    – Mico
    Commented Jan 25, 2020 at 5:10
  • @Mico My knowledge about formatting tables is limited. But some users stack the tabular function (as in use the tabular within a tabular) to get different alignments under a column. Will that work? It seems too much to do though. Commented Jan 25, 2020 at 6:20
  • 1
    Just use \multicolumn{1}{c}{...} for the cells to be centered, see here for example: tex.stackexchange.com/questions/87596/…
    – CarLaTeX
    Commented Jan 25, 2020 at 6:53
  • This site: tablesgenerator.com/# could also be useful.
    – CarLaTeX
    Commented Jan 25, 2020 at 7:13
  • 2
    @JoshuaMathewJacob Try to create a minimal example of your code which reproduces your problem and add it to your question. Perhaps you used l for column specification, not p{...}, but it's impossible to help you without an MWE. topanswers.xyz/tex?q=606
    – CarLaTeX
    Commented Jan 25, 2020 at 7:43

1 Answer 1

4

I think the easiest way is to use tabularx.

Please note that multicol package is for having a document in two (or more) columns, it is not for \multicolumn in tables.

Please also observe that because I've set the width of the tabularx environment to equal \linewidth, the \centering instruction is no longer needed.

As egreg pointed out in his comment, if the table has no caption, a center environment suffices; if it has a caption, then table should not have the [H] option.

For more professional tables, use booktabs.

\documentclass[a4paper]{book}
\usepackage{tabularx} 
\usepackage{ragged2e}
\usepackage{float}
\usepackage{caption}
\usepackage{booktabs}
\begin{document}
If your table is at a fixed position, you can use 
\texttt{center} environment.
%\begin{table}[H]
\begin{center}
%%\centering %% not needed since width of tabularx env. = \linewidth
\begin{tabularx}{\linewidth}{|l|>{\RaggedRight}X|}
 \hline
 \multicolumn{1}{|c}{Compressible} & \multicolumn{1}{|c|}{Incompressible}  \\  \hline
aaaaaaaaaaaaaaaaaaaaaaaa & bbbbbbb bbbbbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbbbbbb bbbbbbbbbb bbbbbbbbbb bbbbbb \\
cccccccccccccccccccccccccccc & ddddddddddddddddddddddddddddd  \\  \hline
\end{tabularx}
%\end{table}
\end{center}

If your table has a caption, the \texttt{[H]} option should not be used, because it can float.
Table \ref{tab:mytab} is an example
with \texttt{booktabs}.
\begin{table}[htb]
\caption{\label{tab:mytab}A professional table}
\begin{tabularx}{\linewidth}{l>{\RaggedRight}X}
\toprule
\multicolumn{1}{c}{Compressible} & \multicolumn{1}{c}{Incompressible} \\  
\midrule
aaaaaaaaaaaaaaaaaaaaaaaa & bbbbbbb bbbbbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbbbbbb bbbbbbbbbb bbbbbbbbbb bbbbbb \\
cccccccccccccccccccccccccccc & ddddddddddddddddddddddddddddd \\  
\bottomrule
\end{tabularx}
\end{table}
\end{document}

enter image description here

5
  • +1. Please remove the meaningless 8pt document class option from the code block, though.
    – Mico
    Commented Jan 25, 2020 at 9:08
  • Perhaps you should add >{\RaggedRight} in front of X, in the table preamble.
    – Bernard
    Commented Jan 25, 2020 at 10:12
  • You're welcome!
    – Bernard
    Commented Jan 25, 2020 at 10:44
  • If the table has no caption, a center environment suffices; if it has a caption, then table should not have the [H] option.
    – egreg
    Commented Jan 25, 2020 at 14:38
  • @egreg Added, thanks!
    – CarLaTeX
    Commented Jan 25, 2020 at 20:21

You must log in to answer this question.

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