# Vertical aligning text and vertical height of rows in longtable environment

I have now been working on fixing this problem for some time and I have read through some forums on the Internet and tried different things, but I can't solve this problem.

I am looking to have some text vertically aligned at the bottom inside a long table, preferable with a line about the text for a signature. Furthermore I would like to have control over the minimum height of the rows.

I've seen that I can align text vertically with the array package and from Vertical aligning in longtable environment it also seems to be possible inside a long table.

I should probably also say that I am generating my tables with R's knitr package and xtable's print.xtable().

I have some working code here (and also a screenshot of what I currently have below the code).

\documentclass[12pt]{article}
\usepackage{longtable}
\usepackage[landscape]{geometry}
\usepackage{colortbl} %
\begin{document}

\begin{longtable}{cccp{6.6cm}p{3.6cm}p{3.6cm}}
\hline
Date & Event & Type & Note & Bank & Banrak Obama \\
\hline \endhead \rowcolor[gray]{.95}Sep 02 2012 & 005 & CPP & ww &John Doe, \#, '\# & Some Name \\
Sep 10 2012 & 006 & IND & Quisque luctus purus non arcu feugiat pharetra. In hac habitasse platea dictumst. &John Doe, \#, '\# & Some Name \\
\rowcolor[gray]{.95} Sep 22 2012 & 002 & CON & I sat around thinking about my life. & John Doe, \#,  ID'\# & Some Name \\
Sep 23 2012 & 003 & OUT & Quisque luctus purus non arcu feugiat pharetra. In hac habitasse platea dictumst. Donec non sapien tellus, et condimentum lectus. Aenean eu mollis velit. Morbi dignissim commodo sapien, bibendum consectetur libero mattis eu. Aliquam tristique pharetra elit et posuere. Nullam adipiscing, purus at volutpat consequat. &John Doe, \# ID'\# & Some Name \\
\rowcolor[gray]{.95} Sep 23 2012 & 004 & OUT & Outreach, Fish. \& $\backslash$ \# Quisque luctus purus non arcu feugiat pharetra. In hac habitasse platea dictumst. Donec non sapien tellus, et condimentum lectus. Aenean eu mollis velit. Morbi dignissim commodo sapien, bibendum consectetur libero mattis eu. Aliquam tristique pharetra elit et posuere. Nullam adipiscing, purus at volutpat consequat. &John Doe, \# ID'\# & Some Name \\
Sep 23 2012 & 003 & OUT & Quisque luctus purus non arcu feugiat pharetra. In hac habitasse platea dictumst. Donec non sapien tellus, et condimentum lectus. Aenean eu mollis velit. Morbi dignissim commodo sapien, bibendum consectetur libero mattis eu. Aliquam tristique pharetra elit et posuere. Nullam adipiscing, purus at volutpat consequat. &John Doe, \# ID'\# & Some Name \\
\rowcolor[gray]{.95} Sep 23 2012 & 004 & OUT & Outreach, Fish. \& $\backslash$ \# Quisque luctus purus non arcu feugiat pharetra. In hac habitasse platea dictumst. Donec non sapien tellus, et condimentum lectus. Aenean eu mollis velit. Morbi dignissim commodo sapien, bibendum consectetur libero mattis eu. Aliquam tristique pharetra elit et posuere. Nullam adipiscing, purus at volutpat consequat. &John Doe, \# ID'\# & Some Name \\
\hline
\hline
\label{PN.df}
\end{longtable}

\end{document}


-
@percusse, thank you for the edits. –  Eric Fail Dec 5 '12 at 5:19
The columntype b{<width>} vertically aligns at the bottom (p aligns at the top, m at the middle). You may further define minimum row (where in that vertical space should be the text’s baseline?). For a tabular-globe use you can redefine \arraystretch, e.g. \renewcommand{\arraystretch}{1.5}. –  Qrrbrbirlbel Dec 5 '12 at 5:54
@Qrrbrbirlbel, thank you for commenting on my question. I am a bit in over my head here, but what is tabular-globe? Could you refer me to a source or maybe share an working example with me? Also, it is my understanding that longtbale' overwrites any b{<width>}'s or m{<width>}'s, but maybe that is what you are circumventing by your suggestion? Thanks, Eric. –  Eric Fail Dec 5 '12 at 6:39
@EricFail no longtable doesn't do anything with the column specifiers other than pass them to the underlying tabular implementation. b{..} means the same in longtable as it does in tabular. –  David Carlisle Dec 5 '12 at 14:47

I would define a new column as in the following example. The newcolumntype command can be very useful for achieving the sorts of affects you're looking for.

\newcolumntype{B}{>{\begin{minipage}[b]{6.6cm}\raggedright{}}c<{\end{minipage}}}


It's not essential to use \raggedright, but I think fully justified text in rather narrow columns looks unappealing and is more difficult to read. Anyhow, if you don't like the \raggedright remove it from my code. (It would be interesting to see what others have to say on this point.)

If you want something that behaves a bit more like p{...}, you can also define a new column type that takes an argument:

\newcolumntype{B}[1]{>{\begin{minipage}[b]{#1}\raggedright{}}c<{\end{minipage}}}


You can set a minimum row height by inserting an 0pt width strut at the beginning of each line of your table. You could define it as

 \newcommand{\minrowheight}{\rule{0pt}{4ex}\relax}


If you don't want to enter this manually into each row (that would be a pain) you can do it when you declare the columns:

\begin{longtable}{>{\minrowheight}cccB{6.6cm}p{3.6cm}p{3.6cm}}


You meniton some conflicts with other packages caused by the > to the first comlumn while there doesn't seem to be a problem with the new column type B. If that's the case, then you can place the code for the minimum row height at the very end of the definition for newcolumntype B.

\newcolumntype{B}[1]{>{\begin{minipage}[b]{#1}\raggedright{}}c<{\end{minipage}\minrowheight}}


You don't want to put it in the minipage environment. That will not create the visual effect you want at all. (Try to see what happens.) It should either be placed before or after the minipage. Hopefully this will be a fix for how your row coloring works.

Here's MWE that includes these suggestions and includes a signature line.

\documentclass[12pt]{article}
\usepackage{longtable}
\usepackage[landscape]{geometry}
\usepackage{colortbl} %
\newcolumntype{B}[1]{>{\begin{minipage}[b]{#1}\raggedright{}}c<{\end{minipage}\minrowheight}}
\newcommand{\minrowheight}{\rule{0pt}{8ex}\relax}
\newcommand{\signatureLine}[1]{\begin{minipage}{3.6cm}\rule{3.6cm}{0.4pt}\newline#1\end{minipage}}
\pagestyle{empty}
\begin{document}
\begin{longtable}{cccB{6.6cm}p{3.6cm}p{3.6cm}}
\hline
Date & Event & Type & Note & Bank & Banrak Obama \\
\hline \endhead \rowcolor[gray]{.95}Sep 02 2012 & 005 & CPP & ww &\signatureLine{John Doe, \#, '\#} & \signatureLine{Some Name} \\
Sep 10 2012 & 006 & IND & Quisque luctus purus non arcu feugiat pharetra. In hac habitasse platea dictumst. &\signatureLine{John Doe, \#, '\#} & \signatureLine{Some Name} \\
\rowcolor[gray]{.95} Sep 22 2012 & 002 & CON & I sat around thinking about my life. & \signatureLine{John Doe, \#,  ID'\#} & \signatureLine{Some Name} \\
Sep 23 2012 & 003 & OUT & Quisque luctus purus non arcu feugiat pharetra. In hac habitasse platea dictumst. Donec non sapien tellus, et condimentum lectus. Aenean eu mollis velit. Morbi dignissim commodo sapien, bibendum consectetur libero mattis eu. Aliquam tristique pharetra elit et posuere. Nullam adipiscing, purus at volutpat consequat. &\signatureLine{John Doe, \# ID'\#} & \signatureLine{Some Name} \\
\rowcolor[gray]{.95} Sep 23 2012 & 004 & OUT & Outreach, Fish. \& $\backslash$ \# Quisque luctus purus non arcu feugiat pharetra. In hac habitasse platea dictumst. Donec non sapien tellus, et condimentum lectus. Aenean eu mollis velit. Morbi dignissim commodo sapien, bibendum consectetur libero mattis eu. Aliquam tristique pharetra elit et posuere. Nullam adipiscing, purus at volutpat consequat. &\signatureLine{John Doe, \# ID'\#} & \signatureLine{Some Name} \\
\hline
\label{PN.df}
\end{longtable}
\end{document}


which gives an appearance like:

My choice of minimum row height was completely arbitrary. But working in units of ex is standard for vertical heights (it's essentially the height of the letter x.

Now what would be nice is to be able to disable the minimum row height for the column headers. This can be done, by setting some flags. That can get a bit tricky and technical. But then this is also going beyond what you were looking for.

You mention something about a line about the text for a signature. I'm not really clear on what you're looking for on that point.

-
Dear A.Ellett, this is crazy impressive. I've been at it for more then 12 hours now, and I have a really hard time grasping how to implement this in my world, but I really appreciate your answer and I will spend some more time looking at it in the morning! Thanks! –  Eric Fail Dec 5 '12 at 5:47
@Eric Fail: And on that note, welcome to TeX.SE. –  Peter Grill Dec 5 '12 at 6:09
Dear A.Ellett, the line about the text for a signature part is me looking to create a small line above the name, like in this and this TeX.SE example, for John Doe and some Name to sign on. Right now I'm fighting to get R's knitr package to pass the > in the {\minrowheight}cccB{6.6cm}p{3.6cm}p{3.6cm}} into my .tex file from my .Rnw file. –  Eric Fail Dec 5 '12 at 6:28
Does knitr accept the > in your preamble when defining the new column type B? If yes, then you can define a new column type for the first column to get around this. \newcolumntype{C}{>{\minrowheight}c}. –  A.Ellett Dec 5 '12 at 14:17
@EricFail, I've slightly modified my solution to address a different approach to getting the minrowheight to take effect. I've put the modification toward the end of my answer. –  A.Ellett Dec 5 '12 at 17:42

Explanation to my comment:

The columntype b{<width>} vertically aligns at the bottom (p aligns at the top, m at the middle). You may further define minimum row (where in that vertical space should be the text’s baseline?). For a tabular-globe use you can redefine \arraystretch, e.g. \renewcommand{\arraystretch}{1.5}.

The column type b{<width>} is probably what you’re looking for. It works quite like p and m, so as David Carlisle (and he ought to know it) said they are just forwarded to the underlying tabular.

The column type l, c and r contain only one line, so they are in fact the reference for the vertical alignment of p, m and b.

In your case, I would even replace the last two columns with an l column as they are always shorter then the given width, but YMMV.

So without changing the content of your table but with the column specification {cccb{6.6cm}ll} you’ll get .

In that Lorem Ipsum example you get warnings about underfull hboxes which is no surprise as p, m and b are justified. With the array package (which is loaded by colortbl) you can automatically make the content of b ragged right {ccc>{\raggedright}b{6.6cm}ll} and you get

For changing row height, you may redefine the macro \arraystretch, its default is 1.0. With for example \renewcommand*{\arraystretch}{1.5}` you get:

If you need more control over the individual height of rows you may use struts as A.Ellett did in his answer.

-