Graphics, Figures & TablesVertically centering text in a tabularx (again!)

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

Vertically centering text in a tabularx (again!)

Post by Cham »

Stefan,

here's the full table (without the vertical centering I'm looking for) :
table.jpg
table.jpg (39.45 KiB) Viewed 6955 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
table2.jpg (34.16 KiB) Viewed 6953 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
table3.jpg (48.52 KiB) Viewed 6950 times
Where I'm working (a huge education college), I'm the only one that uses LaTeX! ;)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
TikZ book
User avatar
Stefan Kottwitz
Site Admin
Posts: 10311
Joined: Mon Mar 10, 2008 9:44 pm

Vertically centering text in a tabularx (again!)

Post by Stefan Kottwitz »

Don't make \arraystretch larger, it destroys vertical centering. Try \renewcommand{\arraystretch}{5} to see what I mean.

Instead, you can add an invisible line:

Code: Select all

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

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertically centering text in a tabularx (again!)

Post by Cham »

Do you mean this ?

Code: Select all

\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.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10311
Joined: Mon Mar 10, 2008 9:44 pm

Vertically centering text in a tabularx (again!)

Post by Stefan Kottwitz »

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
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertically centering text in a tabularx (again!)

Post by Cham »

It's working, but using \newcommand{\mystrut}{\rule[-3ex]{0ex}{8ex}} makes the lines pretty thick. What parameters to have a similar effect as \renewcommand{\arraystretch}{1.5} ?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10311
Joined: Mon Mar 10, 2008 9:44 pm

Vertically centering text in a tabularx (again!)

Post by Stefan Kottwitz »

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
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertically centering text in a tabularx (again!)

Post by Cham »

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
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertically centering text in a tabularx (again!)

Post by Cham »

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 :

Code: Select all

\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
gray.jpg (25.1 KiB) Viewed 6932 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10311
Joined: Mon Mar 10, 2008 9:44 pm

Vertically centering text in a tabularx (again!)

Post by Stefan Kottwitz »

It's the space between columns. You can remove it by @{}:

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

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertically centering text in a tabularx (again!)

Post by Cham »

It's working, thanks. That's the kind of LaTeX stuff that I would never understand, all these !@{}... are so kryptic to me! :oops:
Post Reply