Markings in latex equations

equations

I want to write tensorial equations in latex indicating the equivalent indices as shown below:

enter image description here

enter image description here

Here I have used MS Powerpoint to make these images. Although it works, using this means I cannot use equation numbering in latex and relevant tags. And editing these becomes a pain.

Things I need –

  1. Lines to indicate the number of indices
  2. \underbracket type functionality joining the individual index lines and should allow for overlapping

Is there a more elegant solution, preferably using only latex?

Best Answer

Welcome to TeX.SE!! I think this is a possible solution, but I'm not very sure about you point 2. I don't know how the lines must 'indicate the number of indices'.

However I hope this helps. I made two pics. I call the first vlines which draw those vertical lines that must 'indicate the number of indices'. This pic takes a parameter: the number of lines to draw. If it's not present draws exactly 2 lines. The second pic, underbracket draw a line connecting two nodes (created in the equations with \tikzmarknode). This pic needs four parameters: end point, x shift in the initial point, x shift in the final point and y shift.

Something like this:

\documentclass {article}
\usepackage    {lipsum}
\usepackage    {tikz}
\usetikzlibrary{calc}
\usetikzlibrary{tikzmark}

\tikzset
{
  pics/vlines/.default=2,
  pics/vlines/.style={% #1 -> number of lines (default 2)
    code={%
    \foreach\i in {1,...,#1}
      \draw[pic actions] (-0.05*#1+0.1*\i-0.05,-0.25) --++ (0,-0.25);
    }},
  pics/underbracket/.style n args={4}{% end point, initial delta x, final delta x, delta y
    code={%
      \draw (#2,-0.6) --++ (0,-#4) -| ($(#1)+(#3,-0.6)$);
    }},
}


\begin{document}
\lipsum[1] See equation \ref{eq:1}.

\begin{equation}\color{blue}
\tikzmarknode{F1}F_*^T \cdot 
\tikzmarknode{E1}E \cdot 
\tikzmarknode{F2}F_*=
\tikzmarknode{F3}F_*^T \stackrel{2}{*} 
\tikzmarknode{F4}F_*^T \cdot
\tikzmarknode{E2}E,\label{eq:1}
\end{equation}

\begin{tikzpicture}[remember picture,overlay]
\foreach\i in {F1,E1,F2,F3,F4,E2}
  \pic at (\i) {vlines};
\pic at (F1) {underbracket={E1}{ 0}    {-0.025}{0.5}};
\pic at (E1) {underbracket={F2}{ 0.025}{0}{0.5}};
\pic at (F3) {underbracket={E2}{ 0}    { 0.025}{0.75}};
\pic at (F4) {underbracket={E2}{ 0.025}{-0.025}{0.5}};
\end{tikzpicture}
\vspace*{1cm}% space for the overlay picture

\lipsum[2] See now equation \ref{eq:2}.

\begin{equation}
(\tikzmarknode{A}A   \stackrel{4}{*} 
 \tikzmarknode{C}C   \stackrel{2}{*} 
 \tikzmarknode{E}E)  \stackrel{3}{*}
 \tikzmarknode{B}B = \cdots\label{eq:2}
\end{equation}

\begin{tikzpicture}[remember picture,overlay]
\foreach\i in {A,E,B}
  \pic at (\i) {vlines};
\pic[red] at (C) {vlines={4}}; % four (red) lines (default 2)
\pic at (A) {underbracket={C}{0   }{-0.05}{0.25}};
\pic at (C) {underbracket={E}{0.05}{ 0   }{0.5}};
\pic at (C) {underbracket={B}{0   }{ 0   }{0.75}};
\end{tikzpicture}
\vspace*{1cm}% space for the overlay picture

\lipsum[3]
\end{document}

enter image description here

Edit: I changed the second pic for a style inspired in Andrew Stacey's answer in this post. It does the same but I think that the syntax is a little bit clearer.

And the new code:

\documentclass {article}
\usepackage    {lipsum}
\usepackage    {tikz}
\usetikzlibrary{calc}
\usetikzlibrary{tikzmark}

\tikzset
{
  pics/vlines/.default=2,
  pics/vlines/.style={% #1 -> number of lines (default 2)
    code={%
    \foreach\i in {1,...,#1}
      \draw[pic actions] (-0.05*#1+0.1*\i-0.05,-0.25) --++ (0,-0.25);
    }},
  % idea taken from Andrew Stacey's answer: https://tex.stackexchange.com/questions/609102
  underb/.style n args={3}{% initial delta x, final delta x, delta y
    to path={++(#1,-0.6) --++ (0,-#3) -| ($(\tikztotarget)+(#2,-0.6)$)}
  }
}


\begin{document}
\lipsum[1] See equation \ref{eq:1}.

\begin{equation}\color{blue}
\tikzmarknode{F1}F_*^T \cdot 
\tikzmarknode{E1}E \cdot 
\tikzmarknode{F2}F_*=
\tikzmarknode{F3}F_*^T \stackrel{2}{*} 
\tikzmarknode{F4}F_*^T \cdot
\tikzmarknode{E2}E,\label{eq:1}
\end{equation}

\begin{tikzpicture}[remember picture,overlay]
\foreach\i in {F1,E1,F2,F3,F4,E2}
  \pic at (\i) {vlines};
\draw (F1) to[underb={0    }{-0.025}{0.5}]  (E1);
\draw (E1) to[underb={0.025}{ 0    }{0.5}]  (F2);
\draw (F3) to[underb={0    }{ 0.025}{0.75}] (E2);
\draw (F4) to[underb={0.025}{-0.025}{0.5}]  (E2);
\end{tikzpicture}
\vspace*{1cm}% space for the overlay picture

\lipsum[2] See now equation \ref{eq:2}.

\begin{equation}
(\tikzmarknode{A}A   \stackrel{4}{*} 
 \tikzmarknode{C}C   \stackrel{2}{*} 
 \tikzmarknode{E}E)  \stackrel{3}{*}
 \tikzmarknode{B}B = \cdots\label{eq:2}
\end{equation}

\begin{tikzpicture}[remember picture,overlay]
\foreach\i in {A,E,B}
  \pic at (\i) {vlines};
\pic[red] at (C) {vlines={4}}; % four (red) lines (default 2)
\draw (A) to[underb={0   }{-0.05}{0.25}] (C);
\draw (C) to[underb={0.05}{ 0   }{0.5}]  (E);
\draw (C) to[underb={0   }{ 0   }{0.75}] (B);
\end{tikzpicture}
\vspace*{1cm}% space for the overlay picture

\lipsum[3]
\end{document}
Related Question