# Create Table with bold aligned heading and auto adjusting column width

I want to create a 2 column table with bold headings which are center-aligned while the rows will be left aligned. Since my left column contains long text I want the width of the text to be adjusted automatically depending on the page width so that the whole table can easily fit into the page.

Below is the code which does everything apart from adjusting the column width.

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|}
\hline
\multicolumn{1}{|c|}{\textbf{Column 1}}
& \multicolumn{1}{c|}{\textbf{Column 2}} \\ \hline
long text long text long text long text long text long text long text long
text long text long text long text long text long text long text long text
long text & Row 1                                  \\ \hline
\end{tabular}
\end{table}


I know tabularx can do this. But don't know how to do this. Any help?

This is how you could use tabularx:

\documentclass[]{article}

\usepackage{tabularx}

\begin{document}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{|X|X|}
\hline
\multicolumn{1}{|c|}{\textbf{Column 1}}
& \multicolumn{1}{c|}{\textbf{Column 2}} \\ \hline
long text long text long text long text long text long text long text long
text long text long text long text long text long text long text long text
long text & Row 1                                  \\ \hline
\end{tabularx}
\end{table}
\end{document}


If you know for sure, that the second column is not going to contain much text, you might change the code to:

\documentclass[]{article}

\usepackage{tabularx}

\begin{document}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{|X|l|}
\hline
\multicolumn{1}{|c|}{\textbf{Column 1}}
& \multicolumn{1}{c|}{\textbf{Column 2}} \\ \hline
long text long text long text long text long text long text long text long
text long text long text long text long text long text long text long text
long text & Row 1                                  \\ \hline
\end{tabularx}
\end{table}
\end{document}


In a tabularx environment you define the width of the table with the first argument and can use the column specifier X, which is a column which takes as much space as necessary to fill the specified width.

Also I would enhance the table visually by not using vertical lines and utilizing the booktabs package:

\documentclass[]{article}

\usepackage{tabularx}
\usepackage{booktabs}

\begin{document}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{X l}
\toprule
\multicolumn{1}{c}{\textbf{Column 1}}
& \multicolumn{1}{c}{\textbf{Column 2}} \\
\midrule
long text long text long text long text long text long text long text long
text long text long text long text long text long text long text long text
long text & Row 1                                  \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}


Fourth variant, providing similar results to the second but with fixed width column (I'll put only the tabularx environment here, not the complete MWE):

\begin{tabularx}{\linewidth}{|X|p{2cm}|}
\hline
\multicolumn{1}{|c|}{\textbf{Column 1}}
& \multicolumn{1}{c|}{\textbf{Column 2}} \\ \hline
long text long text long text long text long text long text long text long
text long text long text long text long text long text long text long text
long text & Row 1                                  \\ \hline
\end{tabularx}

• I would prefer the second option you have provided. In this case is it possible to specify the width of the second column so that it can also break the text into multiple lines if the text width exceeds the column width? – Majis Aug 25 '17 at 15:06
• @Majis of course it is, see my edit! – Skillmon Aug 25 '17 at 15:25
• now everything is working fine apart from one warning: "underfull \hbox (badness 10000)". It seems that the warning occurs due to justified alignment. How can I change the alignment? Is there any other alternative to overcome this warning? – Majis Aug 25 '17 at 16:30
• @Majis You could change the X column to >{\raggedright}X which inserts \raggedright to all cells of that column. You could also change the font aspects, so that it stretches more (but this won't look good I guess). – Skillmon Aug 25 '17 at 16:33
• @Majis the same is true for the right column, replace p with >{\raggedright}p. > is introduced by the array package and its argument is a macro sequence which should be executed at the beginning of each cell of a column. There also is < which is takes a command which should be executed after the contents of each cell. – Skillmon Aug 25 '17 at 19:54