## LaTeX forum ⇒ Graphics, Figures & Tables ⇒ Vertically centering text in a tabularx (again!) Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
Cham
Posts: 858
Joined: Sat Apr 02, 2011 4:06 pm

### Vertically centering text in a tabularx (again!)

Stefan,

here's the full table (without the vertical centering I'm looking for) :
table.jpg (39.45 KiB) Viewed 102 times

In my opinion and taste, this looks clear and well organized. There's no confusion. Removing the lines would add some possible confusion. The problem aren't the vertical lines. The horizontal lines clearly show that the text isn't vertically centered in its cell. This is what I would like to fix.

Stefan Kottwitz wrote:Putting everything in cells with borders is rarely the case in good books but in printed excel tables when the full grid is printed, in cheap productions. How are tables in professional books in your field of work? Perhaps compare?

Are you a physics teacher? Did you ever made course plans? We're not talking about books or journal publications. The table I'm showing here is for a course plan, to be given to young students. Things need to be shown clearly to them, with visual supports since they're not experienced readers and they tend to confuse things. Also, conventions aren't the same in Europe (I guess you're in Germany) and in North America (I'm in Canada).

Here's the same tables without the horizontal lines :
table2.jpg (34.16 KiB) Viewed 100 times

While this output is nice to look at, it's a bit more confused than the first one. Removing the vertical lines would be worst.

And just for comparison, here's the original table made in my field, from all other workers. They don't use LaTeX. This was made with the "standard" and default MS Word (that I hate to death, you guess why!) :
table3.jpg (48.52 KiB) Viewed 97 times

Where I'm working (a huge education college), I'm the only one that uses LaTeX!

Stefan Kottwitz
Posts: 8792
Joined: Mon Mar 10, 2008 9:44 pm
Don't make \arraystretch larger, it destroys vertical centering. Try \renewcommand{\arraystretch}{5} to see what I mean.

\newcommand{\mystrut}{\rule[-3ex]{0ex}{8ex}}...\begin{tabularx}{\textwidth}{|!{\mystrut}>{\centering}m{1.5cm}......

And use only m columns.

Stefan

Cham
Posts: 858
Joined: Sat Apr 02, 2011 4:06 pm
Do you mean this ?
\documentclass[11pt,letterpaper,twoside]{article}\usepackage{microtype}\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}\usepackage{hhline}\usepackage{tabularx} \begin{document} \begin{center}	\small	\newcommand{\mystrut}{\rule[-3ex]{0ex}{7ex}}	\begin{tabularx}{\textwidth}{|!{\mystrut}>{\centering}m{1.5cm}|X|>{\centering\arraybackslash}m{4.5cm}|}	\hline	\multicolumn{1}{|c|}{\normalsize\textsf{Chapter}}	& \multicolumn{1}{c|}{\normalsize\textsf{Content}}	& \multicolumn{1}{c|}{\normalsize\textsf{Time}}	\\ \hhline{===}	1	& \textbf{Mathematical notions}	& 3 weeks or so	\\ \hline	2	& \textbf{Something to waste your time}	& 8 weeks	\\ \hline	\end{tabularx}\end{center} \end{document}

But then, the first line is too tight. The output isn't nice.

Stefan Kottwitz
Posts: 8792
Joined: Mon Mar 10, 2008 9:44 pm
I wrote 8ex instead of 7ex (2ex for line height, 3ex below and above, makes 8ex).

In the first row, the column format was changed. So add \mystrut there too.

\multicolumn{1}{|!{\mystrut}c|}{..}

It's enough in one column only.

Stefan

Cham
Posts: 858
Joined: Sat Apr 02, 2011 4:06 pm

Stefan Kottwitz
Posts: 8792
Joined: Mon Mar 10, 2008 9:44 pm
Here, with a base line skip of 12pt, possibly

\newcommand{\mystrut}{\rule[-5pt]{0pt}{18pt}}

The 2 looks centered, the S looks centered, ... the look is what counts anyway.

You could raise step by step in adding 1pt for the minus part, and 2pt for the whole, such as

\newcommand{\mystrut}{\rule[-6pt]{0pt}{20pt}}

Stefan

Cham
Posts: 858
Joined: Sat Apr 02, 2011 4:06 pm
ex is the height of the x in the current font. So it's a relative value, that changes with the font size automatically, so I usually prefer it when it comes to height.

pt is absolute. I just printed it by \the\baselineskip, it was 12pt (with descenders), so I used this (absolute) value as base.

With 11pt font size (document class option), \small font could be 9 or 10 (I did not check).

Stefan

Cham
Posts: 858
Joined: Sat Apr 02, 2011 4:06 pm
And another thing : adding a color to the first line gives a small white gap at the beginning. Why ? Try this code and look carefully at the left part :
\documentclass[11pt,letterpaper,twoside]{article}\usepackage{microtype}\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}\usepackage{hhline}\usepackage{tabularx}\usepackage[dvipsnames,table]{xcolor} \begin{document} \begin{center}	\small	\newcommand{\mystrut}{\rule[-6pt]{0pt}{18pt}}	\begin{tabularx}{\textwidth}{|!{\mystrut}>{\centering}m{1.5cm}|X|>{\centering\arraybackslash}m{4.5cm}|}	\hline	\rowcolor[gray]{0.95}	\multicolumn{1}{|!{\mystrut}c|}{\normalsize\textsf{Chapter}}	& \multicolumn{1}{c|}{\normalsize\textsf{Content}}	& \multicolumn{1}{c|}{\normalsize\textsf{Time}}	\\ \hhline{===}	1	& \textbf{Mathematical notions}	& 3 weeks or so	\\ \hline	2	& \textbf{Something to waste your time}	& 8 weeks	\\ \hline	\end{tabularx}\end{center} \end{document}

Preview:
gray.jpg (25.1 KiB) Viewed 79 times

Stefan Kottwitz
Posts: 8792
Joined: Mon Mar 10, 2008 9:44 pm
It's the space between columns. You can remove it by @{}:

\multicolumn{1}{|@{}!{\mystrut}c|}{\normalsize\textsf{Chapter}}

Stefan