I apologize in advanced for asking a question that has been asked before. I have read the other posts on SE about vertical alignment but I'm very new to Latex and had difficulty understanding them. I have a two column table. One column with text and the other with a tikz picture. Below is a simplified example.
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tabular}{|p{0.5\textwidth}|c|}
\hline
Hello World &
\begin{tikzpicture}
\draw (-3, -3) rectangle (3,3);
\draw (-3,-3) -- (3,3);
\draw (-3,3) -- (3,-3);
\end{tikzpicture} \\
\hline
\end{tabular}
\end{document}
When I compile using pdflatex from texlive on Crunchbang (debian), Hello World is aligned to the bottom of the first cell. I thought that using p{…} in my column parameters would top align. Can someone explain why this doesn't work and the proper way to do this?
Best Answer
This problem is due to the text on the left being pushed down by the tikzpicture. It can be solved by using
adjustbox
package:EDIT: OK, here are some more explanations to the nature of the problem you encountered.
In LaTeX, the
tabular
environment align the contents of adjacent cell such that their reference points are on the same horizontal line. What the optionp{width}
does is to surround the content of the column with\parbox[t]{...}
. On the other hand, by default, picture generated bytikz
will have their baseline at the bottom. This is why the text on the left get push down by the picture in your example.The
baseline=0pt
argument oftikz
adjusted this by forcing the the position of the the x-axis onto the baseline. Alternatively, you can also usebaseline=(current bounding box.north)
to put the upper end of the picture at the reference line (or surround the picture by\raisebox{-171.11646pt}{...}
, though I'm not entirely sure why isn't the parameter-6cm
which is equivalent to-170.07874pt
, though it may have something to do with the defaultPGF
line width which is0.4pt
). Moreover, what the\vspace{0pt}
at the beginning does is to move the reference point of the cell to that position, and it is why you seemingly achieved top alignment.Here is a picture that demonstrate the differences among three options of tikzpicture (without
baseline
, withbaseline
, and withbaseline=(current bounding box.north)
), notice the position of the reference point in each case (marked usinglayouts
package):Now let's go back to my answer. The
adjustbox
with optionvalign=T
does what essentially the same asbaseline=(current bounding box.north)
, that it moves the baseline to the top of the picture. Nonetheless,valign=t
moves the baseline0.7\baselineskip
away from the top, so you get a better alignment (adjustbox
works withtabularx
too):