Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question is best explained by giving the usecase:

I have a table where I list some data and I would like to add a column to the right which gives the ratio of two adjacent rows. Now to make it more obvious the ratio of which rows this is I would like to shift the column which the ratios by half the height of a row so that each of the ratio values sits to the right of the two columns which it was calculated from.

E.g. instead of this

 A    ratio
------------
 3      2
 6      3
18

imagine the same table but the numbers in the right column shifted down by half a row.

Is this even possible?

share|improve this question

2 Answers 2

up vote 7 down vote accepted
\documentclass[a4paper]{article}
\usepackage{collcell,array}
\newcommand{\shiftdown}[1]{\smash{\raisebox{-.5\normalbaselineskip}{#1}}}
\newcolumntype{C}{>{\collectcell\shiftdown}c<{\endcollectcell}}
\begin{document}
\begin{tabular}{cC}
A & \multicolumn{1}{c}{ratio}\\
\hline
3 &2\\
6 &3\\
18
\end{tabular}
\end{document}

The \multicolumn{1}{c}{...} is necessary to avoid applying the shift also to the heading.

share|improve this answer
    
(+1) Snap! And thanks for showing how to use \normalbaselineskip -- I was sure there was a command like that but couldn't remember what it was... –  Ant Sep 26 '11 at 10:57
    
Using a dimensione expressed in ex units is quite the same. –  egreg Sep 26 '11 at 11:00
    
Great work, thanks! –  fuenfundachtzig Sep 26 '11 at 12:23
\usepackage{booktabs}
\usepackage{collcell}
\newcommand*{\movedown}[1]{%
  \smash{\raisebox{-1ex}{#1}}}
\newcolumntype{q}{>{\collectcell\movedown}r<{\endcollectcell}}
   % (for "quotient")
\begin{document}
\begin{tabular}{rq}
\toprule
A & \multicolumn{1}{l}{ratio}\\
\midrule
3 & 2\\
6 & 3\\
18 \\
\bottomrule
\end{tabular}

Table with numbers in ratio column shifted downwards

The key part here is that \raisebox moves the number down and \smash stops it from just expanding the row (as far as the table is concerned the number takes up no space at all). Then we apply it to the whole column at once using Martin Scharrer's collcell (which loads array behind the scenes), and use \multicolumn to stop the column title from going awry.

Using booktabs is just to make it look prettier.

(I'll also note that there are probably better ways to code the shift distance than hard-coding it as -1ex!)

share|improve this answer
    
Thanks for the nice answer including an explanation and screenshot! –  fuenfundachtzig Sep 26 '11 at 12:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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