A small addition to Gonzalo's answer.
Just to cut down on the typing and making the whole thing scale with the font size of the document, I figured it would be nice to incorporate the answer to Get current font size as length and make every size scale accordingly:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\newlength{\mytextsize}
\makeatletter
\setlength{\mytextsize}{\f@size pt}
\makeatother
\newcommand{\JoinUp}[5]{\begin{tikzpicture}[remember picture,overlay,line width=0.05\mytextsize]
\draw([shift={(#1\mytextsize,#2\mytextsize)}]pic cs:start#5) -- ++(0pt,0.7\mytextsize) -| ([shift={(#3\mytextsize,#4\mytextsize)}]pic cs:end#5);
\end{tikzpicture}}
\newcommand{\JoinDown}[5]{\begin{tikzpicture}[remember picture,overlay,line width=0.05\mytextsize]
\draw([shift={(#1\mytextsize,#2\mytextsize)}]pic cs:start#5) -- ++(0pt,-0.7\mytextsize) -| ([shift={(#3\mytextsize,#4\mytextsize)}]pic cs:end#5);
\end{tikzpicture}}
\begin{document}
\[(a+b)(a+b)-(a+b)(a-b)=\tikzmark{starta}aa+\tikzmark{startc}ab+\tikzmark{endc}ba+\tikzmark{startd}bb-(\tikzmark{enda}aa-\tikzmark{startb}ab+\tikzmark{endb}ba-\tikzmark{endd}bb)=2ab+2bb.\]
\JoinUp{0.5}{1}{0.5}{1}{a}
\JoinUp{0.5}{1}{0.5}{1}{b}
\JoinDown{0.5}{-0.4}{0.5}{-0.4}{c}
\JoinDown{0.5}{-0.4}{0.5}{-0.4}{d}
\end{document}

This changes the \JoinUp and \JoinDown commands a little, as the displacements are now given in terms of the font size.
Note: this does not scale with the local font size, as the \mytextsize length is defined in the preamble.