math-mode,notes,renewcommand – How to Create a Math Note Command in LaTeX

math-modenotesrenewcommand

I have made two new commands for making what the first one (\mathnote) does, but with some changes.

The new command (\mathnotee) is what I want, but is not working and I don't know why, I wante that my command (\mathnotee) could work in math mode and you could select where the arrow is pointing and what note appears with out defining what is before and what is after (as \mathnote does), because if I want to doit several times in one equation it makes it really hard. Thank you for your help.

\documentclass{book}

\usepackage{amsmath,amssymb}
\usepackage{nccmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{tcolorbox}
\tcbuselibrary{most}
\newtcolorbox[auto counter,
              number within=chapter,
              list inside=myexercise
              ]{myexercise}[1][]{%
    enhanced,
    title={{\begin{minipage}{\linewidth}\textbf{Ejercicio}~\thetcbcounter.~\textit{#1}\end{minipage}}},
    halign title=left,
    sharp corners,
    colback=white,
    coltitle=black,
    colbacktitle=white,
    boxrule=0pt,frame hidden,
    overlay unbroken={\draw[black,double] (interior.north west)--(segmentation.west);},
    boxed title style={%
      colframe=white, 
      boxrule=0pt,
      colback=white,
      left=0pt,
      right=0pt},
    attach boxed title to top left={xshift={-5pt}},
    underlay unbroken={\draw[help lines,step=3.8mm,black!20!white](interior.south west) grid (segmentation.east);},
    lower separated=false, 
    before lower = {\tcbsubtitle[colback=white, opacityback=0, colframe=black, opacityframe=0, boxrule=1pt, height=1cm,  width=2.55cm, valign=center]{\textbf{Solution:}}}
}

\newcommand{\mathnotee}[2]{
\;
\tikzmarknode{e1}{#1}
\;
\begin{tikzpicture}[overlay,remember picture]
\draw[stealth-,black,thick] (e1)+(90:.2)--+(90:.7) node[above=-2mm]{#2}; 
\end{tikzpicture}
}

\newcommand\mathnote[4]{
\[#1\;
\tikzmarknode{e1}{#2}
\;#3
\]
\begin{tikzpicture}[overlay,remember picture]
\draw[stealth-,black,thick] (e1)+(90:.2)--+(90:.7) node[above=-2mm]{#4}; 
\end{tikzpicture}
}

\usepackage{witharrows}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\mathnote{A(r)=\displaystyle\int_{V'}\dfrac{\mu_0}{4\pi}\dfrac{M(r')\times \hat{R} d\tau'}{R^2}}{=}{\dfrac{\mu_0}{4\pi}\int_{V'}M(r')\times\nabla'\left(\dfrac{1}{R}\right)d\tau'}{$\dfrac{\hat R}{R^2}=\nabla'\left(\dfrac{1}{R}\right)$}

$A(r)=\displaystyle\int_{V'}\dfrac{\mu_0}{4\pi}\dfrac{M(r')\times \hat{R} d\tau'}{R^2}\mathnotee{=}{\dfrac{\hat R}{R^2}=\nabla'\left(\dfrac{1}{R}\right)}\dfrac{\mu_0}{4\pi}\int_{V'}M(r')\times\nabla'\left(\dfrac{1}{R}\right)d\tau'$


\end{document}

The firs equation is what \mathnote does (but you need to be outside math mode, and define what is before and after the arrow), and the second one is what \mathnotee does (which isn't working).

enter image description here

Also here is what I mean when using it more than once in an equation:

enter image description here

This question is based in the previous question I made: Math notes between equations

Best Answer

I'm thinking that the way to do this is not to use the TikZ stuff at all, but rather to define a command \mathnote that takes two arguments: the first the relation to be annotated and the second the annotation. For the annotation, we can stack the down arrow and annotation above the relation.

\NewDocumentCommand{\mathnote}{ mm }
   {
     \overset % ❶
        {% The annotation goes above
          \hbox to 0pt{\hss % ❷
             $ % return to math mode
               \begin{array}{c} % ❸
                   \displaystyle #2\\ % ❹
                   \downarrow % ❺
               \end{array}
             $
          \hss}
        }
        {#1} % 
   }

The use of \overset ❶ assumes that we've loaded either amsmath or mathtools.

We enclose the annotation in a box of width 0pt with \hss before and after ❷ which allows us to center the annotation and have it take no width so it won't affect spacing below. This does mean that you're on your own if two annotations might overlap.

The annotation itself will be set in a single-column array ❸ so that we can easily stack it with the arrow. Other approaches are also possible, but since I'm doing this blind and not testing it, I went for the simplest route.

Note that \displaystyle ❹ does not take an argument and will be in effect for the whole of the cell.

The arrow I've put it in ❺ will be a bit thinner than what you specified. It wouldn't be too hard to put a TikZ arrow in if you so chose. Likewise, if you wanted color, applying \textcolor to the whole math inside the \hbox would accomplish that.