4

I want to explain what the different signs in an expression means like this:enter image description here

Currently I do this with TikZ (see MWE at the bottom), making nodes of everyting in the expression I want to point an arrow to. It is time consuming to finetune the position of the nodes so the result looks like what you would get if you just write the expression the ordinary way.

Is there a more elegant way to do this?

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}

\begin{document}

\begin{tikzpicture}
  [ plass/.style={inner sep=0pt,minimum size=4mm} ]
  \node[plass] (minusfortegn)                                    {$-$};
  \node[plass] (tre)            [right=-1.8mm of minusfortegn  ] {$3$};
  \node[plass] (minusoperator)  [right=-1.0mm of tre           ] {$-$};
  \node[plass] (to)             [right=-1.0mm of minusoperator ] {$2$};
  \node[plass] (lik)            [right=-1.6mm of to            ] {\phantom{(}=\phantom{)}};
  \node[plass] (minusfortegnto) [right=-1.4mm of lik           ] {$-$};
  \node[plass] (fem)            [right=-1.8mm of minusfortegnto] {$5$};

  \node (minusfortegntekst)  [above=4mm of to] {Minustegn som fortegn};
  \node (minusoperatortekst) [below=4mm of to] {Minustegn for subtraksjon};

  \draw [->] (minusfortegntekst)  to [out=230,in= 90] (minusfortegn);
  \draw [->] (minusfortegntekst)  to [out=310,in= 90] (minusfortegnto);
  \draw [->] (minusoperatortekst) to [out=110,in=270] (minusoperator);
\end{tikzpicture}

\end{document}

Edit: I looked at the hf-tikz package, but it doesn't seem to be so elegant in this situation. I really like the subnode method in the tikzmark library though. @manuel shows a good way to do that in a comment below.

I tweaked the solution from @Ignasi a bit and wound up with the code below, which works well for me. Thanks a lot to @Ignasi, @Harish and @manuel for valuable feedback!

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning, tikzmark}

\begin{document}

\begin{tikzpicture}[remember picture]
  \node { $\subnode{fortegn1}{$-$} 3 \mathbin{\subnode{minusoperator}{$-$}} \subnode{to}{$2$} = \subnode{fortegn2}{$-$} 5$ };
  \node[above=4mm of to] (minusfortegntekst)  {Minustegn som fortegn};
  \node[below=4mm of to] (minusoperatortekst)  {Minustegn for subtraksjon};
  \draw [->] (minusfortegntekst)  to [out=230,in= 90] (fortegn1);
  \draw [->] (minusfortegntekst)  to [out=310,in= 90] (fortegn2);
  \draw [->] (minusoperatortekst) to [out=110,in=270] (minusoperator);
\end{tikzpicture}

\end{document}
Jostein Trondal
  • 1,165
  • 9
  • 20

1 Answers1

6

You can declare as nodes only those parts of the expression you want to point. Although I had to adjust spaces around elements to more or less look like the original expression.

Another option could be to use a package like hf-tikz

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usetikzlibrary{positioning}

\begin{document}

\[\tikz[remember picture]{\node[inner sep=0pt](m1){$-\,$ };}3
\tikz[remember picture]{\node[inner sep=0pt](m2){$\,-\,$};}2=
\tikz[remember picture]{\node[inner sep=0pt](m3){$-\,$};}5\]

\begin{tikzpicture}[remember picture, overlay]
\node[above= 5mm of m2] (M1)  {Minustegn som fortegn};
\node[below= 5mm of m2] (M2)  {Minustegn for subtraksjon};
\draw [->] (M1)  to [out=230,in= 90] (m1);
\draw [->] (M1)  to [out=310,in= 90] (m3);
\draw [->] (M2) to [out=110,in=270] (m2);
\end{tikzpicture}
\end{document}

enter image description here

EDIT: As Harish suggested, this code could be simplified using \tikzmark in its original form

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

or using a subnode from its posterior development in tikzmarklibrary like in next code (which has been corrected with Manuel's suggestion to make the marked expression looks like a regular math expression).

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning, tikzmark}

\begin{document}

\[
\begin{tikzpicture}[remember picture]
\node {$\subnode{m1}{$-$} 3 \mathbin{\subnode{m2}{$-$}} 2 = \subnode{m3}{$-$} 5$};
\end{tikzpicture}
\]

\begin{tikzpicture}[remember picture, overlay]
\node[above= 5mm of m2] (M1)  {Minustegn som fortegn};
\node[below= 5mm of m2] (M2)  {Minustegn for subtraksjon};
\draw [->] (M1)  to [out=230,in= 90] (m1);
\draw [->] (M1)  to [out=310,in= 90] (m3);
\draw [->] (M2) to [out=110,in=270] (m2);
\end{tikzpicture}

\end{document}
Ignasi
  • 136,588