4

I am trying to create the following picture using TikZ. I have come up with something, but I am wondering if there is a more efficient way to do this. You can see each part of the integrand is its own node and I am trying to connect text to each part of it.

Any help would be great.

\documentclass[11pt]{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{positioning}
\usetikzlibrary{quotes}
\usetikzlibrary{shapes}
\usetikzlibrary{shadings}
\usetikzlibrary{arrows.meta}
\usepackage{enumitem}

 
\begin{document}
   
    \begin{tikzpicture}
        \node (r) at (0,0) {\(y=\displaystyle\int\)};
        \node[right] (i) at (.25,0){\(f(x)\)};
        \node[right] (v) at (1,0){\(dx\)=};
        \node[right] (w) at (2,0){\(F(x)\)};
        \node[right] (y) at (2.95,0){\(+ C\)};
        \node (s) at (1,2) [draw,color=red,fill=yellow,text=blue]{Variable of Integration};
        \node (p) at (0,-2) [draw,color=red,fill=yellow,text=blue]{Integrand};  
        \draw (p) edge[->] (i);
        \draw (s) edge[->] (v);
\node (a) at (6,2) [draw,color=red,fill=yellow,text=blue]{Constant of Integration};  
\node (b) at (6,-2) [draw,color=red,fill=yellow,text=blue]{An Antiderivative of \(f(x)\)};  
     \draw (a) edge[->] (y);
        \draw (b) edge[->] (w);
    \end{tikzpicture}
\end{document}

Desired:

Picture

From code: code

1
  • 1
    I would probably use tikzmarks for this. That way you can keep the spacing correct in the equation and use a regular number if desired. Or use a subnode from the same package. But you do then have to allow for the space taken by the annotations explicitly.
    – cfr
    Nov 29, 2023 at 18:35

3 Answers 3

6

This example demonstrates two approaches using tikzmark. The first uses the \tikzmark command, which is more fiddly but shouldn't affect the spacing in the equation. The second uses \tikzmarknode, which is simpler but may affect the spacing in the equation.

Both methods require you to explicitly allow for the additional vertical space taken by the annotations. Both also require two compilations.

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{tikzmark}
\usetikzlibrary{calc}

\tikzset{%
  int label/.style={draw,red!50!black}
}
 
\begin{document}
\begin{equation}
      y = \int f(x) dx = F(x) + C
\end{equation}

\vspace*{40pt}
\begin{equation}
  y = \int \tikzmark{f1}f(x)\tikzmark{f2} \tikzmark{dx1}dx\tikzmark{dx2} = \tikzmark{fx1}F(x)\tikzmark{fx2} + \tikzmark{c1}C\tikzmark{c2}
\end{equation}
\begin{tikzpicture}[remember picture,overlay]
  \foreach \i/\j/\k/\l in {f/-40pt/-15pt/Integrand,dx/40pt/-15pt/Variable of Integration,fx/-40pt/15pt/An Antiderivative of $f(x)$,c/40pt/15pt/Constant of Integration}
  {
    \ifdim\j>0pt
      \coordinate [yshift=\baselineskip] (\i)  at ($(pic cs:\i1)!.5!(pic cs:\i2)$);
    \else
      \coordinate [yshift=-.25\baselineskip] (\i) at ($(pic cs:\i1)!.5!(pic cs:\i2)$);
    \fi
    \ifdim\k<0pt
      \def\myanchor{east}
    \else
      \def\myanchor{west}
    \fi
    \draw [Latex-,int label] (\i) |- ++(\k,\j) node (caption \i) [int label,anchor=\myanchor] {\strut\l};
  }
\end{tikzpicture}
\vspace*{40pt}

\vspace*{40pt}
\begin{equation}
  y = \int \tikzmarknode{fn}{f(x)} \tikzmarknode{dxn}{dx} = \tikzmarknode{fxn}{F(x)} + \tikzmarknode{cn}{C}
\end{equation}
\begin{tikzpicture}[remember picture,overlay]
  \foreach \i/\j/\k/\l in {f/-40pt/-15pt/Integrand,dx/40pt/-15pt/Variable of Integration,fx/-40pt/15pt/An Antiderivative of $f(x)$,c/40pt/15pt/Constant of Integration}
  {
    \ifdim\k<0pt
      \def\myanchor{east}
    \else
      \def\myanchor{west}
    \fi
    \draw [Latex-,int label,shorten <=1.5pt] (\i n) |- ++(\k,\j) node (caption \i n) [int label,anchor=\myanchor] {\strut\l};
  }  
\end{tikzpicture}
\vspace*{40pt}
  
\end{document}

Equation (1) is typeset without annotations as a 'baseline'. Equation (2) uses \tikzmark and equation (3) \tikzmarknode.

two strategies using tikzmark following a version without annotations for comparison

You may, of course, draw the arrows at angles if you prefer. I did it this way partly to demonstrate a variation of the layout and partly because it makes it easy to adjust spacing, but mostly because I often have a personal preference for this style.

5
  • looks great, but when I run the file I get an error because of the int label, it says "I don't know the key"
    – Nick B
    Nov 30, 2023 at 0:14
  • @NickB Did you compile the code I posted or did you copy-paste the picture(s)? The \tikzset{% int label/.style={draw,red!50!black} } in the preamble defines the key int label. If you omitted that, you would get that error.
    – cfr
    Nov 30, 2023 at 0:52
  • I am wondering if I need to update Tex. Your file compiled when I tried it in Overleaf, but not when I tried in TeXShop
    – Nick B
    Nov 30, 2023 at 12:25
  • @NickB That's weird. How old is your TeX? There's nothing recent in the PGF stuff I used, though it's possible the tikzmark stuff might not work with a significantly older version of that package.
    – cfr
    Nov 30, 2023 at 17:07
  • 1
    I restarted the program and recopied the code. It worked after that. No idea what I did wrong the first time.
    – Nick B
    Dec 1, 2023 at 11:30
3

Similar to @cfr answer (+1):

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                tikzmark}
\tikzset{
 is/.style = {inner ysep=2pt},
arr/.style = {-Straight Barb},
  N/.style = {draw= red, fill=yellow!30, minimum height=8mm, inner ysep=0pt,
              font=\footnotesize, text=blue, align=center},
        }

\begin{document}
\[
y = \int  \tikzmarknode[is]{A}{f(x)}\,d\tikzmarknode[is]{B}{x\vphantom{()}} 
    = \tikzmarknode[is]{C}{F(x)} + \tikzmarknode[is]{D}{C\vphantom{()}}
%
    \begin{tikzpicture}[
overlay, remember picture,
                        ]
\draw[arr]  (A.south) -- ++ (-0.5,-0.5) node[N,below] {Integrand};
\draw[arr]  (B.south) -- ++ (+0.5,-0.5) node[N,below] {Variable of\\ Integration};
\draw[arr]  (C.north) -- ++ (-0.5,+0.5) node[N,above] {Antiderivative\\ of $f(x)$};
\draw[arr]  (D.north) -- ++ (+0.5,+0.5) node[N,above] {Integration\\ Constant};
\end{tikzpicture}
\vspace{3ex}    % space for image
\]
\end{document}

Addendum:
More complete example with equation explanation design more similar to one in @cfr answer, but code is more simple (elementary/shorter) with care for space for it:

\documentclass{article}

\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                tikzmark}
\tikzset{
 is/.style = {inner ysep=3pt, fill=gray!15},
arr/.style = {-Straight Barb},
  N/.style = {draw= red, fill=yellow!15,
              font=\footnotesize, text=blue},
        }

\begin{document}
\lipsum[23]
\vskip 7mm %space for image above equation
\[
y = \int  \tikzmarknode[is]{A}{f(x)}\,d\tikzmarknode[is]{B}{x\vphantom{()}}
    = \tikzmarknode[is]{C}{F(x)} + \tikzmarknode[is]{D}{C\vphantom{()}}
\]
    \begin{tikzpicture}[
node distance = 4mm and 2mm,
remember picture, overlay,
                        ]
\node (a) [N,below left =of A]  {Integrand};
\node (b) [N,below right=of B]  {Variable of Integration};
\node (c) [N,above left =of C]  {Antiderivative of $f(x)$};
\node (d) [N,above right=of D]  {Integration Constant};
%
\draw[arr]  (a) -| (A);
\draw[arr]  (b) -| (B);
\draw[arr]  (c) -| (C);
\draw[arr]  (d) -| (D);
    \end{tikzpicture}
\vskip 4mm  %space for image below equation
\noindent% if needed
\lipsum[66]
\end{document}

enter image description here

2

Here's a way to do it. Basic ideas:

  • use one node to place the whole equation
  • position 4 nodes around it
  • connect by adjusting anchor points

Packages

I commented out those, neither needed here nor used by you. However, package amsmath might be useful for you.

Equation

Nothing special here. For later references I named it (E). For later explanations I drew its shape in blue.

    % ~~~ equation ~~~~~~~~~~~~~~~~~
    \node[draw=blue!10] (E) at (0,0) {$y= \int{f(x) dx} = F(x) + C$};

Labels

Position them as you need. Fine tune later. Style box will be explained later.

    % ~~~ labels ~~~~~~~~~~~~~~~~~~~
    ...
    \node[box] (C) at( 2,1.5) {Constant of\\integration};
    \node[box] (I) at(-1.2,-1.5) {Integrand};
    ...

Connections

Now, that's the interesting part. Just connect them to the equation node (E). For fine-tuning I used shifting the anchor points of the nodes shapes:

  • think of sitting in the nodes center
  • send out a ray at an angle
  • wherever it hits the outer shape, that's the new anchor point
  • think of notation in polar coordinates (E.42)
  • east=0 deg, north=90 deg, west=180 deg, south=270 deg

I put anchors by some trial and error, and left the left one both for training and demonstration: it still points to (E)'s center.

    % ~~~ arrows ~~~~~~~~~~~~
    \draw[arr] (A) -- (E);          % start like this
    \draw[arr] (I.50)  -- (E.200);      % fine-tune later
    ...

Styles

  • col defines this dark-red tint.
  • box takes care of multilines, shapes color col, and some fill color
  • arr simplifies using arrow tips
  • > is redefined to a nicer tip
 \begin{tikzpicture}[% self-defined styles
    col/.style={draw=red!80!black!100},
    box/.style={align=center,col,fill=gray!10},
    arr/.style={->,col},
    >={Stealth},
    ]

Result and code

result

\documentclass[11pt]{article}
%\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
%\usepackage{pgfplots}
%\pgfplotsset{compat=newest}
%\usetikzlibrary{positioning}
%\usetikzlibrary{quotes}
%\usetikzlibrary{shapes}
%\usetikzlibrary{shadings}
\usetikzlibrary{arrows.meta}
%\usepackage{enumitem}

\usepackage{amsmath}

 
\begin{document}
  
 Alternative:
 \bigskip
    
 \begin{tikzpicture}[% self-defined styles
    col/.style={draw=red!80!black!100},
    box/.style={align=center,col,fill=gray!10},
    arr/.style={->,col},
    >={Stealth},
    ]
    % ~~~ equation ~~~~~~~~~~~~~~~~~
    \node[draw=blue!10] (E) at (0,0) {$y= \int{f(x) dx} = F(x) + C$};
     
    % ~~~ labels ~~~~~~~~~~~~~~~~~~~
    \node[box] (V) at(-1,1.5) {Variable of\\integration};
    \node[box] (C) at( 2,1.5) {Constant of\\integration};
    \node[box] (I) at(-1.2,-1.5) {Integrand};
    \node[box] (A) at( 2.2,-1.5) {An antiderivative\\of $f(x)$};
    
    % ~~~ arrows ~~~~~~~~~~~~
    \draw[arr] (V.330) -- (E.100);
    \draw[arr] (C.270) -- (E.10);
    \draw[arr] (I.50)  -- (E.200);      % fine-tune later
    \draw[arr] (A) -- (E);          % start like this
 \end{tikzpicture}
\end{document}

You must log in to answer this question.

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