[Tex/LaTex] Wrapping column text using multicolumn and tabularx

tabularx

I have two tables I am typesetting above each other as follows.

\documentclass[a4paper,11pt]{article}
\usepackage{tabularx}
\usepackage[left=2cm, right=2cm, bmargin=1.5cm]{geometry}

\begin{document}
\begin{tabularx}{\textwidth}{| X | X | l | l | l | l | l | l |}
\hline
   & Cost Category & Year 1 & Year 2 & Year 3 & Year 4 & Year 5 & Total (Y1-5) \\
\hline
\noalign{\vspace{0.5cm}}
\hline
\multicolumn{7}{|l|}{Some text that goes on and on and on and on and on and on and on and on and on and on} & $100\%$\\
\hline
\end{tabularx}
\end{document}

How can I get the text in the bottom table to word wrap rather than push the column over the right hand edge of the page? Basically, I would like it to take on whatever size the top table has already set for the first 7 columns.

Best Answer

the width of the first seven column is \textwidth minus the last column and the \tabcolsep/\fboxrule: \textwidth-4\tabcolsep-\widthof{Total (Y1-5)}-2\fboxrule}. Without tabularx the rules are not taken into account.

\documentclass[a4paper,11pt]{article}
\usepackage{tabularx,calc}
\usepackage[left=2cm, right=2cm, bmargin=1.5cm]{geometry}

\begin{document}
\begin{tabularx}{\textwidth}{| X | X | l | l | l | l | l | l |}
\hline
   & Cost Category & Year 1 & Year 2 & Year 3 & Year 4 & Year 5 & Total (Y1-5) \\
\hline
\noalign{\vspace{0.5cm}}
\hline
\multicolumn{7}{|p{\textwidth-4\tabcolsep-\widthof{Total (Y1-5)}-2\fboxrule}|}{Some text that goes on and on and on and on and on and on and on and on and on and on} & $100\%$\\
\hline
\end{tabularx}
\end{document}

Something like \multicolumn{7}{|X|}{ is possible but makes no sense. It uses the width of all 7 cells but not for the text itself.

Related Question