28

When I try to use multicolumn inside a tabularx environment, I get this weird behaviour; where the column doesn't fill the whole width.

Anyone know why this is happening, and how I can fix it?

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

\begin{document}

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

    \hline
    \multicolumn{2}{|X|}{\sffamily Lorem ipsum dolor sit amet,}\\\hline
    \multicolumn{2}{|X|}{\lipsum[2]}\\\hline

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

\end{tabularx}

\end{document}

tabularx with multicolumn

5 Answers 5

25

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.

6
  • 1
    Now when I try to add \rowcolor, it won't fit the whole column. Do you know anything about that? Commented Mar 31, 2015 at 13:08
  • \multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep}X|}{...} or you have too short lines.
    – egreg
    Commented Mar 31, 2015 at 13:18
  • @egreg I was editing. Thank you. :-)
    – user11232
    Commented Mar 31, 2015 at 13:21
  • @DavidKristiansen Please see the update.
    – user11232
    Commented Mar 31, 2015 at 13:21
  • I tried to make a custom column type with \newcolumntype{Y}[1]{|>{\hsize=\dimexpr#1\hsize+#1\tabcolsep+\arrayrulewidth\relax}X|} I see It's more going on than I thought. Thanks, I can work from here! Commented Mar 31, 2015 at 13:37
6

I built on top of the accepted answer and made a \newcommand for myself to restore easy multicolumns

\newcommand{\tabularxmulticolumncentered}[3] 
    {\multicolumn{#1}
                 {>{\centering\hsize=\dimexpr#1\hsize+#1\tabcolsep+\arrayrulewidth\relax}#2}
                 {#3}}

Use as

\tabularxmulticolumncentered{2}{X}{My text spanning two columns}
1
  • 2
    Great contribution ! But I used a 12 column table and it looks a bit cropped so I used this expression {|>{\hsize=\dimexpr#1\hsize+\tabcolsep * (2 * (#1 - 1) )+\arrayrulewidth* (#1 - 2)\relax}#2|} Commented Feb 23, 2022 at 5:54
4

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

2
  • It's just a MWE of a larger form I'm making. You can see the whole thing here link. Your code looks greek to me. I'm guessing this is LaTeX3, and something I will have to learn eventually. Might as well dive into it. Commented Mar 31, 2015 at 13:57
  • @DavidKristiansen I suspected something like that. In that case you just need 19 keys and make a tabular from them. Maybe you can change that question asking how to fill and print the form with a key-value interface; the mention to the 19 arguments can stay, just ask for a different method. When you have edited it, I'll vote for reopening.
    – egreg
    Commented Mar 31, 2015 at 14:04
4

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

3

You can use hspan=minimal with tabularray package:

\documentclass[12pt]{article}

\usepackage{tabularray,lipsum}

\begin{document}

\begin{tblr}{
  colspec = {|X|X|},
  row{1,3} = {font=\sffamily},
  cell{1,2}{1} = {c=2}{l}, % multicolumn
  hspan = minimal,
}
\hline
  Lorem ipsum dolor sit amet & \\
\hline
  \lipsum[2] & \\
\hline
  Lorem: & Ipsum: \\
  Foo & Bar\\
\hline
\end{tblr}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .