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

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

Vertically centering text in a tabularx (again!)

Postby Cham » Sat May 12, 2018 3:21 am

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

  1. \documentclass[11pt,letterpaper,twoside]{article}
  2. \usepackage{microtype}
  3. \usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
  4. \usepackage{hhline}
  5. \usepackage{tabularx}
  6.  
  7. \begin{document}
  8.  
  9. \begin{center}
  10. \small
  11. \renewcommand{\arraystretch}{1.75}
  12. \renewcommand{\tabularxcolumn}[1]{>{\raggedright\arraybackslash}m{#1}}
  13. \begin{tabularx}{\textwidth}{|>{\centering}p{1.5cm}|>{\raggedright\arraybackslash}X|>{\centering\arraybackslash}p{4.5cm}|}
  14. \hline
  15. \multicolumn{1}{|c|}{\normalsize\textsf{Chapter}}
  16. & \multicolumn{1}{c|}{\normalsize\textsf{Content}}
  17. & \multicolumn{1}{c|}{\normalsize\textsf{Time}}
  18. \\ \hhline{===}
  19. 1
  20. & \textbf{Mathematical notions}
  21. & 3 weeks or so
  22. \\ \hline
  23. 2
  24. & \textbf{Something to waste your time}
  25. & 8 weeks
  26. \\ \hline
  27. \end{tabularx}
  28. \end{center}
  29.  
  30. \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 118 times

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

Postby Johannes_B » Sat May 12, 2018 7:07 am

This is what you are asking for. A m columns aligns the middle of its contents with the baseline of the other columns.
  1. \documentclass[11pt,letterpaper,twoside]{article}
  2. \usepackage{microtype}
  3. \usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
  4. \usepackage{hhline}
  5. \usepackage{tabularx}
  6.  
  7. \begin{document}
  8.  
  9. \begin{center}
  10. \small
  11. \renewcommand{\arraystretch}{1.75}
  12. \renewcommand{\tabularxcolumn}[1]{m{#1}}
  13. \begin{tabularx}{.4\textwidth}{|>{\raggedleft}p{1.5cm}@{}|@{}X}
  14. \hline
  15. 1 & \textbf{Mathematical notions}\\ \hline
  16. 2 &{Something to waste your time and some more text just to get to the next line}\\
  17. \end{tabularx}
  18. \end{center}
  19.  
  20. \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: 853
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Sat May 12, 2018 3:28 pm

Johannes,

your code above doesn't work. Even in overleaf, the table is all messed.

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

Postby Johannes_B » Sat May 12, 2018 3:54 pm

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: 853
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Sat May 12, 2018 4:04 pm

No, it's all messed up! Here's what it does, and it's horrible :
garbled.jpg
garbled.jpg (11.91 KiB) Viewed 95 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: 3725
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Sat May 12, 2018 4:14 pm

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: 3725
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Sat May 12, 2018 4:23 pm

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
  1. \documentclass[11pt,letterpaper,twoside]{article}
  2. \usepackage{microtype}
  3. \usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
  4. \usepackage{hhline}
  5. \usepackage{tabularx}
  6.  
  7. \begin{document}
  8.  
  9. \begin{center}
  10. \small
  11. \renewcommand{\arraystretch}{3.75}
  12. \renewcommand{\tabularxcolumn}[1]{m{#1}}
  13. \begin{tabularx}{.4\textwidth}{|>{\raggedleft}p{1.5cm}@{}|@{}X}
  14. \hline
  15. 1 & \textbf{athematical notions}\\ \hline
  16. 2 &{Something to waste your time and some more text just to get to the next line}\\
  17. \end{tabularx}
  18. \end{center}
  19.  
  20. \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: 853
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Sat May 12, 2018 4:25 pm

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: 3725
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Sat May 12, 2018 4:51 pm

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: 8787
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sat May 12, 2018 5:17 pm

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
Site admin


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 2 guests