7

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).

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
 \node[circle,draw=black] (A) {$n$};
 \node[circle,draw=black] (B) [right of=A] {$n+1$};
\end{tikzpicture}
\end{document}

enter image description here

3

3 Answers 3

5

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$.

\documentclass{standalone}
\usepackage{stackengine}[2013-09-11]
\def\stacktype{L}
\usepackage{tikz}
\begin{document}
\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$};
\end{tikzpicture}
\end{document}

enter image description here

3
  • Thanks! Many other good answers below, but this one seems to have the easiest-to-remember syntax. 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. Sep 25, 2013 at 12:10
  • 1
    stackengine saved the day twice in the last two weeks so thanks a lot for that ! :)
    – percusse
    Sep 25, 2013 at 12:27
8

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.

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
 \node[circle,draw=black,align=center,text width={width("$n+1$")}] (A) {$n$};
 \node[circle,draw=black] (B) [right=0 of A] {$n+1$};
\end{tikzpicture}
\end{document}

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

4
  • That's perfect english right there....
    – percusse
    Sep 25, 2013 at 10:42
  • I wasn't aware of the width function in PGF. Quite handy!
    – Daniel
    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). Sep 25, 2013 at 11:19
  • Very nice answer, too. PGF is stacked with useful functions I am not aware of... Sep 25, 2013 at 12:06
5

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:

\makeatletter
\newbox\eq@box
\newdimen\eq@w
\newdimen\eq@h
\newdimen\eq@d
\def\rb{\eq@w=0pt\eq@h=0pt\eq@d=0pt}
\def\sb#1{%
  \setbox\eq@box\vbox{\hbox{#1}}%
  \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%
}
\def\ub#1{\raisebox{0pt}[\eq@h][\eq@d]{\makebox[\eq@w][c]{#1}}}
\makeatother

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]
 \rb\sb{$n$}\sb{$n+1$}
 \node[circle,draw=black] (A) {\ub{$n$}};
 \node[circle,draw=black] (B) [right of=A] {\ub{$n+1$}};
\end{tikzpicture}

example-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]
 \rb
 \sb{$a_1+a$}
 \sb{$a_{a_2}+a^b$}
 \sb{$a_{a_{a_3}}+a^{b^{c^d}}$}
 \sb{$a_{a_{a_4}}+a^{b^{c^{d^e}}}$}
 \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}}}$}};
\end{tikzpicture}

example-2

You must log in to answer this question.

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