[Tex/LaTex] Positioning TikZ pictures

diagramstikz-pgf

How do I position a TikZ picture relative to body text? I have drawn a circle using TikZ and would like to position it to the side of the body text (so it is easy to see as you're reading, but not in the way). I am not sure how to do this. I have tried changing the coordinates to move the picture, but it seems to only move figures relative to each other rather than moving the entire thing. I would appreciate some help with this. Thanks!

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
Body text.
\begin{tikzpicture}
    \draw [fill = lightgray] (0,0) circle [radius = 1.25 cm];
\end{tikzpicture}
\caption{Circular Region}
\end{document}

Best Answer

To make clear the following examples, I'll define a macro which produces a simple tikz picture, such as a circle with some text inside:

\newcommand{\mypicture}[1][]{% 1 optional parameter for options for the tikz picture
\begin{tikzpicture}[#1]
\node[draw, circle, fill=yellow!30, inner sep=2mm] (a) {A};
\end{tikzpicture}
} 

Which produces:

enter image description here

Once you finish a TikZ picture, for TeX it is only a box. Think of it as a "big character" which can be part of a paragraph, like the following:

This paragraph contains a figure \mypicture{} inline.

Inline

As you can see, the "box" containing the image is aligned with the rest of your text at is baseline. Each character has an imaginary line which is used to vertically align it with other characters in the same line. The box can extends above and below of its base line, but by default tikz puts the baseline of the image at the very bottom of the resulting box.

You can change this, giving the option baseline to your tikzpicture. This option can receive as argument a dimension such as 1mm, or the name of any coordinate inside the figure, such as a.center, a.north or even a.base, which is the baseline of the text inside the node a. Lets see these cases:

\parbox{6cm}{
\begin{itemize}
\item Default: \hrulefill\mypicture 
\item 2mm: \hrulefill\mypicture[baseline=-2mm] 
\item Center: \hrulefill\mypicture[baseline=(a.center)]
\item North: \hrulefill\mypicture[baseline=(a.north)] 
\item Base: \hrulefill\mypicture[baseline=(a.base)]
\end{itemize}
}

Vertical align

Of course, in addition to use it "inline" you can put it anywhere you could put any other text, such as in a footnote, a marginal note, a table, or a figure environment (which would make it float). See it for example in a marginal note:

\marginpar{\mypicture[baseline=(a.base)]}
\lipsum[1]

\lipsum[2]

In marginpar

You can also use it in combinations with packages such as wrapfig which al­lows fig­ures or ta­bles to have text wrapped around them.

If you use tikz options remember picture and overlay, you can put it at any absolute position in the page, as the other answer shows.