5

What is the correct way to determine the correct size for use with p{} needed by \multicolumn to get the size correct?

The problem is this: I want to use tabularx and also use \multicolumn. To do that, I have to give \multicolumn an exact size to use. And this is the problem. In complicated table, where I can have one table inside another, and a row that extends to few columns, the calculations are getting hard.

It become harder to figure the correct size, and if I change the table later, I have to change the code again.

The first example below shows that X can't be used with \multicolumn to make it extend to more than one column:

\documentclass[10pt,notitlepage]{article}%
\usepackage{tabularx}
\usepackage{array}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage{lipsum}
\setlength{\parindent}{0pt}
\begin{document}

\begin{tabularx}{\textwidth}{@{}|l|Y|@{}}\hline
 column 1 &
 {\begin{tabularx}{\linewidth}{@{}Y|Y|Y@{}}\hline
      \lipsum[75] & \lipsum[75] &  \lipsum[75] \\\hline
      \multicolumn{3}{X}{\lipsum[75]}
  \end{tabularx}
 }
 \\\hline
\end{tabularx}
\end{document}

Mathematica graphics

So have to use p{}. Nothing else would let it extend to the other columns.

But p wants a size. after some trial and error, here is one that gets close to the size needed

\documentclass[10pt,notitlepage]{article}%
\usepackage{tabularx}
\usepackage{array}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage{lipsum}
\setlength{\parindent}{0pt}
\begin{document}

\begin{tabularx}{\textwidth}{@{}|l|Y|@{}}\hline
 column 1 &
 {\begin{tabularx}{\linewidth}{@{}Y|Y|Y@{}}\hline
       \lipsum[75] &  \lipsum[75] &   \lipsum[75] \\\hline
      \multicolumn{3}
        %this calculation below is needed, and not sure if it is correct now
        {p{.9\dimexpr \textwidth-4\arrayrulewidth-8\tabcolsep\relax}}{\lipsum[75]}
  \end{tabularx}
 }
 \\\hline
\end{tabularx}
\end{document}

Mathematica graphics

Ok, it is better now. But if I change something later, I have to go change .9 to something else I am sure.

The question is: Is this really how one is supposed to do this in Latex? It seems that Latex should have a way to do this automatically. Is there a better approach I should be looking at instead of what I am doing above?

The above is a small example. I want to use tabularx and have other tabularx tables inside as well. So this can get complicated very quickly.

5

You don't need multicolumn here, your text is just a paragraph the full width of the outer table column. (Incidentally as I've commented before I wouldn't use tabularx for the inner column: in a table where every column is an X column, you know in advance the column widths, so there is no need to make tabularx work them out (although it works).

\documentclass[10pt,notitlepage]{article}%
\usepackage{tabularx}
\usepackage{array}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage{lipsum}
\setlength{\parindent}{0pt}
\begin{document}

\begin{tabularx}{\textwidth}{@{}|l|Y|@{}}\hline
 column 1 &
 {\begin{tabularx}{\linewidth}{@{}Y|Y|Y@{}}\hline
       \lipsum[75] &  \lipsum[75] &   \lipsum[75] \\\hline
  \end{tabularx}
 }

\lipsum[75]
 \\\hline
\end{tabularx}
\end{document}
2

This is a way to achieve the objective. The default size of each single column is \hsize. To change it, use >{\setlength\hsize{1\hsize}}. As to the 3-column width, one needs to set it to 3 times the basic \hsize because it takes up 3 columns.

enter image description here

Code:

\documentclass[10pt,notitlepage]{article}%
\usepackage{tabularx}
\usepackage{array}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage{lipsum}
\setlength{\parindent}{0pt}
\begin{document}

\begin{tabularx}{\textwidth}{@{}|l|Y|@{}}\hline
 column 1 &{
\begin{tabularx}{\linewidth}{Y|Y|Y}%
\lipsum[75] &\lipsum[75] & \lipsum[75] \\ \hline
\multicolumn{3}{>{\centering\setlength\hsize{3\hsize}}Y}{\lipsum[75]}
\end{tabularx}}\\ \hline
\end{tabularx}
\end{document}
  • 1
    You're forgetting the space taken by the rules and the intercolumn spaces, so \setlength{\hsize}{\dimexpr3\hsize+2\arrayrulewidth+4\tabcolsep\relax} should give a better result. – egreg Dec 25 '13 at 14:56
  • @egreg Are you referring to the setting '\setlength\hsize{1\hsize}}X@{}' that I should consider ? – Jesse Dec 25 '13 at 15:02
  • I'm referring to the call inside \multicolumn. – egreg Dec 25 '13 at 15:05
  • @egreg Just tried your suggestion, both look the same to my naked eyes. Your suggestion will keep in mind, thanks. – Jesse Dec 25 '13 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.