Graphics, Figures & TablesHorizontal and vertical alignment in tables with long text

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Fibonacci
Posts: 37
Joined: Tue Jan 25, 2011 1:16 am

Horizontal and vertical alignment in tables with long text

Post by Fibonacci »

Hello.

I'm trying to make a table in which the text of the first column is aligned to the top right, and the text of the second (which can span multiple lines) is centered. This is what I've tried so far:

Code: Select all

\begin{tabular}{|r|p{3cm}|} \hline
Top right & {Some text\newline Some text} \\ \hline
Much longer line & {Some text\newline Some text} \\ \hline
\end{tabular}
That fails because "Some text" is not centered.

Code: Select all

\usepackage{multirow}
...
\begin{tabular}{|r|c|} \hline
\multirow{2}{*}{Top right} & Some text\\ 
                           & Some text\\ \hline
\multirow{2}{*}{Much longer line} & Some text\\
                                  & Some text\\ \hline
\end{tabular}
That fails because the text on the first column is vertically centered – I need it on the top part of the cells.

Code: Select all

\usepackage{multirow}
...
\begin{tabular}{|p{3cm}|c|} \hline
\multirow{2}{*}{Top right} & Some text\\ 
                           & Some text\\ \hline
\multirow{2}{*}{Much longer line} & Some text\\
                                  & Some text\\ \hline
\end{tabular}
That fails even worse, because the text on the first column is left-aligned AND vertically centered.

How can I achieve such a result?

Thanks in advance.

Recommended reading 2024:

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

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

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Horizontal and vertical alignment in tables with long text

Post by frabjous »

I think you probably want something like this:

Code: Select all

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{|r|>{\centering\arraybackslash}p{3cm}|} \hline
Top right &Some text\linebreak Some text\\ \hline
Much longer line &Some text\linebreak Some text\\ \hline
\end{tabular}
\end{document}
Fibonacci
Posts: 37
Joined: Tue Jan 25, 2011 1:16 am

Horizontal and vertical alignment in tables with long text

Post by Fibonacci »

Unfortunately, that stops working when I merge some of the cells:

Code: Select all

\begin{tabular}{|r|>{\centering\arraybackslash}p{3cm}|} \hline
Top right & \multirow{2}{*}{Some text\linebreak Some text}\\ \cline{1-1}
Much longer line & \\ \hline
\end{tabular}
Now the text on the second column has no linebreaks, does not wrap, and is not centered.

How can I solve that?
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Horizontal and vertical alignment in tables with long text

Post by frabjous »

I didn't know about that before, but it looks like if you use a multirow in a p column you need to manually re-insert the parbox on the inside:

Code: Select all

\documentclass{article}
\usepackage{array}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|r|>{\centering\arraybackslash}p{3cm}|} \hline
Top right & \multirow{2}{*}{\parbox{3cm}{\centering Some text\linebreak Some text}}\\ \cline{1-1}
Much longer line & \\ \hline
\end{tabular}
\end{document}
Post Reply