# Vertical Alignment of tikz picture in a table cell

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?

-
updated my answer with some explanation. –  Francis Aug 7 '13 at 7:35

After I posted I discovered that my problem was actually related to vertical alignment of the tikzpicture. If I replace that portion of the above code with multi-line text I get the alignment I wanted. Searching along those lines I discovered several answers on SE that I was able to combine to solve my problem. I also switched to tabularx because I prefer the X syntax over p{...}.

\documentclass{article}

\usepackage{tikz}
\usepackage{tabularx}

\begin{document}
\begin{tabularx}{\textwidth}{|c|X|}
\hline
Hello World &
\vspace{0pt}
\begin{tikzpicture}[baseline]
\draw (-3, -3) rectangle (3,3);
\draw (-3,-3) -- (3,3);
\draw (-3,3)  -- (3,-3);
\end{tikzpicture}
\\
\hline
\end{tabularx}
\end{document}


There are two keys: one add [baseline] (default is 0pt) to the tikzpicture. If that is all I do I get the text aligned roughly to the middle of the picture. Someone recommended using \vspace{0pt} and I tried that and found it to move the text to the top. I'm not clear on why this solution works but it does.

-

This problem is due to the text on the left being pushed down by the tikzpicture. It can be solved by using adjustbox package:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tabular}{|p{0.5\textwidth}|c|}
\hline
Hello World &
\draw (-3, -3) rectangle (3,3);
\draw (-3,-3) -- (3,3);
\draw (-3,3)  -- (3,-3);
\end{tikzpicture}}\\
\hline
\end{tabular}
\end{document}


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 option p{width} does is to surround the content of the column with \parbox[t]{...}. On the other hand, by default, picture generated by tikz 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 of tikz adjusted this by forcing the the position of the the x-axis onto the baseline. Alternatively, you can also use baseline=(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 default PGF line width which is 0.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, with baseline, and with baseline=(current bounding box.north)), notice the position of the reference point in each case (marked using layouts package):

Now let's go back to my answer. The adjustbox with option valign=T does what essentially the same as baseline=(current bounding box.north), that it moves the baseline to the top of the picture. Nonetheless, valign=t moves the baseline 0.7\baselineskip away from the top, so you get a better alignment (adjustbox works with tabularx too):

-