[Tex/LaTex] Add Annotations and Remarks next to Equations

amsmathannotationsequationsmath-mode

for a thesis I am currently writing on, I would like to highlight and explain some mathematical transformations inside the equations (to explain why the transformation holds). Of course, I could "interrupt" the mathematical proof and explain the transformation in a new line, but I think it is nicer if the annotations are written directly next to the lines they are related to. I already thought about creating a table or two minipages, but I wasn't sure if there is a nicer way to do it.

Attached you will find a simple example (the actual equation is more complicated), just in order to get an idea what I am thinking of. The arrows are not mandatory, but it would be nice if the annotation is written between the mathematical lines (but the spacing of the equation should not change because of the annotation). Thank you for your help!

enter image description here

\documentclass{scrartcl}
\usepackage{amsthm} 
\usepackage{amsmath}
\usepackage{amssymb} 
\usepackage{mathtools}

\begin{document}

\begin{equation*}
\begin{split}
\textrm{OPT} & = \textrm{OPTL} + \textrm{OPTR} \\
    & = (x+y) + (y+z) \\
    & \leq 2* (y+z) \\
    & = \textrm{OPTR} + \textrm{OPTR}  
\end{split}
\end{equation*}

\end{document}

Best Answer

The witharrows package can do something like this.

\documentclass{scrartcl}
\usepackage{amsthm} 
\usepackage{amssymb} 
\usepackage{mathtools}
\usepackage{witharrows}

\WithArrowsOptions{tikz={font={\normalfont\small}}}

\begin{document}

\begin{equation*}
\begin{WithArrows}
\mathrm{OPT} & = \mathrm{OPTL} + \mathrm{OPTR}\Arrow{Definition of $\mathrm{OPT}$ and $\mathrm{OPTR}$} \\
    & = (x+y) + (y+z)\Arrow{As $x \leq z$} \\
    & \leq 2* (y+z)\Arrow{Definition of $\mathrm{OPTR}$} \\
    & = \mathrm{OPTR} + \mathrm{OPTR}  
\end{WithArrows}
\end{equation*}

\end{document}

enter image description here


There is a semi-automatic way to get line breaks in the text as well, you will need to specify a text width, then the label will be broken to fit into the horizontal space.

\mathrm{OPT} & = \mathrm{OPTL} + \mathrm{OPTR}
\Arrow[tikz={text width=5.3cm}]{Definition of $\mathrm{OPT}$ and $\mathrm{OPTR}$,
  furthermore we use the well-known Unicorn theorem} \\

This solution is semi-automatic since you have to find a good value for text-width yourself meaning that for occasional use it is probably not faster than giving manual breaks with \\.

As soon as the text occupies multiple lines you run the risk of running into the labels above or below.


Version 1.9 of witharrows define a replacement environments for align in which labels can be wrapped automatically. Thanks to F. Pantigny for the hint in the comments.

\begin{DispWithArrows*}[wrap-lines]
\mathrm{OPT} & = \mathrm{OPTL} + \mathrm{OPTR}\Arrow{Definition of $\mathrm{OPT}$ and $\mathrm{OPTR}$,
  and the Unicorn theorem} \\
    & = (x+y) + (y+z)\Arrow{As $x \leq z$} \\
    & \leq 2* (y+z)\Arrow{Definition of $\mathrm{OPTR}$} \\
    & = \mathrm{OPTR} + \mathrm{OPTR}  
\end{DispWithArrows*}
Related Question