How do I remove the additional space that is added when using \underbrace{...}
, to get the same width in both lines:
\documentclass{scrreprt}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{beramono}
\begin{document}
\texttt{https://example.org/foo/id/123456789/bar/baz}
$
\underbrace{\texttt{https://example.org/foo}}_{\text{Base-URL}}%
\texttt{/id/}%
\underbrace{\texttt{123456789\vphantom{g}}}_{\text{Foo-ID}}%
\texttt{/}%
\underbrace{\texttt{bar/baz\vphantom{g}}}_{\text{Function}}
$
\end{document}
Edit:
Using the approach of Werners answer, I defined a command, because I need this special underbrace without spacing a several times:
...
\newcommand{\underbracewithoutspace}[2]{\mathrlap{\underbrace{\phantom{#1\strut}}_{#2}}#1}
$
\underbracewithoutspace{\texttt{https://example.org/foo}}{\text{Base-URL}}%
\texttt{/id/}%
\underbracewithoutspace{\texttt{123456789}}{\text{Foo-ID}}%
\texttt{/}%
\underbracewithoutspace{\texttt{bar/baz}}{\text{Function}}
$
Best Answer
I would use a combination of
\mathrlap
and\phantom
to get the positioning just right. For\mathrlap
you would need themathtools
package. Instead of using\vphantom{g}
to get the vertical alignment correct with the other components of your URL, I used\strut
, although this may be just a difference in preference.This works fine from a visual/typesetting perspective, but the embedded URL is not properly expanded since it is broken into pieces (for spacing purposes). If you want to maintain a clickable link (using the
url
package, for example), you would have to do a little more leg work. Here's one approach: