Text FormattingTable with full Text Width and merged Cells

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
ojeromos
Posts: 1
Joined: Sat Mar 23, 2013 12:38 am

Table with full Text Width and merged Cells

Post by ojeromos »

Hello!

I am newcomer here and I am facing a problem with the tabularx package. I would like to create tables with full text width. If I merge some columns with \multicolumn, somehow the number of columns times \hsize is not equal to \textwidth. Therefore those items which are centralized in a \multicolumn entry are not on the "center". What I want is that A and B should be under the bold III, C should be under II and D between IV and V. In a previous post I have found that an equivalent amount of \tabcolsep should be added to \hsize, however this does not solve the problem. If I put a higher value for \hsize than the number of merged cells (last row of the table), I can achieve the wanted result, however I do not find this solution very nice.

Code: Select all

\documentclass{article}
\usepackage{tabularx}

\newcolumntype{C}[1]{>{\setlength{\hsize}{#1\hsize}\addtolength{\hsize}{#1\tabcolsep}\centering\arraybackslash}X}%

\begin{document}
\noindent
\begin{table}
\caption{Caption}
\label{tab:table1}
\begin{tabularx}{\textwidth}{ C{1} C{1} C{1} C{1} C{1} C{1} }
    \strut & \textbf{I} & \textbf{II} & \textbf{III} & \textbf{IV} & \textbf{V} \\
    \hline
    \strut & \multicolumn{5}{C{5}}{A}                                           \\
    \strut & \multicolumn{3}{C{3}}{C}                & \multicolumn{2}{C{2}}{D} \\
    \hline
    \multicolumn{6}{C{7.5}}{some text here}
\end{tabularx}
\end{table}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Post Reply