# Using Tikz, Tikzmark to show differentiation of ln f(x)

I am trying to replicate this diagram. My confusion is due to the reason that I am not starting in an tikzpicture enviroment but in an align* environment. Here is a skeleton of the diagram. I believe this is a correct beginning:

\documentclass{article}
\usepackage{tikz}
\usepackage{mathtools,amsmath,geometry}
\usetikzlibrary{tikzmark}

\begin{document}
\begin{align*}
h(x)&=\ln (x^{2}-8x) && \\
&               && \\
&               && \\
h^{\prime}(x)&=\dfrac{\textcolor{red}{2x-8}}{\textcolor{blue}{x^{2}-8x}} && \\
\end{align*}
\end{document}


This outputs

My goal is to create:

Creating the circles 1 and 2 are possible but programming it right in the context of the labels and arrows is what I am not sure how to approach? Tikzmark is the right tool here.

Something like this? (EDIT: Added descriptions, thanks to ArtificialStupidity for speeding me up!)

1. The braces are done with decorations.pathreplacing. You could also do them with \underbrace and \overbrace.
2. Instead of adding several empty lines, you can produce vertical gaps by adding [distance] after \\. I added one more such distance since the annotations are drawn in overlay mode and would run in the text otherwise.
3. The nodes with texts are placed along the vertical line at certain positions indicated by pos=... (and then moved to the right).

\documentclass{article}
\usepackage{tikz}
\usepackage{mathtools,amsmath,geometry}
\usetikzlibrary{tikzmark,decorations.pathreplacing}

\begin{document}
\begin{align*}
h(x)&=\ln \tikzmarknode{ln}{(x^{2}-8x)} && \\[6em]
h^{\prime}(x)&=\dfrac{\tikzmarknode[text=red]{num}{2x-8}}{\tikzmarknode[text=blue]{den}{x^{2}-8x}}
&& \\[2em]
\end{align*}
\begin{tikzpicture}[overlay,remember picture]
\draw[red,thick,decorate,decoration={brace,mirror}] (ln.south west) -- (ln.south east)
coordinate[midway,below=0.5em](aux1);
\draw[red,thick,decorate,decoration={brace,raise=1pt}] (num.north west) -- (num.north east)
coordinate[midway,above=0.5em](aux2);
\draw[red,thick,decorate,decoration={brace,mirror}] (den.south west) -- (den.south east)
coordinate[midway,below=0.5em](aux3);
\path (aux1) -- (aux2) coordinate[midway] (aux4);
\draw[-latex,red] (aux1) -- (aux4-|aux1) -| (aux2)
node[pos=0.5,left=0.25em,circle,black,draw,inner sep=1pt]{1};
\draw[latex-,red] (aux3) |- ++ (3em,-2em)
node[pos=0.5,left=0.25em,circle,black,draw,inner sep=1pt]{2} |-(aux4-|aux1)
node[pos=0.2,right=1em,circle,black,draw,inner sep=1pt,
label={[blue]right:Divide by the inside'' function.}] {2}
node[pos=0.4,right=1em,circle,black,draw,inner sep=1pt,
label={[red]right:Differentiate the inside'' function.}] {1};
\end{tikzpicture}
Hello
\end{document}


• +1 of course, but does it need 2 compilations? And how about the descriptions on the right side? – Only The Paranoid Survive Feb 10 '19 at 15:44
• @ArtificialStupidity Thanks! The TikZ overlay,remember picture trick generally requires two compilations. In the first compilation, the remembered coordinates get written to the aux file and in the second step they get used. That's why they are off in the first compilation. TeX doesn't tell you right away where it is going to put stuff (where meaning absolute page coordinates). – user121799 Feb 10 '19 at 15:49
• @ArtificialStupidity You also have to say draw when you say blue. E.g. \path[blue] does not produce a visible curve (or whatever), and \path[blue,draw] does. So color=blue sets the color. Likewise, decoration=.... just sets the decoration. And in order to use it, you need to say decorate. Or you can say \path[draw,postaction={decorate,decoration=decoration A}, postaction={decorate,decoration=decoration B}]...` to have the path drawn and decorated by two different decorations. – user121799 Feb 10 '19 at 16:15
• @Marmot, yes I really do appreciate your willingness to help! – MathScholar Feb 10 '19 at 16:18
• @MathScholar I think it is fair to say that all credits go to LoopSpace for providing us with the great library. – user121799 Feb 10 '19 at 16:20