A thing I'd often like to do is put in my document a box the size of some text A and containing some other text B. For instance, I have two tikz nodes, one containing $n$ and one containing $n+1$, and I want them to be the same size (but not larger than necessary, as I would get by specifying a minimum width).

If I want the text B to be right- or left-aligned, I can do the trick with a combination of phantom and mathrlap/mathllap. However, if I want it to be centered, I don't see an easy way to do it.

Is there a simple command in some package to do this? It looks like a common task.

Minimal example: I want to alter the bounding box of $n$ so that the two circles are the same size (and $n$ is still centered in the circle).

 \node[circle,draw=black] (A) {$n$};
 \node[circle,draw=black] (B) [right of=A] {$n+1$};

enter image description here


3 Answers 3


As far as making the two circles the same size, this is what I propose. It basically overlays the $n$ on the phantom of $n+1$.

\begin{tikzpicture}[node distance=1.2cm]
 \node[circle,draw=black] (A) {\stackon[0pt]{\phantom{$n+1$}}{$n$}};
 \node[circle,draw=black] (B) [right of=A] {$n+1$};

enter image description here

  • Thanks! Many other good answers below, but this one seems to have the easiest-to-remember syntax. Commented Sep 25, 2013 at 12:05
  • @FedericoPoloni Thank you. I think that percusse's comments on right= of are very useful, too. If the content of your nodes is always in math mode, and if you are doing it a lot, the syntax \stackMath \node[circle,draw=black] (A) {\stackon[0pt]{\phantom{n+1}}{n}}; allows the mode to be set where all stack arguments are assumed to be in math mode, thus allowing you to forego the $ delimiters. The \stackMath only needs to be issued once. Commented Sep 25, 2013 at 12:10
  • 1
    stackengine saved the day twice in the last two weeks so thanks a lot for that ! :)
    – percusse
    Commented Sep 25, 2013 at 12:27

Having the n+1 node first might help things to get the size of the node directly but you can use the limited width("<text>") function of pgfmath to get the text width. Then you can center it. By limited I mean if your text is a complex TeX macro it usually fails to understand or expand properly so extra protection is required. However for straightforward text it is quite handy.

 \node[circle,draw=black,align=center,text width={width("$n+1$")}] (A) {$n$};
 \node[circle,draw=black] (B) [right=0 of A] {$n+1$};

enter image description here

Finally a minor nuisance is explained here if you wish to have an idea why of= syntax might not be a good idea

Difference between "right of=" and "right=of" in PGF/TikZ

  • That's perfect english right there....
    – percusse
    Commented Sep 25, 2013 at 10:42
  • I wasn't aware of the width function in PGF. Quite handy!
    – Daniel
    Commented Sep 25, 2013 at 10:52
  • @Daniel See \uline within a PGFmath expression produces an error for the downsides of the PGFmath functions width, height and depth (namely expansion via \edef). Commented Sep 25, 2013 at 11:19
  • Very nice answer, too. PGF is stacked with useful functions I am not aware of... Commented Sep 25, 2013 at 12:06

Here's a dirtier solution which:

  1. easily extends to more than one nodes (without manually keeping score which one is the largest)
  2. differentiates between width, height and depth (the widest node is not necessarily the tallest!), and
  3. aligns the baselines of the text of all nodes.

You'll have to add this to your preamble:

  \ifnum\eq@w<\wd\eq@box \eq@w=\wd\eq@box \fi%
  \ifnum\eq@h<\ht\eq@box \eq@h=\ht\eq@box \fi%
  \ifnum\eq@d<\dp\eq@box \eq@d=\dp\eq@box \fi%

Then you just use the three commands:

  1. \rb for resetting boxes
  2. \sb{t} for declaring that you'll later have text t in a node
  3. \ub{t} for actually using text t in a node

Here's your example:

\begin{tikzpicture}[node distance=1.5cm]
 \node[circle,draw=black] (A) {\ub{$n$}};
 \node[circle,draw=black] (B) [right of=A] {\ub{$n+1$}};


And here's one more which may show you the difference from other solutions that you'll come up with.

\begin{tikzpicture}[node distance=3cm]
 \node[circle,draw=black] (P1) [below of=A] {\ub{$a_1+a$}};
 \node[circle,draw=black] (P2) [right of=P1] {\ub{$a_{a_2}+a^b$}};
 \node[circle,draw=black] (P3) [right of=P2] {\ub{$a_{a_{a_3}}+a^{b^{c^d}}$}};
 \node[circle,draw=black] (P4) [right of=P3] {\ub{$a_{a_{a_4}}+a^{b^{c^{d^e}}}$}};


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .