I think I found something more elegant, but I may not know enough TeX to realize what mistakes I am doing.
Here is a minimal (showcasing a few possible usecases) working example:
\documentclass{tufte-book} %For some reason, article does not work
\usepackage[xcolor,hyperref,notion,quotation]{knowledge} %Knowledge package for my use, but you can probably add color and hyperlinks to the rule in other ways
\hypersetup{pdfborder=1 1 1,urlbordercolor=red} %To highlight the link box
\knowledge{notion} %(knowledge package) Add a new notion to link
| closed
%%%%%%% Meat of the solution %%%%%%%%%
%I create variables for storing a box (some fixed piece of math), its height and its width
\newsavebox{\closebox}
\newlength{\closeheight}
\newlength{\closewidth}
\newcommand{\closure}[1]{ %New command with 1 argument
\savebox{\closebox}{$ #1 $} %Save the content of the argument inside the box
\settoheight{\closeheight}{\usebox{\closebox}} %Retrieve the height of the box
\settowidth{\closewidth}{\usebox{\closebox}} %Retrieve the width of the box
\ooalign{ %Some environment that lets me put two things at the same position.
\raisebox{\closeheight+0.1em} %Raise just above the box
{\kl[closed]{\rule{\closewidth}{0.5pt}}% Draw a rule with a knowledge (or other) link
}\cr%Reset position
\usebox{\closebox}%Put the content of the box.
}
}
%Minimal example with exponents and zsubscripts to make sure it works.
\begin{document}
This is a ""closed"" document.
$\closure{2^3+{X{d_e}}5}$
[\sum_{x \in \closure{(A\cup B)}} f_{\closure{x}}]
\hypersetup{pdfborder=0 0 0,urlbordercolor=red} %Stop highlighting link box
Here is the same thing without link boxes.
$\closure{2^3+{X{d_e}}5}$
[\sum_{x \in \closure{(A\cup B)}} f_{\closure{x}}]
\end{document}
And its output:
