4

I am wanting the ability to vertically align any cell within a table. This appears possible but only on a per column or row basis. At the moment I can horizontally align the contents of individual cells with the help of the \raggedleft and \centering commands. However, \multicolumn applies vertical alignment for the whole row and the longtabu environment can only specify the alignment type per column.

The following is an example of the table I want to create where both horizontal and vertical alignments need to be set for each cell instead of per row or column:

\documentclass[11pt,a4paper]{article}
\usepackage{longtable}      
\usepackage{tabu}
\usepackage{lipsum}
\usepackage{multirow}

\newcolumntype{M}{m{\dimexpr 1\tabucolX+1\tabcolsep+\arrayrulewidth\relax}}
\newcolumntype{P}{p{\dimexpr 1\tabucolX+1\tabcolsep+\arrayrulewidth\relax}}
\newcolumntype{B}{b{\dimexpr 1\tabucolX+1\tabcolsep+\arrayrulewidth\relax}}

\newcolumntype{H}{>{\begin{minipage}[b]{\hsize}}B<{\end{minipage}}}

\begin{document}

\begin{longtabu} to 150mm [l] {|X[l]|X[l]|X[l]|}
\hline 
\multicolumn{1}{|P|}{ \lipsum[1]} & \multicolumn{1}{H|}{\centering H: Center, V:Center} & \multicolumn{1}{B|}{\raggedleft \multirow{1}{*}[-2\baselineskip]{H:Right, V:Bottom}} \tabularnewline
\hline
\multicolumn{1}{|P|}{\lipsum[1]} & \multicolumn{1}{P|}{H: Left, V:Top} & \multicolumn{1}{M|}{\centering H:Center, V:Center} \tabularnewline
\hline
\multicolumn{1}{|P|}{\lipsum[1]} & \multicolumn{1}{M|}{\raggedleft H: Right, V:Center} & \multicolumn{1}{B|}{H:Left, V:Bottom} \tabularnewline
\hline 
\tabuphantomline 
\end{longtabu}

\end{document}

Update:

Attempting the solutions presented in "Move tabular entry to bottom of row" won't work.

The first solution I believe simply adjusts the entire column by creating a new column type:

\newcolumntype{B}{>{\begin{minipage}[b]{\hsize}}X<{\end{minipage}}}

This doesn't help my problem where I need the vertical alignment applied to individual cells, not a whole column. Applying this column type to \multicolumn doesn't appear to work.

The second solution places a \baselineskip into the optional parameter of \multirow which works initially but seems to become vertically centred as soon as the cell becomes a certain height.

I have updated my example above to show how it breaks.

Thanks

  • Sorry when I say "vertically align" I am not referring to the state of being vertically centred in the middle but the ability to be positioned vertically e.g. Top, Middle or Bottom. – A Dark Divided Gem Feb 16 '14 at 23:47
  • You can use m or b columns from array package. – user11232 Feb 16 '14 at 23:56
  • As I mention in my question I am looking to apply vertical positioning at the cell level instead of per column. For \multicolumn only one column-spec is allowed. – A Dark Divided Gem Feb 17 '14 at 1:12
  • My recommendation: stay away from longtabu. The developer has already said that he is going to make some major changes and it is not going to be backward compatible. You can find more about this in this question: tex.stackexchange.com/questions/106452/… – Mario S. E. Mar 6 '14 at 11:06
  • David Carlisle's answer at tex.stackexchange.com/questions/166808/… can address this problem. – Steven B. Segletes Apr 2 '14 at 2:19
2

David Carlisle's answer doesn't appear to be working for me.

hmph:-)

Works for me:

enter image description here

\documentclass[11pt,a4paper]{article}
\usepackage{longtable}  
\usepackage{array}    

\usepackage{lipsum}

\makeatletter


\newcolumntype{P}{p{.3\textwidth}}


\newcommand\m[1]{\multicolumn{1}{#1}}

\newcommand\zc[2]{%
\setbox0\hbox{\parbox[c]{.3\textwidth}{#2}}%
\smash{\raisebox{\dimexpr(\csname PDFSAVEe#1\endcsname sp-
                         \csname PDFSAVEb#1\endcsname sp)/2\relax}{\box0}}}
\newcommand\zb[2]{%
\setbox0\hbox{\parbox[t]{.3\textwidth}{#2}}%
\smash{\raisebox{\dimexpr\csname PDFSAVEe#1\endcsname sp-
                         \csname PDFSAVEb#1\endcsname sp\relax}{\box0}}}

\def\foo#1{\leavevmode
\expandafter\ifx\csname PDFSAVE#1\endcsname\relax
\expandafter\gdef\csname PDFSAVE#1\endcsname{0}%
\fi
\pdfsavepos\write\@auxout{\gdef\string\PDFSAVE#1{\the\pdflastypos}}}
\makeatother

\begin{document}

\begin{longtable}{|P|P|P|}
\hline 
\foo{ba}\lipsum*[1]\foo{ea} &
\zc{a}{\centering H: Center, V:Center} &
\zb{a}{\raggedleft H:Right, V:Bottom} \tabularnewline
\hline
\foo{bb}\lipsum*[1]\foo{eb} & 
{H: Left, V:Top} &
\zc{b}{\centering H:Center, V:Center} \tabularnewline
\hline
\foo{bc}\lipsum*[1]\foo{ec} &
\zc{c}{\raggedleft H: Right, V:Center} & 
\zb{c}{H:Left, V:Bottom} \tabularnewline
\hline 
\end{longtable}

\end{document}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.