GeneralVertical alignment of vertical text in tabular & array

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
olly78
Posts: 14
Joined: Wed Sep 24, 2008 10:57 am

Vertical alignment of vertical text in tabular & array

Post by olly78 »

Hi all

I'd like to have vertical text down the side of a figure vertically centred. Here is a MWE of the problem:

\documentclass[a4paper,11pt]{report}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\begin{tabular}{cc}
\rotatebox{90}{This should be centred vertically} &
\framebox[0.8\linewidth]{\rule{1pt}{\linewidth}}
\end{tabular}
\end{figure}
\end{document}

I've tried the obvious use of [c] in the tabular environment and enclosing the text in a parbox with [c] also, but these don't work as desired. Currently I'm using \hspace, and setting the amount manually by trial and error, which isn't ideal.

Any ideas of how to do this properly would be much appreciated.

Thanks,
Olly

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Vertical alignment of vertical text in tabular & array

Post by phi »

Hello,
here is a solution. It is probably suboptimal, but at least working:

Code: Select all

\documentclass[a4paper,11pt]{report}
\usepackage{graphicx}
\usepackage{array}
\newsavebox\DescBox
\begin{document}
\begin{figure}
\sbox\DescBox{This should be centred vertically}
\begin{tabular}{m{\dimexpr\ht\DescBox+\dp\DescBox}m{0.8\linewidth}}
\rotatebox[origin=c]{90}{\usebox\DescBox} &
\framebox[0.8\linewidth]{\rule{1pt}{\linewidth}}
\end{tabular}
\end{figure}
\end{document}
olly78
Posts: 14
Joined: Wed Sep 24, 2008 10:57 am

Vertical alignment of vertical text in tabular & array

Post by olly78 »

Many thanks for your suggestion, Phi.

In fact, I can make your code much simpler and have it still work:

Code: Select all

\documentclass[a4paper,11pt]{report}
\usepackage{graphicx}
\def\uptext#1{\rotatebox[origin=c]{90}{#1}}
\begin{document}
\begin{figure}
\begin{tabular}{m{11pt}m{0.8\linewidth}}
\uptext{This should be centred vertically} &
\framebox[0.8\linewidth]{\rule{1pt}{0.7\linewidth}}\\
\uptext{So should this} &
\framebox[0.8\linewidth]{\rule{1pt}{0.7\linewidth}}
\end{tabular}
\end{figure}
\end{document}
However, it breaks as soon as I define any of the columns using something other than 'm'. This is a pain if I want the width of the other columns to be variable (which I do), for example using 'c'.

Can anyone see a way round this?

Many thanks,
Olly
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Vertical alignment of vertical text in tabular & array

Post by phi »

It's much simpler than I thought:

Code: Select all

\documentclass[a4paper,11pt]{report}
\usepackage{graphicx}
\newcommand*\vertcenter[1]{\raisebox{-0.5\height}{#1}}
\newcommand*\uptext[1]{\vertcenter{\rotatebox{90}{#1}}}
\setlength\fboxsep{0.0pt}
\begin{document}
\begin{figure}
\begin{tabular}{cc}
\hline
\uptext{This should be centred vertically} &
\vertcenter{\framebox[0.8\linewidth]{\rule{1pt}{0.42\linewidth}}} \\
\hline
\uptext{So should this} &
\vertcenter{\framebox[0.8\linewidth]{\rule{1pt}{0.12\linewidth}}} \\
\hline
\end{tabular}
\end{figure}
\end{document}
I set \fboxsep to zero, otherwise there is a small offset of length \fboxsep, which can be easily corrected in the \raisebox command, if necessary.

EDIT: This version of \vertcenter also works if the box has a nonzero depth (like \framebox):

Code: Select all

\newcommand*\vertcenter[1]{\raisebox{\dimexpr(\depth-\height)/2}{#1}}
olly78
Posts: 14
Joined: Wed Sep 24, 2008 10:57 am

Re: Vertical alignment of vertical text in tabular & array

Post by olly78 »

Great. Thanks, Phi.
Post Reply