# Center vertically in tabularx with multiple font size and arraystretch

I would like to find a way to make boxes with a colored background, that take the whole width available, with variable text size and variable arraystretch.

For the moment, the best solution I've found is to use tabularx but depending on the combination of text size and arraystretch factor, the text is often not vertically aligned.

Here is an example with 3 text size and 2 arraystretch factors :

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{tabularx}

\begin{document}

\setlength{\arrayrulewidth}{0pt} \renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}}

\section{Arraystretch == 1}

\renewcommand{\arraystretch}{1}
\begin{tabularx}{\linewidth}{X} \rowcolor{lightgray}  \begin{normalsize}Title\end{normalsize}  \end{tabularx}

\begin{tabularx}{\linewidth}{X} \rowcolor{lightgray}  \begin{Large}Title\end{Large}  \end{tabularx}

\begin{tabularx}{\linewidth}{X} \rowcolor{lightgray}  \begin{Huge}Title\end{Huge}  \end{tabularx}

\section{Arraystretch == 1.5}

\renewcommand{\arraystretch}{1.5}
\begin{tabularx}{\linewidth}{X} \rowcolor{lightgray}  \begin{normalsize}Title\end{normalsize}  \end{tabularx}

\begin{tabularx}{\linewidth}{X} \rowcolor{lightgray}  \begin{Large}Title\end{Large}  \end{tabularx}

\begin{tabularx}{\linewidth}{X} \rowcolor{lightgray}  \begin{Huge}Title\end{Huge}  \end{tabularx}

\end{document}


In this example, the first 3 tabularx are not vertically aligned, the text touches the top. With the arraystretch factor set to 1.5, depending on the size of the font, the text is aligned or touches the top.

I would like a solution that always center vertically the text, regardless of the font size and the arraystretch factor.

-

The alignment is out (and you get an overfull box warning) because you have made a paragraphs of three tables, they it starts with a paragraph indent except after a section heading. Put \noindent before them. Or use a declaration such as \centering that avoids paragraph indentation. You can use \extrarowheight to avoid the capital letters touching the tops of the rows.

Also don't do font changes like

 \begin{Huge}Title\end{Huge}


If you make a font change you must always make sure that the end of paragraph or end of box, or end of table cell is in the scope of the font change. Otherwise you get large letters on normally spaced lines. Just use

 \Huge Title


You do not need any extra environment or brace groups as the font chaneg is restricted to the table cell in any case.

-
This code is only a MWE, the horizontal align is not a problem, my question was about vertical align. Concerning the \extrarowheight command, it does not seems to work. The text is indeed shifted a little to the bottom but not centered. I would like something to automatically align the text vertically, regardless of the fontsize and arraystretch. –  Thibault Dory May 18 '12 at 13:41
"Vertical alignment" would mean that you had two things side by side and wanted to raise or lower them to align in some way. Your MWE only has one word on each line there is nothing to align vertically? –  David Carlisle May 18 '12 at 13:47
I want to center vertically the text inside the tabularx. Currently, even if I replace \begin{Huge}Title\end{Huge} with \Huge Title, the text is still touching the top of the tabularx in most of the 6 cases. –  Thibault Dory May 18 '12 at 13:51
You need to make extrarowheight big enough to push things down, or if you need different thing sin different cells add a strut of the right height in each cell. At the point TeX typesets each box it doesn't know the final row height so and at the point when it does know the final row height and centers the box containing your text with that it doesn't know where the text is in the box, which already has text and space so it centers something but that doesn't correspond to making the words have as much space above as below. or perhaps \vspace*{6pt} \Huge Title –  David Carlisle May 18 '12 at 14:04
Well in fact this latex code is generated, therefore I would like a way to automatically center vertically the text. I've already played a little with \vspace but the problem with this approach is that each combination of font size and arraystretch factor needs a different vspace. –  Thibault Dory May 18 '12 at 14:11

Each table begins in a new paragraph, each one applies the default indentation, pushing the table to the right. But you claim \linewidth for the table, and this invades the right margin.

You must cancel the indentation, either adding \noindent before each table declaration, or changing the paragraph indent by putting

\setlength\parindent{0pt}


somewhere at the beginning of the document.

-
This code is only a MWE, the horizontal align is not a problem, my question was about vertical align. –  Thibault Dory May 18 '12 at 13:41