10

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?

2 Answers 2

11
\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.

2
  • (+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, 2011 at 10:57
  • Using a dimensione expressed in ex units is quite the same.
    – egreg
    Sep 26, 2011 at 11:00
10
\documentclass[a4paper]{article}
\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}
\end{document}

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!)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .