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 »

I'm having troubles (again!?) with vertically centering items in a table, as shown in this MWE below:

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
	\renewcommand{\arraystretch}{1.75}
	\renewcommand{\tabularxcolumn}[1]{>{\raggedright\arraybackslash}m{#1}}
	\begin{tabularx}{\textwidth}{|>{\centering}p{1.5cm}|>{\raggedright\arraybackslash}X|>{\centering\arraybackslash}p{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}
While this code is using "p" for the first and third columns, and "m" for the second column, the text isn't vertically well centered in the second column. Why? What is happening here? What's wrong with my code? :|

Here's a preview of what this super awesome code ( :roll: ) is doing:
vertical.jpg
vertical.jpg (17.65 KiB) Viewed 19427 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Vertically centering text in a tabularx (again!)

Post by Johannes_B »

This is what you are asking for. A m columns aligns the middle of its contents with the baseline of the other columns.

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
\renewcommand{\arraystretch}{1.75}
	\renewcommand{\tabularxcolumn}[1]{m{#1}}
	\begin{tabularx}{.4\textwidth}{|>{\raggedleft}p{1.5cm}@{}|@{}X}
\hline
1 & \textbf{Mathematical notions}\\ \hline
2 &{Something to waste your time and some more text just to get to the next line}\\
\end{tabularx}
\end{center}

\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertically centering text in a tabularx (again!)

Post by Cham »

Johannes,

your code above doesn't work. Even in overleaf, the table is all messed.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Vertically centering text in a tabularx (again!)

Post by Johannes_B »

No, it isn't messed at all. It is supposed to look like that. It demonstrates more clearly, that LaTeX does the correct centering of the content.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertically centering text in a tabularx (again!)

Post by Cham »

No, it's all messed up! Here's what it does, and it's horrible :
garbled.jpg
garbled.jpg (11.91 KiB) Viewed 19404 times
I get the same in Overleaf.

Please, could you stick with the original code, and just show the appropriate modifications?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Vertically centering text in a tabularx (again!)

Post by Johannes_B »

The image shows, the text is vertically centered.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Vertically centering text in a tabularx (again!)

Post by Johannes_B »

Johannes_B wrote:This is what you are asking for. A m columns aligns the middle of its contents with the baseline of the other columns.
That actually was wrong as i just now noticed. It is your arraystretch that messes stuff up

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
\renewcommand{\arraystretch}{3.75}
	\renewcommand{\tabularxcolumn}[1]{m{#1}}
	\begin{tabularx}{.4\textwidth}{|>{\raggedleft}p{1.5cm}@{}|@{}X}
\hline
1 & \textbf{athematical notions}\\ \hline
2 &{Something to waste your time and some more text just to get to the next line}\\
\end{tabularx}
\end{center}

\end{document}
Google gives me https://tex.stackexchange.com/questions ... raystretch
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Vertically centering text in a tabularx (again!)

Post by Cham »

It is worst. This table is horrible. Please, use the original code. What to do to center (vertically) everything in their cell, without changing the stretch ?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Vertically centering text in a tabularx (again!)

Post by Johannes_B »

Of course it is horrible, it is to show off the reason.
Have you tried the solutions in the link i gave you?

I don't have a lot of time today and don't want to waste it on your tables. As far as I am concerned, they will just as ugly if that spacing is fixed. I really don't like how they look.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Vertically centering text in a tabularx (again!)

Post by Stefan Kottwitz »

Text should be aligned at base lines. If one just aligns vertically, the base lines are not at the same height, that looks strange.

Today the typographic taste is usually, that no vertical lines are used (reading flow from left to right without blocking lines) and no double lines; just few horizontal lines (top, bottom). 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?

Stefan
LaTeX.org admin
Post Reply