[Tex/LaTex] Overlapping over/underbrace

formattingmath-mode

I wanted to do something like this: How to have overlapping under-braces and over-braces

The example given is really great, so I tried it on my own: what I was hoping to achieve is to represent a contribution from four fields to Wick theorem by clipping pair of fields, so I tried this:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}
\begin{equation}
    \begin{aligned}
        \overbracket{\phi (x_1) \quad \phi (x_2)} \quad \overbracket{\phi (x_3) \quad \phi (x_4)} \\
        \rlap{$\overbracket{\phantom{\phi (x_1) \quad \phi(x_2) \quad \phi (x_3)}}$} \phi(x_1) \quad
        \underbracket{\phi (x_2) \quad \phi (x_3) \quad \phi (x_4)} \\
        \overbracket{\phi (x_1) \quad \underbracket{\phi (x_2) \quad \phi (x_3)} \quad \phi (x_5)}
    \end{aligned}
\end{equation}

\end{document}

enter image description here

Almost, but not quite! The under/overbrackets are starting and ending on the wrong position, I would rather stress the point (x_1, …), where the bracket starts or ends. So I tried modifying it like this:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}

\begin{equation}
    \begin{aligned}
        \phi (x\!\!\overbracket{\,_{1}) \quad \phi (x\!}{\,}_{\!2}) \quad \phi (x\!\overbracket{_3) \quad \phi (x\!}{\,}_{\!4}) \\
        \rlap{$\phi (x\!\!\overbracket{\phantom{\,_1) \quad \phi(x_2) \quad \phi (x}}_3)$} \phi(x_1) \quad
        \phi (x\!\!\underbracket{\,_2) \quad \phi (x_3) \quad \phi (x\!}{\,}_{\!4}) \\
        \phi (x\!\!\overbracket{\,_1) \quad \phi (x\!\!\underbracket{\,_2) \quad \phi (x\!}{\,}_{\!3}) \quad \phi (x\!}{\,}_{\!4})
    \end{aligned}
\end{equation}

\end{document}

enter image description here

This is, in fact, very ugly code, but it works for the first and last case – because there are no interlapping clips. What is worse than ugly code, is that this is not working for the middle case (interlapping clip x1 - x3 and x2 - x4), when number 3 is positioned below the first overbracket (right under x_2, where the second clip starts), the right parenthesis is duplicated and also, if you watch carefully, the \phi (x is somewhat bold, so it's duplicated and slightly displaced too. How can I achieve the interlapping clips effect with exact positioning over x's without these issues?

Thank you.

P.S.: I'm using pdflatex in TeXworks

Best Answer

It's simpler to do it with pst-node: consider variables you want to link as \rnodes, and connect them with the \ncbar command:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{pst-node, auto-pst-pdf}

\begin{document}

\begin{postscript}
\begin{equation}
    \begin{gathered}
        \phi (\rnode{n1}{x_1}) \quad \phi (\rnode{n2}{x_2}) \quad\phi (\rnode{n3}{x_3}) \quad \phi (\rnode{n4}{x_4}) \\[1ex]
        \phi (\rnode{p1}{x_1}) \quad \phi(\rnode{p2}{x_2}) \quad \phi (\rnode{p3}{x_3}) \quad\phi(\rnode{p4}{x_4}) \\[2ex]
       \phi (\rnode{q1}{x_1}) \quad \phi (\rnode{q2}{x_2}) \quad \phi (\rnode{q3}{x_}3) \quad \phi (\rnode{q4}{x_4})
    \end{gathered}
\end{equation}
\psset{nodesep=3pt, arm=5pt, angle=90, linejoin=1}
\ncbar{n1}{n2}\ncbar{n3}{n4}
\ncbar{p1}{p3}\ncbar[nodesep=2pt, angle=-90]{p2}{p4}
\ncbar{q1}{q4}\ncbar[nodesep=2pt, angle=-90]{q2}{q3}
\end{postscript}

\end{document} 

enter image description here