34

I need to draw a graph where some nodes have the same y-coordinate. I want the text within the nodes on the same y-coordinate to be baseline-aligned. However, the text seems to be aligned by the bottom lines, not the baseline.

Here's what I tried to do:

\documentclass[final,t]{beamer}
\mode<presentation>
\usepackage{tikz}
\usepackage{pgfplots}
\tikzstyle{gate2} = [circle,fill=white,draw=black,minimum size=1cm]
\begin{document}
    \begin{tikzpicture}
        \node[gate2] (mu_0) at (0, 0) {$\mu_0$};
        \node[gate2, baseline = (mu_0.base)] (Lambda_0) at (2,  0) {$\Lambda_0  $};
    \end{tikzpicture}
\end{document}

What I see is that the shapes are at the same y-coordinate (as expected), but mu is higher than Lambda. How do I specify that mu and Lambda should be on the baseline?

  • 3
    You can add anchor=base to the gate2 style. The default anchor is the center anchor. If you load the positioning library you can do base right=of mu_0 at the second node. The value of node distance is then used as the distance between the nodes. (Unrelated to your actual question: Should \tikzset or \tikzstyle be used to define TikZ styles?) – Qrrbrbirlbel Sep 13 '13 at 21:07
  • anchor=base moves the shapes, unfortunately: the circle containing mu becomes lower than the Lambda circle. – vuvuzela Sep 13 '13 at 21:16
  • Also, looks like positioning is not available in my environment, alas: adding \usepackage{positioning} leads to error LaTeX Error: File positioning.sty' not found. [\usepackage]` – vuvuzela Sep 13 '13 at 21:18
  • 1
    That has something to do that \mu_0 is not as high as \Lambda_0 as TikZ positions the boxes containing the text in the center of the shape. You will need to work with \vphantom{\Lambda} in the \mu node or use text height and sometime even text depth. — positioning is a TikZ library not a LaTeX package, so you need to do \usetikzlibrary{positioning}, the problem is still the same though. – Qrrbrbirlbel Sep 13 '13 at 21:32
21

I would just add a \strut to both characters

\documentclass[final,t]{beamer}
\mode<presentation>
\usepackage{tikz}
\usepackage{pgfplots}
\tikzstyle{gate2} = [circle,fill=white,draw=black,minimum size=1cm]
\begin{document}
    \begin{tikzpicture}
        \node[gate2] (mu_0) at (0, 0) {$\strut\mu_0$};
        \node[gate2, baseline = (mu_0.base)] (Lambda_0) at (2,  0) {$\strut\Lambda_0$};
    \end{tikzpicture}
\end{document}

enter image description here

18

Depending on your actual diagram you want to use the TikZ library positioning loaded by

\usetikzlibrary{positioning}

and the base right=of <other node> key.

Either way, you want to use the text height key. You also want to set the text depth key for the very same reasons if you have text of different depth (in your case, the \mu is lower/deeper than the subscript 0 in both nodes).

If you expect only general characters like A-Za-z0-9 and simple subscripts you can easily use font=\vphantom{Ag} which is similar to font=\strut but doesn’t add that much height and depth (which you don’t notice as you use minimum size=1cm here).

The difference is that the keys text height and text depth do set the height and depth no matter the content while \vphantom and \strut only set a minimum height/depth (so to speak).

Code

\documentclass[tikz]{standalone}
\tikzset{gate2/.style={circle, inner sep=+0pt, fill=white, draw=black, minimum size=+1cm}}
\newcommand*{\tfbox}[1]{{\fboxsep-\fboxrule\fbox{#1}}}
\begin{document}
\begin{tikzpicture}[gate2/.append style={anchor=base}]
  \node[gate2] (mu_0)                {\tfbox{$\mu_0$}};
  \node[gate2] (Lambda_0) at (2,  0) {\tfbox{$\Lambda_0$}};
  \draw (mu_0.center) -- (Lambda_0.center) (mu_0.base)   -- (Lambda_0.base);
\end{tikzpicture}
\begin{tikzpicture}[gate2/.append style={text height=\heightof{$\Lambda$},text depth=\depthof{g}}]
  \node[gate2] (mu_0)                {$\mu_0$};
  \node[gate2] (Lambda_0) at (2,  0) {$\Lambda_0$};
  \draw (mu_0.center) -- (Lambda_0.center) (mu_0.base)   -- (Lambda_0.base);
\end{tikzpicture}
\begin{tikzpicture}[gate2/.append style={font=\vphantom{Ag}}]
  \node[gate2] (mu_0)               {$\mu_0$};
  \node[gate2] (Lambda_0) at (2, 0) {$\Lambda_0$};
  \draw (mu_0.center) -- (Lambda_0.center) (mu_0.base)   -- (Lambda_0.base);
\end{tikzpicture}
\end{document}

Output

enter image description here enter image description here enter image description here

  • 1
    As you suggested in a comment above, \vphantom works well. This solution is also tied to the actual symbols being used; is it better than \vphantom? – vuvuzela Sep 13 '13 at 21:56
  • I'm just wondering why TikZ doesn't put the text baseline at the shape center. It's quite sensible, IMO. – vuvuzela Sep 13 '13 at 21:57
  • 1
    @vuvuzela Both solutions have their advantages and disadvantages. If you only use the usual characters like letters and digits you can easily just set font=\vphantom{Ag} which works similar to font=\strut solution but doesn’t add that much height (\strut is higher and deeper than A and g). But \vphantom is not safe when bigger content is used, text height and text depth set the height and depth no matter what the content (which may also not what you’re after). It really depends on the general content of your nodes. Probably, font=\vphantom{Ag} is just fine. – Qrrbrbirlbel Sep 13 '13 at 22:18
  • Thanks for the explanation! I'll go with adding phantom characters for now as a simpler solution, because I haven't got many nodes and the content is always a letter or two. – vuvuzela Sep 13 '13 at 22:23
3

You can accomplish this by setting fixed text height and text depth settings. I used this piece of code, originally for lining up expressions in commutative diagrams:

\tikzstyle{inline text}=[text height=1.5ex, text depth=0.25ex, yshift=0.5mm]

but it also seems to work pretty well for other kinds of diagrams.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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