1

I'm trying to align vertically the content of cells, for which I'm using the multicolumn command. The problem is that I need to set the width of the text for the m parameter, but I don't know how to get this if the column has an X attribute for expanding that column till the width of the page (\columnwidth doesn't work, and \linewidth gives me too much width). The code I have so far is

\documentclass[10pt,a4paper]{article}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} % use color
\usepackage{booktabs} % commands for table rules
\usepackage{tabularx}
\usepackage{lipsum}

\newcommand{\scell}[1]{\cellcolor{black!25} \bfseries #1 }
\newcommand{\slcell}[1]{\cellcolor{black!25} #1 }


\begin{document}

\noindent
{\renewcommand{\arraystretch}{1.3}% Spread rows out...
\begin{tabularx}{\linewidth}{|>{\arraybackslash}m{2.4cm}|>{\arraybackslash}X|>{\centering\arraybackslash}m{2.1cm}|}
\hline 
 \scell{Header 1} & \scell{Header 2} &  \scell{ Header 3}  \\
\hline \hline
Cell 1 &\multicolumn{1}{m{4cm}}{ \lipsum[1]}  &  test \\
\hline 
\end{tabularx}
}


\end{document}
5
  • Why do you need to use \multicolumn{m{some length}} since X column type their contents as paragraphs?
    – Bernard
    Commented Oct 3, 2017 at 13:38
  • I need vertical alignment
    – aaragon
    Commented Oct 3, 2017 at 13:39
  • Do you mean centred? top-aligned?
    – Bernard
    Commented Oct 3, 2017 at 13:40
  • Yes, center-aligned vertically, for which I need to use m
    – aaragon
    Commented Oct 3, 2017 at 13:40
  • 1
    Then Mico's solution works fine.
    – Bernard
    Commented Oct 3, 2017 at 13:42

1 Answer 1

3

Instead of writing

\multicolumn{1}{m{<some still-to-be-determined length>}}{\lipsum[1]}

I would execute the instruction

\renewcommand{\tabularxcolumn}[1]{m{#1}}

in the preamble (after loading tabularx, naturally) and then write

\lipsum*[1]

in the cell in question.

Longer explanation, as requested by the OP: The tabularx environment in your example contains 1 column of type X and 2 columns of type m. An X-type column is, by default, just a p-type column whose width is calculated automatically by LaTeX. Importantly, p-type columns have their material top-aligned. As a (probably unexpected!) by-product of this setting, the material in the 2 columns of type m will not be vertically centered in general. (If the height of the material in the X column exceeds the heights of the material in both m columns -- as is the case in your example -- the taller of the 2 m-type columns will be top-aligned, the shorter of the 2 m-type columns will be vertically centered relative to the taller m column, and the placement of the p-type column will be different still. Ouch!) By running the instruction \renewcommand{\tabularxcolumn}[1]{m{#1}}, the underlying type of the X column is changed from p to m, and the desired result comes about, i.e., all 3 columns get vertically centered in the expected way.

Moral of the story: The frequency of puzzling (and likely unexpected and unwanted) placement outcomes drops sharply if the underlying type of all columns is the same, i.e., all-m or all-p. Mixing column types is likely to cause quite some frustration.

A full MWE:

\documentclass[10pt,a4paper]{article}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\newcommand{\scell}[1]{\cellcolor{black!25}\bfseries #1}
\newcommand{\slcell}[1]{\cellcolor{black!25}#1}

\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{m{#1}}

\usepackage{booktabs,lipsum}

\begin{document}

\noindent
\begingroup
\renewcommand{\arraystretch}{1.3} % localize scope of this command
\begin{tabularx}{\linewidth}{% require "\arraybackslash" in final column only
   | m{2.4cm} | X | >{\centering\arraybackslash}m{2.1cm} |}
\hline 
\scell{Header 1} & \scell{Header 2} & \scell{Header 3}\\
\hline 
\hline
Cell 1 & \lipsum*[1] &  test \\
\hline 
\end{tabularx}
\endgroup

\end{document}
2
  • This works nice! But I don't understand why. Could you please briefly explain in the answer?
    – aaragon
    Commented Oct 3, 2017 at 13:41
  • @aaragon - I've posted an addendum to my answer, in which I explain what's going on in a bit more detail.
    – Mico
    Commented Oct 3, 2017 at 14:06

You must log in to answer this question.

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