# math-mode: How to underline with borders?

Is there a simple way to get an underline style like this?

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\underline{{ABC}$
\end{document}

For simple applications you could just add rules.

\documentclass{article}
\usepackage{amsmath}
\newcommand{\UnderlineWithBorders}[1]{\setbox0\hbox{#1}%
\rule[\the\dimexpr-0.37ex-\dp0]{0.1ex}{0.3ex}\underline{#1}\rule[\the\dimexpr-0.37ex-\dp0]{0.1ex}{0.3ex}}
\begin{document}
\UnderlineWithBorders{ABC} \UnderlineWithBorders{ijk}
\end{document}

For a more general version, you could start writing very complicated macros, or just use TikZ with the tikzmark library.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\newcounter{underlinedtikzmarknode}
\newcommand{\UnderlineWithBorders}[2][]{\stepcounter{underlinedtikzmarknode}%
\tikzmarknode[inner xsep=0.2ex,inner ysep=0.4ex,path picture={
\draw([yshift=0.4ex,xshift=\pgflinewidth]path picture bounding box.south west)
|- ([yshift=\pgflinewidth,xshift=-\pgflinewidth]path picture bounding box.south east)
-- ([yshift=0.4ex,xshift=-\pgflinewidth]path picture bounding box.south east);
}]{underlinedtikzmarknode-\number\value{underlinedtikzmarknode}}{#2}}
\begin{document}
\UnderlineWithBorders{ABC} \UnderlineWithBorders{ijk}
$\UnderlineWithBorders{A}=\UnderlineWithBorders{\int\limits_0^1f(x)\,\mathrm{d}x}$
\end{document}

• Ah, very good. Could I possibly create something with \lgroup, \rgroup? – haver Nov 4 '19 at 16:30
• @haver There are dedicated packages for Wick contractions, if that's what you are looking for. If not you could ask a new question, which is free of charge. – Schrödinger's cat Nov 4 '19 at 16:38