3

I am writing a basic mathematics book and I would like to annotate some calculations, using arrows, underlines, horizontal, vertical and diagonal lines, boxes, circles, etc., with different colors, to provide a visual aid to the reader. Here is an example (please disregard the wrong explanations and visual clutter):

Example of annotated calculation Is there any specific package for this task? (A few weeks ago, I came across a site that explained something along those lines. I don't remember exactly, but it looks like they used the "amsmath" package for that functionality.)

2
  • 3
    I'm not sure if this is a good idea... That said, you should be able to do this using the tikzmark package.
    – projetmbc
    Commented Dec 30, 2022 at 20:59
  • Welcome to TeX.SE. Searching for tikzmark cancel should give you some ideas on how to get started. Basic idea is to use \tikzmark (which is available in the {tikzmark} package) to make specific point in the text, then after the text is written (include the any necessary space for the drawing), to overlay the drawing. I suggest you start with one of the examples and see how far you get on one of those specific drawing issues. If you get stuck and have a specific question about a specific issue, please post a question. Commented Dec 30, 2022 at 22:25

2 Answers 2

8

I suggest using tikzmark. The command is \tikzmarknode{<label>}{<content>}. You can use several of these in your equation with different labels. Then use the options remember picture, overlay in a tikzpicture and use TikZ commands to draw the various annotations.

enter image description here

The code uses a variety of libraries other than tikzmark: shapes.geometric is used for the ellipse, shapes.arrows is used for the "result" arrow, fit is used to contain the contents of the red ellipse, blue rectangle and yellow circle, bending is used to improve the arrowhead of the blue arrow.

Remember, when using tikzmark you must compile twice after any changes to the node positions.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{tikzmark, shapes.geometric, shapes.arrows, fit, bending}

\begin{document}

\[
\tikzmarknode{a1}{\frac{4}{2}}+\tikzmarknode{a2}{\frac{6}{3}}=
\frac{\tikzmarknode{b1}{(3\times 4)}+\tikzmarknode{b2}{(2\times 6)}}{\tikzmarknode{b3}{2\times 3}}=\tikzmarknode{c1}{\frac{12+12}{6}}=\tikzmarknode{c2}{\frac{24}{6}}=\tikzmarknode{d1}{4}
\]

\begin{tikzpicture}[remember picture, overlay, font=\sffamily]
\draw[red, thick, ->, shorten <=1em, , shorten >=1em](a2.south east)--node[below, pos=.45, scale=.4]{1}(a1.north west);
\node[draw=red, thick, ellipse, fit=(b1), inner ysep=0, inner xsep=-1mm](ell){};
\draw[thick, red] (ell)--++(0,.5)node[above, red, scale=.4]{This is important!};
\draw[red, thick] (b2.south west)--node[above=2mm, red, scale=.5]{\bfseries WRONG!}(b2.north east);
\draw[green!70!black, thick] (b3.east)--node[below]{$\uparrow$}node[below=5mm, align=left, green!70!black, scale=.5]{This multiplication is not\\necessary Try to simplify.}(b3.west) ([yshift=2pt]b3.east)--([yshift=2pt]b3.west);
\node[draw=blue, thick, fit=(c1), inner xsep=1pt](box){};
\draw[blue, thick, ->, shorten >=3pt] (box) to[out=90, in=90] (c2.north);
\node[draw=yellow!70!orange, thick, circle, fit=(d1), inner sep=1pt](cir){};
\node[draw=yellow!70!orange, thick, text=yellow!70!orange, single arrow, shape border rotate=180, anchor=west, scale=.4, single arrow tip angle=40, minimum height=30mm] at (cir.east){\quad RESULT};
\end{tikzpicture}

\end{document}
3

A simpler (but not as powerful as tikzmark) package would be the annotate-equations package.

Here a short example taken from the package documentation:

\documentclass{article}

\usepackage{mathtools}
\usepackage{annotate-equations}


\begin{document}

\renewcommand{\eqnhighlightheight}{\vphantom{\hat{H}}\mathstrut} 
\begin{equation*}
i \tikzmarknode{hbar}{\mathstrut\hbar} \frac{\partial}{\partial t} \eqnmarkbox[blue]{Psi1}{\Psi(x, t)} = \eqnmark[red]{Hhat}{\hat{H}} \eqnmarkbox[blue]{Psi2}{\Psi(x, t)}
\end{equation*}
\annotate[yshift=3em]{above}{hbar}{$\hbar = \frac{h}{2\pi}$, reduced Planck constant} 
\annotate[yshift=1em]{above}{Hhat}{Hamilton operator} 
\annotatetwo[yshift=-1em]{below}{Psi1}{Psi2}{Wave function}

\end{document}

annotate-equations package

5
  • @samcarter_is_at_topanswers.xyz - Thank you for providing the annotate-equations code. Commented Jan 7, 2023 at 1:31
  • You're welcome! Commented Jan 7, 2023 at 10:17
  • @samcarter_is_at_topanswers.xyz - Your code is such a concise solution ... something I should have at least tried to do. Distracted (and overwhelmed) with eTOC, I skipped giving a proper answer. Thank you again. Commented Jan 8, 2023 at 2:47
  • Not my code :) As said in the edit the code is "taken from the package documentation" :) I thought it could help the question because it already had gathered two delete votes in the review queue. Commented Jan 8, 2023 at 9:40
  • @samcarter_is_at_topanswers.xyz - Thank you! Commented Jan 14, 2023 at 10:16

You must log in to answer this question.

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