5

I am learning Tikz and hope somebody can help me achieve the following i.e. I want to draw a directed arc between objects in an equation. Below is a picture of what I am trying to achieve. Arrow in TikZ

I have also attached the code I have used so far:

\documentclass{article}
\usepackage{amsmath,amssymb,braket,tikz}
\usetikzlibrary{tikzmark,calc}

\begin{document}
\begin{tikzpicture}
    $(x+2)(x+3)$
\end{tikzpicture}
\end{document}

I also suspect that there is a way to specify a line or arc, between elements e.g. numbers and letters, without explicitly stating the coordinates. Is this the case? If it were, it would simplify the things I'm trying to achieve.

Any help would be very much appreciated.

Dan317
  • 561
  • 2
  • 6
  • 9

2 Answers2

7

The following solution draws an arc above the formula using arc; the actual angles and lengths may have to be adjusted. To get coordinates relative to the formula, the formula is wrapped into the node formula.

\documentclass{article}
\usepackage{amsmath,amssymb,braket,tikz}
\usetikzlibrary{tikzmark,calc}

\begin{document}
\begin{tikzpicture}
    \node (formula) [] {$(x+2)(x+3)$};
    \draw[-latex,red] ($(formula.north west)+(.4,0)$) arc
    [
        start angle=160,
        end angle=20,
        x radius=0.5cm,
        y radius =0.5cm
    ] ;

\end{tikzpicture}
\end{document}

Output:

enter image description here

jf_
  • 3,099
  • 2
  • 13
  • 31
  • Thanks! That looks superb! what does the -latex do? Also, I'm guessing that the ($(formula.north west)+(.4,0)$) says that the arc should start at the left of the "formula" and adds 0.4 to the x-coordinate to move it slightly to the right? I'm struggling to find a good tutorial on this kind of thing. Do you have any suggestions? – Dan317 May 26 '21 at 10:26
  • @Dan317 `-latex` is the shape of the arrow head. Try with `->` to see the effect – samcarter_is_at_topanswers.xyz May 26 '21 at 10:32
  • @Dan317 correct. The Position is calculated from a point and an offset part. If you enter `[draw]` in the node parameters of `formula` you see the actual shape of the node, where north west is the uppe rleft corner. `+(.4,0)` specifies a position shifted from there on the x-axis. – jf_ May 26 '21 at 11:07
  • A suggestion: [texample.net](https://texample.net/tikz/examples/) is excellent to find inspiration on how to do things. Just try to find smething that matches what you want to do and then take it from the code. – jf_ May 26 '21 at 11:07
  • @samcarter_is_at_topanswers.xyz Thank you. I found the following and I see what -> and latex now mean : https://www.bu.edu/math/files/2013/08/tikzpgfmanual.pdf – Dan317 May 26 '21 at 12:38
  • @Dan317 That's a very old version, see https://texdoc.org/pkg/tikz for a current version of the tikz documentation. – samcarter_is_at_topanswers.xyz May 26 '21 at 12:53
  • @Dan317 In case you happen to speak french, there is a nice book about tikz: http://math.et.info.free.fr/TikZ/index.html – samcarter_is_at_topanswers.xyz May 26 '21 at 12:53
7

Another possibility with the tikzmark library:

\documentclass{article}
\usepackage{amsmath,amssymb,braket,tikz}
\usetikzlibrary{tikzmark,calc}

\begin{document}
\begin{equation}
    (\tikzmarknode{a}{x}+2)(\tikzmarknode{b}{x}+3)
\end{equation}
\tikz[remember picture, overlay]{\draw[-latex,red] ([yshift=0.1em]a.north) to[bend left] ([yshift=0.1em]b.north);}
\end{document}

enter image description here

  • 2
    Rather than using `to[bend left]` you can use the out/in options for better control, e.g., `to[out=60,in=120]` looks good to me. – Forss May 26 '21 at 12:07
  • 1
    @Forss Yeah, there are many alternative approaches to get the bent in the arrow. I choose `bend left` as a simple example. There are also a lot of nice examples with bezier curves in the tikzmark documenation – samcarter_is_at_topanswers.xyz May 26 '21 at 12:26