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

  • Welcome to TeX.SE. Please tell us what you've tried so far. – Mico Jan 25 '20 at 05: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. – howstheJosh Jan 25 '20 at 06:20
  • 1
    Just use \multicolumn{1}{c}{...} for the cells to be centered, see here for example: https://tex.stackexchange.com/questions/87596/center-only-heading-text-in-a-table – CarLaTeX Jan 25 '20 at 06:53
  • This site: http://www.tablesgenerator.com/# could also be useful. – CarLaTeX Jan 25 '20 at 07:13
  • @CarLaTeX I'm confused with how multicolumn works, is there anything I could read to understand it more clearly? – howstheJosh Jan 25 '20 at 07:22
  • @JoshuaMathewJacob Look at here: https://en.wikibooks.org/wiki/LaTeX/Tables#Rows_spanning_multiple_columns – CarLaTeX Jan 25 '20 at 07:36
  • @CarLaTeX thanks I've got it. I'm now able to centre my headings. But now my table seems to overrun the page. How can I fix that? – howstheJosh Jan 25 '20 at 07:39
  • 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. https://topanswers.xyz/tex?q=606 – CarLaTeX Jan 25 '20 at 07:43
  • @CarLaTeX I've added it to the body of the post – howstheJosh Jan 25 '20 at 08:15
  • Off-topic: 8pt is not a recognized option for the book document class, and will thus get ignored. The book class recognizes only three options regarding the main document font size: 10pt, 11pt, and 12pt. You'll need a different class, or load a package that does its own font size calculations, if 8pt is supposed to be the main font size for the entire document. – Mico Jan 25 '20 at 08:53
  • @CarLaTeX thank you very much! btw can you also tell me how to make numbered lists inside the cells of a table? Edit on this sample of code itself if possible.... – howstheJosh Jan 25 '20 at 09:04
  • @Mico Thanks for the info. I’ve a lot to learn. A doubt tho: if its ignored will the default be 12pt? – howstheJosh Jan 25 '20 at 09:06
  • @JoshuaMathewJacob - No, the default is 10pt. – Mico Jan 25 '20 at 09:07
  • @Mico thanks for all the help – howstheJosh Jan 25 '20 at 09:09

1 Answers1

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

CarLaTeX
  • 62,716