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

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

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