# Shift entire TikZ-picture vertically into part of align environment

I am trying to move a TikZ-picture upward so that it eliminates the amount of space taken up for my solution. How can I move the TikZ-picture upward (just a bit) into the align-environment? FYI, the following is the exam problem.

"The midpoint of the line segment from $(-2,1)$ to a point $P$ is $(1, -1)$. Determine the coordinates of $P$."

The following is my solution to the exam problem.

Let $P = (x,y)$. We need to find $x$ and $y$. We have
\begin{align*}
\text{Midpoint} = \biggl( \frac{-2 + x}{2} , \frac{1 + y}{2} \biggr)
(1,-1) = \biggl( \frac{-2 + x}{2} , \frac{1 + y}{2} \biggr)\\
\begin{dcases}
1 = \frac{-2 + x}{2}\\
-1 = \frac{1 + y}{2}
\end{dcases}\\
\begin{dcases}
2 = -2 + x\\
-2 = 1 + y
\end{dcases}
\begin{dcases}
x = 4\\
y = -3.
\end{dcases}
\end{align*}
%\begin{minipage}[t]{5cm}{        %DO I NEED THIS?
\begin{tikzpicture}[>=latex',scale=0.5]
% Draw grid lines
\draw[help lines] (-2.5,-3.5) grid (4.5,1.5);
% Draw x-axis
\draw[very thick,->] (-3,0) -- (5.5,0)
node[right] {\large $x$};
% Draw y-axis
\draw[very thick, ->] (0,-4) -- (0,2.5)
node[above] {\large $y$};
% Special points
\node[above, fill=SolutionColor] at (-2,1) {$(-2,1)$};
\fill (-2,1) circle (4pt);
\node[right, fill=SolutionColor] at (1.5,-1) {$(1,-1)$};
\fill (1,-1) circle (4pt);
\node[right, fill=SolutionColor] at (4,-3) {$P$};
\draw (-2,1) -- (1,-1) -- (4,-3);
\fill[red] (4,-3) circle (4pt);
\end{tikzpicture}
%\end{minipage}%    DO I NEED THIS?


-
A complete working example is preferable. – Alain Matthes Jul 13 '12 at 19:37
As Altermundus said, you should make sure your code is complete and compilable. In this case, one had to figure out that the code needs the tikz and mathtools packages, the arrows library, and some custom colour. It's always best to include the necessary preamble, so others can just copy and paste the code into a new document without having to make any adjustments. – Jake Jul 13 '12 at 20:13

This is a job for Martin Scharrer's excellent adjustbox package: It allows (among many other things) to trim the top off a box (which can contain, among many other things, a tikzpicture), so its official height is smaller than the actual height of the content.

If you wrap your tikzpicture in \begin{adjustbox}{trim=0 0 0 2.5cm} ... \end{adjustbox}, you'll get

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
Let $P = (x,y)$. We need to find $x$ and $y$. We have
\begin{align*}
\text{Midpoint} = \biggl( \frac{-2 + x}{2} , \frac{1 + y}{2} \biggr)
(1,-1) = \biggl( \frac{-2 + x}{2} , \frac{1 + y}{2} \biggr)\\
\begin{dcases}
1 = \frac{-2 + x}{2}\\
-1 = \frac{1 + y}{2}
\end{dcases}\\
\begin{dcases}
2 = -2 + x\\
-2 = 1 + y
\end{dcases}
\begin{dcases}
x = 4\\
y = -3.
\end{dcases}
\end{align*}

\begin{tikzpicture}[>=latex',scale=0.5]
% Draw grid lines
\draw[help lines] (-2.5,-3.5) grid (4.5,1.5);
% Draw x-axis
\draw[very thick,->] (-3,0) -- (5.5,0)
node[right] {\large $x$};
% Draw y-axis
\draw[very thick, ->] (0,-4) -- (0,2.5)
node[above] {\large $y$};
% Special points
\node[above, fill=white] at (-2,1) {$(-2,1)$};
\fill (-2,1) circle (4pt);
\node[right, fill=white] at (1.5,-1) {$(1,-1)$};
\fill (1,-1) circle (4pt);
\node[right, fill=white] at (4,-3) {$P$};
\draw (-2,1) -- (1,-1) -- (4,-3);
\fill[red] (4,-3) circle (4pt);
\end{tikzpicture}
\end{document}

-

It's possible to use \useasboundingbox. You need to find the dimensions of the rectangle to place the picture correctly.

 \documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
Let $P = (x,y)$. We need to find $x$ and $y$. We have
\begin{align*}
\text{Midpoint} = \biggl( \frac{-2 + x}{2} , \frac{1 + y}{2} \biggr)
(1,-1) = \biggl( \frac{-2 + x}{2} , \frac{1 + y}{2} \biggr)\\
\begin{tikzpicture}[>=latex',scale=0.5]
\useasboundingbox (0,0) rectangle (7,3);
% Draw grid lines
\draw[help lines] (-2.5,-3.5) grid (4.5,1.5);
% Draw x-axis
\draw[very thick,->] (-3,0) -- (5.5,0)
node[right] {\large $x$};
% Draw y-axis
\draw[very thick, ->] (0,-4) -- (0,2.5)
node[above] {\large $y$};
% Special points
\node[above, fill=white] at (-2,1) {$(-2,1)$};
\fill (-2,1) circle (4pt);
\node[right, fill=white] at (1.5,-1) {$(1,-1)$};
\fill (1,-1) circle (4pt);
\node[right, fill=white] at (4,-3) {$P$};
\draw (-2,1) -- (1,-1) -- (4,-3);
\fill[red] (4,-3) circle (4pt);
\begin{dcases}
1 = \frac{-2 + x}{2}\\
-1 = \frac{1 + y}{2}
\end{dcases}\\
\begin{dcases}
2 = -2 + x\\
-2 = 1 + y
\end{dcases}

That seems to also shift the last two align lines. – Peter Grill Jul 14 '12 at 16:24
Yes because I placed the picture with the second \implies but it's possible to place it with the third \implies. We have the choice. – Alain Matthes Jul 14 '12 at 16:55