Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to get the math part the equation to be centered with the tikz picture:

\begin{equation}
  \mathcal{G} = 
  \begin{tikzpicture}[]
    \tikzstyle{vertex}=[anchor=base,baseline,
    circle,fill=black!25,minimum size=18pt,inner sep=2pt]
    \node[vertex] (G1) at (0,0)   {1};
    \node[vertex] (G2) at (1,0)   {2};
    \node[vertex] (G3) at (1,1)   {3};
    \node[vertex] (G4) at (0,1)   {4};
    \draw (G1) -- (G2) -- (G3) -- (G4) -- (G1) -- cycle;
  \end{tikzpicture}
\end{equation}

But instead it places it on an even baseline:

enter image description here

My attempts at the tikz portion come from the related questions:

Common baseline in TikZ and Mathmode

How to align a series of TikZ pictures at the baseline

I think I'm misunderstanding the syntax between \tikz \node, \tikzpicture and \tikzstyle. What do I change to align the tikz picture without adjusting the raw coordinates?

share|improve this question
    
You could try baseline=(current bounding box.center). –  Tom Bombadil Oct 3 '12 at 18:55
    
@TomBombadil In the MWE example above, that produces no effect. Is the problem related to the fact that I'm wrapped up in an equation? –  Hooked Oct 3 '12 at 19:00
    
baseline must be used on the tikzpicture not on the nodes. Also \tikzstyle is deprecated in favor of \tikzset{vertex/.style=..}. –  Martin Scharrer Oct 3 '12 at 19:04

2 Answers 2

up vote 11 down vote accepted

You need to use baseline on the tikzpicture, not on the nodes. As Tom Bombadil mentioned in the comments you can use current bounding box.center to put the center of the TikZ picture on the baseline. I would also add an yshift to center it around the middle of the = sign. -.5ex seems to do it.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{equation}
  \mathcal{G} = 
  \begin{tikzpicture}[baseline={([yshift=-.5ex]current bounding box.center)},vertex/.style={anchor=base,
    circle,fill=black!25,minimum size=18pt,inner sep=2pt}]
    \node[vertex] (G1) at (0,0)   {1};
    \node[vertex] (G2) at (1,0)   {2};
    \node[vertex] (G3) at (1,1)   {3};
    \node[vertex] (G4) at (0,1)   {4};
    \draw (G1) -- (G2) -- (G3) -- (G4) -- (G1) -- cycle;
  \end{tikzpicture}
\end{equation}

\end{document}

Result

share|improve this answer
    
Why the yshift (and how will I know what the proper shift is ahead of time, trial and error)? –  Hooked Oct 3 '12 at 19:09
    
@MartinScharrer of course i should have seen that shifting against the bounding box is much more clever than using the node positions, great style! :) –  zeroth Oct 3 '12 at 19:11
    
@Hooked: The yshift centers it around the middle of the = sign. I got the .5ex indeed by trail and error. 1ex is about the height of a normal x. –  Martin Scharrer Oct 3 '12 at 19:12

There are a couple of things wrong with your setup.

  1. \tikzstyle are meant to be declarations which are global. This means that they are like preample statements where you initialize some styles that gets used a lot. Thus you should not put this inside the \begin{tikzpicture}. Also (as Martin correctly states), use the \tikzset or \pgfkeys commands instead, they are much more consistent).

  2. Temporary styles can be created in the \begin{tikzpicture}[<here>] segment of the picture.

  3. baseline is a style affecting the tikzpicture and is thus only used in the \begin{tikzpicture}[baseline].

For a reference about styles and their scoping you can see one of my previous answers: Scopes of styles

Ok, so a quick fix for your solution would be to do the following:

\begin{equation}
  \mathcal{G} = 
  \begin{tikzpicture}[baseline,vertex/.style={anchor=base,
        circle,fill=black!25,minimum size=18pt,inner sep=2pt}]
    \node[vertex] (G1) at (0,0)   {1};
    \node[vertex] (G2) at (1,0)   {2};
    \node[vertex] (G3) at (1,1)   {3};
    \node[vertex] (G4) at (0,1)   {4};
    \draw (G1) -- (G2) -- (G3) -- (G4) -- (G1) -- cycle;
  \end{tikzpicture}
\end{equation}

which yields:

enter image description here

i.e. an alignment against the circle containing 1.

However, if you wish to align against the center of the structure you should specify the baseline shift for the tikzpicture. That is baseline=<length>.
In your case the middle of the figure can be calculated using the calc library. Here you need to realize two things:

  1. The baseline aligns the base of the line to the placement.

  2. You would rather align against the middle of the line.

This means that you need to calculate the shift according to the height of the line (i.e. include the text height in the shift).

This can be done easily by using this style:

baseline=($(G1.base)!.5!(G4.base)$)

This will yield:

enter image description here

share|improve this answer
    
Thanks zeroth, while (Martin's) centering around the box itself seems more robust - I really appreciate the tikz style tips. –  Hooked Oct 3 '12 at 19:14
    
@Hooked that is fine, with my option you can more easily fine tune against certain objects (if that is required). But Martin's is much more clever in obtaining the middle of the entire picture! –  zeroth Oct 3 '12 at 19:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.