Tabularx and multicolumn

You have to specify \hsize like |>{\hsize=2\hsize}X|. If it is for three columns 3\hsize and so on. To be precise, take the \tabcolsep and one \arrayrulewidth in to account like

>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}

Code:

\documentclass[12pt]{article}
\usepackage{tabularx,lipsum}
\usepackage[table]{xcolor}

\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|X|X|}

    \hline
    \multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{\sffamily Lorem ipsum dolor sit amet, }\\\hline
  \rowcolor{blue!40}
    \multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{\lipsum*[2]}\\\hline

    \sffamily Lorem av:&\sffamily Ipsum:\\
    foo&bar \\\hline

\end{tabularx}

\end{document}

enter image description here

As an example, for three columns you have to use

>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}

as in

\documentclass[12pt]{article}
\usepackage{tabularx,lipsum}
\usepackage[table]{xcolor}

\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|X|X|X|}

    \hline
    \multicolumn{3}{|>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{\sffamily Lorem ipsum dolor sit amet, }\\\hline
  \rowcolor{blue!40}
    \multicolumn{3}{|>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{\lipsum*[2]}\\\hline

    \sffamily Lorem av:&\sffamily Ipsum:&\sffamily Ipsum:\\
    foo&bar &bar\\\hline

\end{tabularx}

\end{document}

Here there are three columns, six \tabcolseps and 4 \arrayrulewidths. Of these 2 outer \tabcolseps and 2 outer \arrayrulewidths are retained (inside multi column) while the space for 4 \tabcolseps and 2 \arrayrulewidths have to be used.


Harish Kumar has already given an answer about the specific problem. Here I'd like to show a different way for inputting what seems to be some kind of form.

\documentclass[12pt]{article}
\usepackage{expl3,environ}
\usepackage{lipsum}

\ExplSyntaxOn
\NewEnviron{Form}[1]
 {
  \keys_set:nn { david/form } { #1 }
  \noindent
  \begin{tabular}
   {
    |
    p{ \dim_eval:n { .5\textwidth - 2\tabcolsep } }
    |
    p{ \dim_eval:n { .5\textwidth - 2\tabcolsep } }
    |
   }
  \hline
  \multicolumn{2}
   { | p{ \dim_eval:n { \textwidth - 2\tabcolsep } } | }
   { \sffamily \l_david_form_title_tl }
  \\
  \hline
  \multicolumn{2}
   { | p{ \dim_eval:n { \textwidth - 2\tabcolsep } } | }
   { \BODY }
  \\
  \hline
  \sffamily Lorem av: & \sffamily Ipsum \\ % <--------- CHANGE HERE
  \l_david_form_bottomleft_tl & \l_david_form_bottomright_tl
  \\
  \hline
  \end{tabular}
 }

\keys_define:nn { david/form }
 {% CHANGE THE KEY NAMES!
  title .tl_set:N = \l_david_form_title_tl,
  loremav .tl_set:N = \l_david_form_bottomleft_tl,
  ipsum .tl_set:N = \l_david_form_bottomright_tl,
 }
\ExplSyntaxOff

\begin{document}

\begin{Form}{
  title = {Lorem ipsum dolor sit amet, eu vel enim virtute, modo rebum},
  loremav = foo,
  ipsum = bar
}
Here goes the body of the form, \lipsum*[2]
\end{Form}

\end{document}

I used expl3 for defining the keys and environ for gathering the main body. The definitions are longer, but inputting the form is much easier, because the various parts have well definite places and are not hidden in code.

enter image description here


Here is a way to go, with some improvements (vertical padding of rows):

\documentclass[12pt]{article}
\usepackage[showframe]{geometry}
\usepackage{tabularx,array, lipsum}
\usepackage{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{6pt}
\addparagraphcolumntypes{X}

\begin{document}\noindent
\begin{tabularx}{\linewidth}{|S{X}|S{X}|}
  \hline
  \multicolumn{2}{|>{\hsize=\dimexpr2\hsize- + 2\tabcolsep\relax}S{X}|}{\sffamily Lorem ipsum dolor sit amet,}\\
  \hline
  \multicolumn{2}{|>{\hsize=\dimexpr2\hsize + 2\tabcolsep\relax} S{X}|} {\lipsum[2]} \\
  \hline
  \textsf{Lorem av}:\newline foo & \textsf{Ipsum:}\newline bar \\
  \hline
\end{tabularx}

\end{document} 

enter image description here

Tags:

Tables