5

I'm using the bayesnet TikZ library to draw a graphical model. Node variables might have different size, say $v_0$ and $v_{N-1}$ and the result is that some bubbles are bigger than others. I am trying to use \phantom to create bubbles of the same size like this:

\node[obs] (v0) {$v_{0\phantom{N-}}$};

The problem with this solution is that the label is pushed all the way to the left inside the bubble. Referring to the above fragment, is there a way to compute the space taken by {N-} (note that it's a subscript) and distribute it evenly to the left and right of $v_0$?

  • 1
    Sure there is, \sbox0{$\scriptstyle N-$} and then you can use \hspace{.5\wd0} on both ends. But why don’t you use the minimum width key to esnure a certain width for all nodes (bubbles). Or even something like Dependent node size in TikZ? – Qrrbrbirlbel Aug 19 '13 at 22:24
  • I had not realized that I could pass the minimum width key. That fixed my problem. – Arrigo Aug 19 '13 at 22:59
1

Yes, you can measure the width. With

\sbox0{$\scriptstyle N-$}

and one

\hspace*{.5\wd0}

before and after the actual content would solve this.

But it is easier to use the text width key (or minimum width subtracting the /pgf/inner xsep values). You can use either \widthof (more robust) or PGFmath’s width function.

Furthermore you can also simply specify any arbitrary value for minimum width or text width.

And if you do not want to give the content before hand you can use the .aux file as in Dependent node size in TikZ.

Code

\documentclass[tikz,convert=false]{standalone}
\begin{document}
\begin{tikzpicture}[nodes=draw]
\node           {$v_{N-1}$};
\node at (0,-.5) {$v_0$};
\end{tikzpicture}
\begin{tikzpicture}[nodes={draw,align=center,text width=\widthof{$v_{N-1}$}}]
\node           {$v_{N-1}$};
\node at (0,-.5) {$v_0$};
\end{tikzpicture}
\begin{tikzpicture}[nodes={draw,align=center,text width=width("$v_{N-1}$")}]
\node           {$v_{N-1}$};
\node at (0,-.5) {$v_0$};
\end{tikzpicture}
\begin{tikzpicture}[nodes={draw,minimum width=+3.5em}]
\node           {$v_{N-1}$};
\node at (0,-.5) {$v_0$};
\end{tikzpicture}
\begin{tikzpicture}[nodes=draw]
\node           {$v_{N-1}$};
\node at (0,-.5) {\sbox0{$\scriptstyle N-$}\hspace*{.5\wd0}$v_0$\hspace*{.5\wd0}};
\end{tikzpicture}
\end{document}

Output

Original

enter image description here

\widthof{…}

enter image description here

width("…")

enter image description here

Arbitrary value

enter image description here

Measuring

enter image description here

| improve this answer | |

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.