7

I have a table like this:

\begin{tabular}[b]{c}
A \\
B \\
C \\
D \\
\end{tabular}E

The code above places "D" and "E" on the same baseline, no matter what size any of the letters are.

Without changing "E" (I can't make changes to this text), how can I adjust the table, such that "C" and "E" are on the same baseline, no matter what size any of the letters are?

David Carlisle
  • 757,742
Village
  • 13,603
  • 23
  • 116
  • 219

3 Answers3

6

A much simpler solution, using TeX commands. Remember that for TeX everything are boxes.

\documentclass{article}
\begin{document}
\mbox{}\raise-6.0pt\hbox{
\begin{tabular}[c]{c}
A \\
B \\
C \\
D \\
\end{tabular}}E
\end{document}
David Carlisle
  • 757,742
yannisl
  • 117,160
  • 4
    How did you know it is "-6.5 pt"? – Village Jan 21 '12 at 12:30
  • 1
    Why not \raisebox{-6.5pt}{\begin{tabular}...} that would dispense with a mysterious \mbox{} and foreign syntax? – egreg Jan 21 '12 at 13:28
  • @Vilage It should be half the baseline. Actually it should be 6pt. I edited my post for this. One could incorporate the calculation in the code. – yannisl Jan 21 '12 at 13:33
  • @egreg originally I had a \leavevmode, which I thought would confuse the OP. I am not too sure if I agree with your statement of foreign syntax. When I started with programming we always learned the primitives and started building abstractions on top of them. A lot of new programmers start from the abstractions and then learn the primitives. The reason I offered this solution, is that a lot of newcomers to (La)TeX don't think in boxes. One needs to think inboxes to understand TeX. – yannisl Jan 21 '12 at 13:43
  • That's why \raisebox does automatically a \leavevmode: where doesn't it lead to "think in boxes"? – egreg Jan 21 '12 at 13:45
4

Assuming no \hline and that the table cells have "normal" size, this should work (the setting of \arraystretch is just to show that it can be done):

\documentclass{article}
\newsavebox{\lowertabularbox}
\newenvironment{lowertabular}[1][0]
  {\newcommand{\lowertabulararg}{#1}\begin{lrbox}{\lowertabularbox}
   \begin{tabular}[b]}
  {\end{tabular}\end{lrbox}%
   \raisebox{-\dimexpr\arraystretch\baselineskip*\lowertabulararg\relax}{\usebox{\lowertabularbox}}}

\begin{document}
\renewcommand{\arraystretch}{1.4}
\begin{lowertabular}[0]{c}
A \\
B \\
C \\
D \\
\end{lowertabular}E
\begin{lowertabular}[1]{c}
A \\
B \\
C \\
D \\
\end{lowertabular}E
\begin{lowertabular}[2]{c}
A \\
B \\
C \\
D \\
\end{lowertabular}E
\begin{lowertabular}[3]{c}
A \\
B \\
C \\
D \\
\end{lowertabular}E

some text below to ensure that all is going well
\end{document}

enter image description here

David Carlisle
  • 757,742
egreg
  • 1,121,712
4

Here's a solution using TikZ nodes for positioning on baselines. In the example, we align at the third row.

\documentclass{article}
\usepackage{tikz}
\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,
    baseline=(#1.base)] \node (#1) {#2};}
\begin{document}
\tikz[overlay,remember picture,baseline=(cell.base)]{
  \node (table) {\begin{tabular}{c}
  A \\
  B \\
  \tikzmark{cell}{C} \\
  D \\
  \end{tabular}};}
\quad text aligned with the third row
\end{document}

table with customized alignment

The macro \tikzmark produces a node we called cell. The whole table has been put into a node contained in a TikZ environment, and we defined that the baseline of this environment (with the table) should be the base of this node. This way the table gets the desired baseline, matching the baseline of the following text.

Once having such TikZ nodes, it gives you further possibilities, such as drawing arrows or braces or using it for further alignment. Have a look at Highlighting elements in a matrix to see what I mean.

Stefan Kottwitz
  • 231,401