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

Vertically centering text in a tabularx (again!)

Postby Cham » Sat May 12, 2018 5:33 pm

Stefan,

here's the full table (without the vertical centering I'm looking for) :
table.jpg
table.jpg (39.45 KiB) Viewed 390 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 388 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 385 times


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

User avatar
Stefan Kottwitz
Site Admin
Posts: 9105
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sat May 12, 2018 8:09 pm

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:

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


And use only m columns.

Stefan
Site admin

User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

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

Do you mean this ?
  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. \newcommand{\mystrut}{\rule[-3ex]{0ex}{7ex}}
  12. \begin{tabularx}{\textwidth}{|!{\mystrut}>{\centering}m{1.5cm}|X|>{\centering\arraybackslash}m{4.5cm}|}
  13. \hline
  14. \multicolumn{1}{|c|}{\normalsize\textsf{Chapter}}
  15. & \multicolumn{1}{c|}{\normalsize\textsf{Content}}
  16. & \multicolumn{1}{c|}{\normalsize\textsf{Time}}
  17. \\ \hhline{===}
  18. 1
  19. & \textbf{Mathematical notions}
  20. & 3 weeks or so
  21. \\ \hline
  22. 2
  23. & \textbf{Something to waste your time}
  24. & 8 weeks
  25. \\ \hline
  26. \end{tabularx}
  27. \end{center}
  28.  
  29. \end{document}


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

User avatar
Stefan Kottwitz
Site Admin
Posts: 9105
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sat May 12, 2018 8:31 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
Site admin

User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Sat May 12, 2018 8:38 pm

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

Postby Stefan Kottwitz » Sat May 12, 2018 8:54 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
Site admin

User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Sat May 12, 2018 9:01 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

User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Sat May 12, 2018 9: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 :
  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. \usepackage[dvipsnames,table]{xcolor}
  7.  
  8. \begin{document}
  9.  
  10. \begin{center}
  11. \small
  12. \newcommand{\mystrut}{\rule[-6pt]{0pt}{18pt}}
  13. \begin{tabularx}{\textwidth}{|!{\mystrut}>{\centering}m{1.5cm}|X|>{\centering\arraybackslash}m{4.5cm}|}
  14. \hline
  15. \rowcolor[gray]{0.95}
  16. \multicolumn{1}{|!{\mystrut}c|}{\normalsize\textsf{Chapter}}
  17. & \multicolumn{1}{c|}{\normalsize\textsf{Content}}
  18. & \multicolumn{1}{c|}{\normalsize\textsf{Time}}
  19. \\ \hhline{===}
  20. 1
  21. & \textbf{Mathematical notions}
  22. & 3 weeks or so
  23. \\ \hline
  24. 2
  25. & \textbf{Something to waste your time}
  26. & 8 weeks
  27. \\ \hline
  28. \end{tabularx}
  29. \end{center}
  30.  
  31. \end{document}


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

User avatar
Stefan Kottwitz
Site Admin
Posts: 9105
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sat May 12, 2018 9:15 pm

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

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

Stefan
Site admin

User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Sat May 12, 2018 9:19 pm

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


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 9 guests