17

I often find myself typing equations into a node of a tikz picture. I then often find that I want to isolate certain characters in the equation and define them as a node.

For instance, consider the following figure.

\documentclass{standalone}

\usepackage{tikz}

\begin{document} \begin{tikzpicture} \node {$e^{i\pi}+1=0$}; \end{tikzpicture} \end{document}

enter image description here

I'd like it if in this figure I could define each character e, i, pi, +, 1, =, and 0 as its own node. Is this possible?

1 Answers1

22

The \subnode command from tikzmark is designed for this sort of situation. It fakes a node around text that's inside a \node. It does need a few compilation runs to settle (it took 3 for this code for me) and you must remember the remember picture key on the tikzpicture.

\documentclass{article}
%\url{https://tex.stackexchange.com/q/582127/86}

\usepackage{tikz} \usetikzlibrary{tikzmark, arrows.meta}

\begin{document} \begin{tikzpicture}[>=Latex,remember picture] \node (eipi) at (0,0) {(e^{i\pi}+1=0)}; \node at (0,-1) {(\subnode{e}{e}^{\subnode{i}{i}\subnode{pi}{\pi}}+\subnode{one}{1}=\subnode{zero}{0})}; \draw[<-] (e) to[out=-90, in=90] ++(-2,-1) node[below] {Base of natural logarithm}; \draw[<-] (i) to[out=135, in=0] ++(-2,1) node[left] {A square root of (-1)}; \draw[<-] (pi) -- ++(0,-2) node[below] {Area of a unit circle}; \draw[<-] (one) to[out=-45, in=180] ++(2,-1) node[right] {Unity}; \draw[<-] (zero) -- ++(2,0) node[right] {Zilch}; \draw[<-] (eipi) -- ++(0,1) node[above] {An ugly equation}; \end{tikzpicture} \end{document}

Annotated equation with subnodes

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • 1
    This has become my new workhorse! It's helping a lot with beamer slides that I'm writing for the course I'm teaching.

    I'm curious why you've started the math text in the nodes with \(. I was writing a document earlier that didn't work correctly when I used $ but then did work when I switched to \(. Any idea why?

    – Brian Fitzpatrick Feb 20 '21 at 20:44
  • 1
    @BrianFitzpatrick I don't know of a reason why it wouldn't work with $. If you have an example then feel free to send it my way (open an issue on https://github.com/loopspace/tikzmark or email me directly - my email is in the tikzmark documentation). In general, though, \( is to be preferred to $, see https://tex.stackexchange.com/q/510/86 for a bit more on that. – Andrew Stacey Feb 21 '21 at 19:57
  • I'll see if I can build a MWE. The document I was building was very complex so it's definitely possible there was something else going on. – Brian Fitzpatrick Feb 25 '21 at 20:17
  • So I just tried to replicate my issue... and couldn't. However, I'm now almost positive that I was recycling a node name in another tikzfigure, which causes problems with remember picture. When I edited my original document I must have changed the node name in addition to switching the $ to \(. – Brian Fitzpatrick Mar 02 '21 at 19:20
  • @BrianFitzpatrick Well, if it does resurface then do let me know. I'm pleased to hear you got it working. – Andrew Stacey Mar 03 '21 at 08:59