8

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}

Underbrace problems


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}}
$
8

I would use a combination of \mathrlap and \phantom to get the positioning just right. For \mathrlap you would need the mathtools 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.

\documentclass{scrreprt}

\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath,mathtools}
\usepackage{beramono}
\begin{document}

\texttt{https://example.org/foo/id/123456789/bar/baz}

$
\mathrlap{\underbrace{\phantom{\texttt{https://example.org/foo\strut}}}_{\text{Base-URL}}}\texttt{https://example.org/foo}%
\texttt{/id/}%
\mathrlap{\underbrace{\phantom{\texttt{123456789\strut}}}_{\text{Foo-ID}}}\texttt{123456789}%
\texttt{/}%
\mathrlap{\underbrace{\phantom{\texttt{bar/baz\strut}}}_{\text{Function}}}\texttt{bar/baz}%
$
\end{document}​

Using \mathrlap for correct horizontal alignment

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:

...
\usepackage{amsmath,mathtools,url}
\newcommand{\ubspace}[2]{% Correct \underbrace spacing
  \makebox[\widthof{\texttt{#1\strut}}][c]{\ensuremath{\underbrace{\phantom{\texttt{#1}\strut}}_{\text{#2}}}}
}
...

\begin{document}

\texttt{https://example.org/foo/id/123456789/bar/baz}

$
\mathrlap{%
  \ubspace{https://example.org/foo}{Base-URL}%
  \text{\hphantom{\texttt{/id/}}}%
  \ubspace{123456789}{Foo-ID}%
  \text{\hphantom{\texttt{/}}}%
  \ubspace{bar/baz}{Function}}%
\text{\url{https://example.org/foo/id/123456789/bar/baz}}% Correct clickable URL
$
...
  • Many thanks for this detailed answer! Works great. And with \strut the code looks cleaner, too. – hiddenbit Aug 4 '11 at 19:54
3

There's no need to box and reset the text, you just need to put {} around the construct to stop \mathop spacing.

enter image description here

\documentclass{scrreprt}

\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\def\xunderbrace#1_#2{{\underbrace{#1}_{#2}}}

\usepackage{amsmath}

\usepackage{beramono}

\begin{document}

\texttt{https://example.org/foo/id/123456789/bar/baz}

$
\xunderbrace{\texttt{https://example.org/foo}}_{\text{Base-URL}}%
\texttt{/id/}%
\xunderbrace{\texttt{123456789\vphantom{g}}}_{\text{Foo-ID}}%
\texttt{/}%
\xunderbrace{\texttt{bar/baz\vphantom{g}}}_{\text{Function}}
$
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.