# Overlapping under/over-brackets originating from centre of symbols

I am trying to accomplish this brace-like result with tikz, in the most versatile way possible.

Naively using \overbracket{} and \underbracket{} from mathtools with manual alignment hacks such as

$\,\,\overbracket{\!\!\nabla A + \nabla A\!\!}\,\,$


has the disadvantage of not being able to be overlapped.

The best solution I have so far is uses tikz, MWE shown below. The problem with this is that the tikzpictures (seem to) require the overlay option in order for inter-picture coordinates to work—however this also breaks the flow of the math. (Notice the alignment in the image below).

This example may be helpful: Similar thing for chemical equations. Thanks very much.

\documentclass{article}

\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{align*}
\mathcal{G} &= \nabla A + \nabla A
\\  \mathcal{G} &=
\begin{tikzpicture}[overlay, remember picture, baseline={(current bounding box.south)}]
\node[anchor=base, inner sep=0pt] (G1) at (0,0) {$\nabla$};
\end{tikzpicture}
A+{}\,
\begin{tikzpicture}[overlay, remember picture, baseline={(current bounding box.south)}]
\node[anchor=base, inner sep=0pt] (G2) at (0,0) {$\nabla$};
\draw[blue] (G2) -- +(0, .2) -- ($(G1) + (0, 0.2)$) -- (G1);
\end{tikzpicture}
A
\\  \mathcal{G} &= A + A
\end{align*}

\end{document}


You could also do that rather simply with the simplewick package.

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{simplewick}
\begin{document}
$\acontraction[.5ex]{\nabla_\mu A_\nu \nabla^\mu A^\nu - \nabla_\mu A_\nu \nabla^\nu A^\mu = }{\nabla}{A}{\nabla} \acontraction[.5ex]{\nabla_\mu A_\nu \nabla^\mu A^\nu - \nabla_\mu A_\nu \nabla^\nu A^\mu = \nabla A \nabla A -}{\nabla}{A\nabla}{A} \bcontraction[.5ex]{\nabla_\mu A_\nu \nabla^\mu A^\nu - \nabla_\mu A_\nu \nabla^\nu A^\mu = \nabla}{A}{\nabla}{A} \bcontraction[.5ex]{\nabla_\mu A_\nu \nabla^\mu A^\nu - \nabla_\mu A_\nu \nabla^\nu A^\mu = \nabla A \nabla A - \nabla}{A}{}{\nabla} \nabla_\mu A_\nu \nabla^\mu A^\nu - \nabla_\mu A_\nu \nabla^\nu A^\mu = \nabla A \nabla A - \nabla A \nabla A$
\end{document}


• This is good! There's also simpler-wick which doesn't require extraneous repetition: mirror.aut.ac.nz/CTAN/macros/latex/contrib/simpler-wick/… – Jollywatt Jan 18 at 4:28
• Interesting, thanks I didn't know that! However, with simpler-wick it does not seem to be possible to put the contractions below the equation, or to control the height of the contractions individually. – Vincent Jan 18 at 4:36
• One can certainly extend simpler-wick to have contractions above and below, and to control the height and all other aspects, see tex.stackexchange.com/q/451817/105570. – Schrödinger's cat Jan 18 at 7:05

You can use tikzmark. Usually this approach has the problem that the stuff is to be drawn using overlay, and may hence go through other parts of the equation. Here some vertical padding is computed and and inserted to avoid this.

\documentclass{article}

\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\newcommand{\Contract}[3][]{\begin{tikzpicture}[remember picture]
\tikzset{contract/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/contract/##1}}%
\begin{scope}[overlay]
\ifdim\pv{vsep}>0pt
\draw[style/.expanded=\pv{style}]
([yshift=\pv{gap}]#2.north) -- ++ (0,\pv{vsep}) -|
([yshift=\pv{gap}]#3.north);
\else
\draw[style/.expanded=\pv{style}]
([yshift=-\pv{gap}]#2.south) -- ++ (0,\pv{vsep}) -|
([yshift=-\pv{gap}]#3.south);
\fi
\end{scope}
\ifdim\pv{vsep}>0pt
\path let \p1=($(#2.north)-(#2.base)$),
\n1={\y1+\pv{gap}+\pv{vsep}-height("A")}
in node[baseline={(#2.base)}]{\vphantom{\raisebox{\n1}{.}}};
\else
\path let \p1=($(#2.base)-(#2.south)$),
\n1={\y1-\pv{gap}+\pv{vsep}+2pt}
in node[baseline={(#2.base)}]{\vphantom{\raisebox{\n1}{.}}};
\fi
\end{tikzpicture}%
}
\tikzset{contract/.cd,vsep/.initial=3pt,gap/.initial=1pt,style/.initial={-,thick}}
\begin{document}

\begin{align*}
\mathcal{G} &= \nabla A + \nabla A \\
\mathcal{G} &= \tikzmarknode{n1}{\nabla} A
+\tikzmarknode{n2}{\nabla} A    \Contract{n1}{n2} \\
\mathcal{G} &= A + A
\end{align*}
$\nabla_\mu A_\nu \nabla^\mu A^\nu- \nabla_\mu A_\nu \nabla^\nu A^\mu = \tikzmarknode{n3}{\nabla}\tikzmarknode{A3}{A} \tikzmarknode{n4}{\nabla}\tikzmarknode{A4}{A} + \tikzmarknode{n5}{\nabla}\tikzmarknode{A5}{A} \tikzmarknode{n6}{\nabla}\tikzmarknode{A6}{A} \Contract{n3}{n4} \Contract[vsep=-3pt]{A3}{A4} \Contract{n5}{A6} \Contract[vsep=-3pt]{n6}{A5}$

$\nabla_\mu A_\nu \nabla^\mu A^\nu- \nabla_\mu A_\nu \nabla^\nu A^\mu = \tikzmarknode{m3}{\nabla}\tikzmarknode{B3}{A} \tikzmarknode{m4}{\nabla}\tikzmarknode{B4}{A} + \tikzmarknode{m5}{\nabla}\tikzmarknode{B5}{A} \tikzmarknode{m6}{\nabla}\tikzmarknode{B6}{A} \Contract[style={blue,thick}]{m3}{m4} \Contract[vsep=-3pt,style={red,thick}]{B3}{B4} \Contract[style={red,thick}]{m5}{B6} \Contract[vsep=-3pt,style={blue,thick}]{m6}{B5}$

\end{document}