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.

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?

share|improve this question
1  
possible duplicate: [How to align a series of TikZ pictures at the baseline –  Count Zero Jun 3 '12 at 19:51
2  
Agree with the duplicate, but Peter's answer here is the best of the lot. –  Andrew Stacey Jun 3 '12 at 20:09
add comment

marked as duplicate by Count Zero, Andrew Stacey, Peter Grill, lockstep, Marco Daniel Jun 6 '12 at 15:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 11 down vote accepted

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}
share|improve this answer
4  
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 3 '12 at 20:04
1  
Incidentally, baseline is short for baseline=0pt. –  Andrew Stacey Jun 3 '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 3 '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 3 '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 4 '12 at 7:10
show 3 more comments

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

share|improve this answer
    
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 4 '12 at 6: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 4 '12 at 12:36
add comment

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