3

I'd like to know if it is possible to create a equation like the one shown in the figure below. And if it is possible, how can I do it?

Desired characteristics:

  1. Side comments aligned between equations

  2. Curved arrows pointing from one equation to another

Can someone help me?

enter image description here

2 Answers 2

8

Make sure that the following packages are loaded:

\usepackage{tikz}
\usetikzlibrary{tikzmark,arrows,calc}

and add the following definition to your preamble:

\newcommand\sidecomment[5][0.3,0.1]%
  {\begin{tikzpicture}[remember picture,overlay]
   \draw[-stealth',thick]
     ($({pic cs:#4}|-{pic cs:#2})+(#1)$)
     .. controls +(1,0) and +(1,0) ..
     node[right,align=left]{#5}
     ($({pic cs:#4}|-{pic cs:#3})+(#1)$);
   \end{tikzpicture}%
  }

Add marks at the end of the lines where you want the arrow to start and end using

\tikzmark{name of mark}

Then you can use the command

\sidecomment[offset]{<start mark>}{<end mark>}{<alignment mark>}{<text>}

to draw an arrow from start mark to end mark labeled with text. To align start and end of the arrow vertically, use the rightmost of these two marks additionally as alignment mark. offset is an optional argument that allows to shift start and end of the arrow by the same amount; it is a pair of numbers x,y that gives the x- and y-offset in cm. The default is 0.3,0.1, i.e., \sidecomment{a}{b}{c}{d} is the same as \sidecomment[0.3,0.1]{a}{b}{c}{d}, which means that the arrow starts and ends 3mm right of the mark and 1mm above the baseline.

Note that you have to run LaTeX twice to get the positions right.

The following document was typeset with the code below it.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark,arrows,calc}
\usepackage{amsmath}
\newcommand\sidecomment[5][0.3,0.1]%
  {\begin{tikzpicture}[remember picture,overlay]
   \draw[-stealth',thick]
     ($({pic cs:#4}|-{pic cs:#2})+(#1)$)
     .. controls +(1,0) and +(1,0) ..
     node[right,align=left]{#5}
     ($({pic cs:#4}|-{pic cs:#3})+(#1)$);
   \end{tikzpicture}%
  }
\begin{document}
\begin{align*}
\beta_1^*
&= \frac{\sum_{i=1}^n(x_i^*-\hat x^*)y_i^*}{\sum_{i=1}^n(x_i^*-\hat x^*)^2}\tikzmark{a}\\
&= \frac{\sum_{i=1}^n(w_2x_i-w_2\hat x)w_1y_i}{\sum_{i=1}^n(w_2x_i-w_2\hat x)^2}\tikzmark{b}
\end{align*}
\sidecomment{a}{b}{b}{since $y_i^*=w_1y_i$\\and $x_i*=w_2x_i$}
\end{document}
3

You can use the pacakge witharrows which has been specially designed for that purpose.

\documentclass{article}
\usepackage{witharrows}
\begin{document}
\begin{DispWithArrows*}
\beta_1^*
&= \frac{\sum_{i=1}^n(x_i^*-\hat x^*)y_i^*}{\sum_{i=1}^n(x_i^*-\hat x^*)^2}
\Arrow{since $y_i^*=w_1y_i$\\and $x_i^*=w_2x_i$} \\
&= \frac{\sum_{i=1}^n(w_2x_i-w_2\hat x)w_1y_i}{\sum_{i=1}^n(w_2x_i-w_2\hat x)^2}
\end{DispWithArrows*}
\end{document}

The result of the code

2
  • Very nice. For my humble opinion it is better since and and in text mode.
    – Sebastiano
    Jul 15, 2019 at 8:23
  • In fact, in witharrows by default, the text associated to an arrow is italicized. This behaviour of witharrows is for historical reasons. I will change that in a future release, even if, strictly speaking, that change will break compatibility with previous versions. Jul 15, 2019 at 9:25

You must log in to answer this question.

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