27

Possible Duplicate:
How to align a series of TikZ pictures at the baseline

I want to mix my formulas with some TikZ graphics. For example:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{equation}
  n\cdot
  \tikz[baseline]\node [minimum height=2em,draw=black] {$m$}; \cdot
  \tikz[baseline]\node [draw=black] {a}; \cdot
  b
\end{equation}
\end{document}

This generates the following output:

Latex Output

However, the baseline from Tikz doesn't fit with that from the equation. You see, the m is deeper than the n and the dot is anywhere above it.

How can I make all the text on one common baseline?

helami
  • 485

2 Answers2

25

The baseline for the box is the bottom of the box. The default behavior is to align to the baseline:

\tikz \node [minimum height=2em,draw=black] {$m$};

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{equation}
  n\cdot
  \tikz \node [minimum height=2em,draw=black] {$m$}; \cdot
  \tikz \node [draw=black] {a}; \cdot
  b
\end{equation}
\end{document}

If you want to align the text at the baseline, then as per Jake's suggetsion you need to add anchor=base:

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{equation}
  n\cdot
  \tikz [anchor=base, baseline] \node [minimum height=2em,draw=black] {$m$}; \cdot
  \tikz [anchor=base, baseline] \node [draw=black] {a}; \cdot
  b
\end{equation}
\end{document}
Peter Grill
  • 223,288
  • 6
    Just to clarify how this works: anchor is a node option, not a tikzpicture option (it defines how a node is anchored, not a tikzpicture). In this case it's fine, because the options passed to \tikz are automatically applied to all nodes. – Jake Jun 03 '12 at 20:04
  • 5
    Incidentally, baseline is short for baseline=0pt. – Andrew Stacey Jun 03 '12 at 20:47
  • How about \tikzset{align properly/.style={baseline=0pt,every node/.style={anchor=base}}} \tikz[align properly] \node {m}; – Andrew Stacey Jun 03 '12 at 20:49
  • 1
    @AndrewStacey yes but \tikz[align properly] \node at (0,1em) {m}; is wrong. anchor=base is correct only because the current point is (0,0). – Alain Matthes Jun 03 '12 at 22:37
  • @Altermundus Yes, but for this particular use-case I wouldn't expect anyone to do that! Actually, I would imagine that if someone wrote that then they would want the m lifted by 1em off the baseline which is what would happen with align properly. Anyway, there's more than one way to skin a cat, as the saying goes. – Andrew Stacey Jun 04 '12 at 07:10
  • Or how about: \tikzset{align at last node/.style={baseline=(last node.base),every node/.append style={name=last node}}}? – Andrew Stacey Jun 04 '12 at 07:15
  • @AndrewStacey I'm not sure and I love too much cats to try ! My point of view is pedagogic and perhaps my english is not good enough to explain exactly what I want to say. The problem is that the question appears regularly. Globally there is only two ways to place the final box : baseline= dim or baseline= coord. The second one is the more easier to use. Now I agree that you have several ways to give a coordinate. It's possible to use the node current bounding box, last node or any node inside the picture. – Alain Matthes Jun 04 '12 at 08:06
  • @Altermundus +1 for being pedagogical. It's better to explain what each piece does (as you do) than give a hundred different ways of doing it with no information to help the user decide what to do in any particular case. – Andrew Stacey Jun 04 '12 at 10:12
11

I think it's preferable to avoid particular case. The result of a tikz's environment is a box with a width, a height and a depth. Normally, the lower end of the picture is put on the current baseline. We can consider in this case that the depth is null. You can modify the value of the depth and of the height with the use of the option baseline (option of tikzpicture).

In the next picture, the circle is placed at (0,0)

enter image description here

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

a\tikz [baseline=0pt] {\draw (-1em,-1em) rectangle (1em,2em);
                  \fill (-1em,0) circle (2pt);
                  \draw[dashed] (-1em,0em) --(1em,0em);  }b 
                  \tikz  {\draw (-1em,-1em) rectangle (1em,2em);
                  \fill (-1em,0) circle (2pt);
                  \draw[dashed] (-1em,0em) --(1em,0em);  }
\end{document} 

The first rectangle is placed with the circle on the baseline, for the second rectangle without the baseline option the depth of the box is null.

The problem now is how to use the baseline option. The pgfmanual says /tikz/baseline=⟨dimension or coordinate or default ⟩ with default =0pt. To avoid the use of a dimension we can use coordinate. It's possible to name the coordinates with tikz, so it's easy to use a name but if you have no name in your picture, tikz allows the use of a particular node : it's the current bounding box.

For example,

 a\tikz [baseline=(current bounding box.base)] {%
        \draw (-1em,-1em) rectangle (1em,2em);
        \fill (-1em,0) circle (2pt);
        \draw[dashed] (-1em,0em) --(1em,0em);  }b . 

gives the same result as [baseline=0pt]

Particular case with only one node in the picture

It's possible to use anchor = base because the current coordinate is (0,0) but

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

   a\tikz[baseline] \draw (0,-1em) -- (0,2em) +(1em,0) node[anchor=base] {$m$};  

   \end{document}   

is wrong. anchor=baselike jake wrote is an option for the nodes.

enter image description here

The (0,0) is placed on the baseline but not the text. anchor=base is not very useful in this case.

Now you can use a coordinate defined in the picture

if in the picture there are several nodes, we need to define a particular node

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

   a\tikz[baseline=0pt] {\node at (0,-1em) {$m$}; 
                              \node(x) at (1em,2em) {$n$};
                              \node at (2em,3em) {$p$};}b                                                                      
   c\tikz[baseline=(x.base)] {\node at (0,-1em) {$m$}; 
                              \node(x) at (1em,2em) {$n$};
                              \node at (2em,3em) {$p$};}d  

   \end{document}   

In this case, the (x.base) is placed on the baseline. Tikz modified the depth of the final box to place n on the baseline.

enter image description here

Alain Matthes
  • 95,075
  • Maybe I'm misunderstanding something, but it seems like the first part of your answer just explains that baseline = baseline=0pt = baseline=(current bounding box.base). Is that the case? – Jake Jun 04 '12 at 06:59
  • @Jake My answer contains two parts. The first part explains (try to ...) how to use baseline to align the final box with the current baseline. In the second part I try to explain that there are two kinds of picture : 1) picture without nodes ( except the node current bounding box) in this case baseline=0pt put (0,0) on the current baseline 2) If the picture contains specific coordinates then it's possible to use the name of the coordinates. anchor=base is used only with a particular case (a picture with only one node at (0,0). – Alain Matthes Jun 04 '12 at 12:36