[Tex/LaTex] hline not filling up in tabularx

horizontal alignmenttablestabularx

I created a table with the following code:

\begin{tabularx}{14cm}{|X|X|X|X|}
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\multicolumn{4}{|p{\linewidth}|}{contentcontentcontentcontentcontentcontentcontentcontent} \\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent 1:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\textbf{contentcontent 3:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\textbf{contentcontent 2:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\multicolumn{4}{|p{\linewidth}|}{contentcontent} \\
\hline
\multicolumn{4}{|l|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline

\end{tabularx}

And for some reason my h-line doesn't fill up fully on the right side.

Table output

What could be the issue? Thanks in advance.

Best Answer

The behavior you are noticing is due to your \multicolumns like

\multicolumn{4}{|p{\linewidth}|}{contentcontent}

In all these cases replace p{\linewidth} with l and the problem is solved.

An example which doesn't work with your settings

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{tabularx,colortbl,xcolor}

\begin{document}

\begin{tabularx}{14cm}{|X|X|X|X|}
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\multicolumn{4}{|p{\linewidth}|}{contentcontentcontentcontentcontentcontentcontentcontent} \\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent 1:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\textbf{contentcontent 3:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\textbf{contentcontent 2:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\multicolumn{4}{|p{\linewidth}|}{contentcontent} \\
\hline
\multicolumn{4}{|l|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\end{tabularx}

\end{document} 

Output

enter image description here

And this is how it should be (replacing p{\linewidth} with l)

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{tabularx,colortbl,xcolor}

\begin{document}

\begin{tabularx}{14cm}{|X|X|X|X|}
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\multicolumn{4}{|l|}{contentcontentcontentcontentcontentcontentcontentcontent} \\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent 1:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\textbf{contentcontent 3:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\textbf{contentcontent 2:}& \multicolumn{3}{c|}{contentcontent}\\
\hline
\multicolumn{4}{|c|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\multicolumn{4}{|l|}{contentcontent} \\
\hline
\multicolumn{4}{|l|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\end{tabularx}

\end{document} 

Output:

enter image description here


Why your code doesn't work? Because your tabularx is 14cm wide and your \linewidth is greater than that. The available space for a p column spanning the whole tabularx is 14cm-2\tabcolsep-2\arrayrulewidth, so if you want to use a p column, it can not be greater than that length. If you want to use a p column with the maximum length, use p{\dimexpr14cm-2\tabcolsep-2\arrayrulewidth\relax}


EDIT (in response to the OP's comment)

If you want automatical linebreaks in your table cells, this can not be done in c and l columns.

At this point, I suggest you to define

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{\dimexpr#1-2\tabcolsep-2\arrayrulewidth}}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{\dimexpr#1-2\tabcolsep-2\arrayrulewidth}}

and replace all c columns with C{<width>} columns and all l columns with P{<width>} columns, as in the following example:

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{tabularx,colortbl,xcolor}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{\dimexpr#1-2\tabcolsep-2\arrayrulewidth}}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{\dimexpr#1-2\tabcolsep-2\arrayrulewidth}}

\begin{document}

\begin{tabularx}{14cm}{|X|X|X|X|}
\hline
\multicolumn{4}{|C{14cm}|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\multicolumn{4}{|P{14cm}|}{contentcontentcontentcontentcontentcontentcontentcontent} \\
\hline
\multicolumn{4}{|C{14cm}|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|C{14cm}|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|C{14cm}|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\textbf{contentcontent:}& \multicolumn{3}{C{10.5cm}|}{contentcontent}\\
\hline
\textbf{contentcontent:}& \footnotesize{contentcontent}& \textbf{contentcontent:} & \footnotesize{contentcontent}\\
\hline
\multicolumn{4}{|C{14cm}|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\textbf{contentcontent 1:}& \multicolumn{3}{C{10.5cm}|}{contentcontent}\\
\hline
\textbf{contentcontent 3:}& \multicolumn{3}{C{10.5cm}|}{contentcontent}\\
\hline
\textbf{contentcontent 2:}& \multicolumn{3}{C{10.5cm}|}{contentcontent}\\
\hline
\multicolumn{4}{|C{14cm}|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\multicolumn{4}{|P{14cm}|}{contentcontent} \\
\hline
\multicolumn{4}{|P{14cm}|}{\textbf{\cellcolor{gray!20}contentcontent}} \\
\hline
\end{tabularx}

\end{document} 
Related Question