# Rotating text at the side of a table?

In the following code I want to rotate the text 'Probability' 90 degrees and have it in a column to itself at the left hand side of the table. I would also like vertical bars either side of the top row 'Probability'. As you can see, I haven't been able to achieve this, here is my code -

\begin{document}
\begin{figure}[h]
\centering
\scalebox{0.7}{
$\begin{tabular}{|c | c | c | c | c | c | c | c | c | c | c | c |} \hline \multicolumn{12}{c}{Position} \\ \hline & -5 & -4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4 & 5 \\ \hline & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ \hline & 0 & 0 & 0 & 0 & 0.5 & 0 & 0.5 & 0 & 0 & 0 & 0 \\ \hline & 0 & 0 & 0 & 0.25 & 0 & 0.5 & 0 & 0.25 & 0 & 0 & 0 \\ \hline & 0 & 0 & 0.125 & 0 & 0.375 & 0 & 0.375 & 0 & 0.125 & 0 & 0 \\ \hline & 0 & 0.0625 & 0 & 0.25 & 0 & 0.375 & 0 & 0.25 & 0 & 0.0625 & 0 \\ \hline & 0.03125 & 0 & 0.15625 & 0 & 0.3125 & 0 & 0.3125 & 0 & 0.15625 & 0 & 0.03125 \\ \hline \rot{\rlap{~Probability}} \\ \end{tabular}$
}
\caption{Probabilities for the first $5$ steps of a classical random walk.}
\label{cw_first_5_steps}
\end{figure}
\end{document}


Edit: This doesn't work either!

\begin{figure}[h]
\centering
\scalebox{0.8}{
$\begin{tabular}{|c | c | c | c | c | c | c | c | c | c | c | c |} \hline \multicolumn{12}{c}{Position} \\ \hline & -5 & -4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4 & 5 \\ \hline \multirow{6}{*}{\rotatebox[origin=c]{90}{Probability}}& & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ \hline & 0 & 0 & 0 & 0 & 0.5 & 0 & 0.5 & 0 & 0 & 0 & 0 \\ \hline & 0 & 0 & 0 & 0.25 & 0 & 0.5 & 0 & 0.25 & 0 & 0 & 0 \\ \hline & 0 & 0 & 0.125 & 0 & 0.625 & 0 & 0.125 & 0 & 0.125 & 0 & 0 \\ \hline & 0 & 0.0625 & 0 & 0.625 & 0 & 0.125 & 0 & 0.125 & 0 & 0.0625 & 0 \\ \hline & 0.03125 & 0 & 0.53125 & 0 & 0.125 & 0 & 0.125 & 0 & 0.15625 & 0 & 0.03125 \\ \hline \end{tabular}$
}
\caption{Probabilities for the first $5$ steps of a classical random walk.}
\label{cw_first_5_steps}
\end{figure}

• possibly helpful starting point: Producing a column with rotated text in all cells, How to rotate text in multirow table?, – cmhughes Apr 4 '14 at 18:17
• @cmhughes I have tried some examples like that but I can't get them to work in my case...I can't make that one work either unfortunately. – sonicboom Apr 4 '14 at 18:19
• I've edited in what I tried based on your link...at least the side text is rotated, but the table is still badly messed up. – sonicboom Apr 4 '14 at 18:23
• unrelated to the tabular layout but don't use h on its own as [h] latex usually gives a warning and changes it to [ht] but [htp] is better (otherwise the table is highly likely to go to the end of the document) – David Carlisle Apr 4 '14 at 19:46

Is something like this what you seek? I used \cline to avoid the horizontal lines in column 1, I used {|c|} in the multicolumn to get vertical lines on row 1, and I placed the rotated text as an element in column one of one of the rows itself.

\documentclass{article}
\usepackage{graphicx}
\def\rot#1{\rotatebox{90}{#1}}
\begin{document}
\begin{figure}[h]
\centering
\scalebox{0.7}{
$\begin{tabular}{|c | c | c | c | c | c | c | c | c | c | c | c |} \hline \multicolumn{12}{|c|}{Position} \\ \hline & -5 & -4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4 & 5 \\ \cline{2-12} & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ \cline{2-12} & 0 & 0 & 0 & 0 & 0.5 & 0 & 0.5 & 0 & 0 & 0 & 0 \\ \cline{2-12} & 0 & 0 & 0 & 0.25 & 0 & 0.5 & 0 & 0.25 & 0 & 0 & 0 \\ \cline{2-12} & 0 & 0 & 0.125 & 0 & 0.375 & 0 & 0.375 & 0 & 0.125 & 0 & 0 \\ \cline{2-12} \rot{\rlap{~Probability}} & 0 & 0.0625 & 0 & 0.25 & 0 & 0.375 & 0 & 0.25 & 0 & 0.0625 & 0 \\\cline{2-12} & 0.03125 & 0 & 0.15625 & 0 & 0.3125 & 0 & 0.3125 & 0 & 0.15625 & 0 & 0.03125 \\ \hline \end{tabular}$
}
\caption{Probabilities for the first $5$ steps of a classical random walk.}
\label{cw_first_5_steps}
\end{figure}
\end{document}


• Very nice, is there any way to get vertical lines at the sides of the 'Position' row? – sonicboom Apr 4 '14 at 18:30
• @sonicboom Revised. Make the multicolumn format {|c|}. – Steven B. Segletes Apr 4 '14 at 18:31

You can get the result you wish by using the multirow package

Here's the complete code, the important part is:

% arara: pdflatex
\documentclass{article}
\usepackage{graphicx}
\usepackage{multirow}
\begin{document}
\begin{figure}[h]
\centering
\scalebox{0.7}{
$\begin{tabular}{|c | c | c | c | c | c | c | c | c | c | c | c |} \hline \multicolumn{12}{c}{Position} \\ \hline \multirow{7}{*}{\rotatebox[origin=c]{90}{~Probability}} & -5 & -4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4 & 5 \\ \cline{2-12} & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ \cline{2-12} & 0 & 0 & 0 & 0 & 0.5 & 0 & 0.5 & 0 & 0 & 0 & 0 \\ \cline{2-12} & 0 & 0 & 0 & 0.25 & 0 & 0.5 & 0 & 0.25 & 0 & 0 & 0 \\ \cline{2-12} & 0 & 0 & 0.125 & 0 & 0.375 & 0 & 0.375 & 0 & 0.125 & 0 & 0 \\ \cline{2-12} & 0 & 0.0625 & 0 & 0.25 & 0 & 0.375 & 0 & 0.25 & 0 & 0.0625 & 0 \\ \cline{2-12} & 0.03125 & 0 & 0.15625 & 0 & 0.3125 & 0 & 0.3125 & 0 & 0.15625 & 0 & 0.03125 \\ \hline \end{tabular}$
}
\caption{Probabilities for the first $5$ steps of a classical random walk.}
\label{cw_first_5_steps}
\end{figure}
\end{document}


Note that you a lot of folks here would recommend the booktabs and siunitx package for this type of table:

% arara: pdflatex
\documentclass{article}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{figure}[h]
\centering
\begin{tabular}{c |S[table-format=1.6] S[table-format=1.4] S[table-format=1.5] S[table-format=1.2]S[table-format=1.4]S[table-format=1.3]S[table-format=1.4]S[table-format=1.2]S[table-format=1.5]S[table-format=1.4]S[table-format=1.5]}
\toprule
\multicolumn{12}{c}{Position}  \\
\midrule
\multirow{7}{*}{\rotatebox[origin=c]{90}{~Probability}} & -5 & -4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4 & 5  \\
& 0          & 0         & 0         & 0         & 0         & 1     & 0         & 0         & 0         & 0         & 0         \\
& 0          & 0         & 0         & 0         & 0.5       & 0     & 0.5       & 0         & 0         & 0         & 0         \\
& 0          & 0         & 0         & 0.25      & 0         & 0.5   & 0         & 0.25      & 0         & 0         & 0         \\
& 0          & 0         & 0.125     & 0         & 0.375     & 0     & 0.375     & 0         & 0.125     & 0         & 0         \\
& 0          & 0.0625    & 0         & 0.25      & 0         & 0.375 & 0         & 0.25      & 0         & 0.0625    & 0         \\
& 0.03125    & 0         & 0.15625   & 0         & 0.3125    & 0     & 0.3125    & 0         & 0.15625   & 0         & 0.03125   \\
\bottomrule
\end{tabular}
\caption{Probabilities for the first $5$ steps of a classical random walk.}
\label{cw_first_5_steps}
\end{figure}
\end{document}