删除 \underbrace{…} 周围的多余空间

删除 \underbrace{…} 周围的多余空间

如何删除使用时添加的额外空间\underbrace{...},以使两行获得相同的宽度:

\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}

下支架问题


编辑:

使用 Werners 答案的方法,我定义了一个命令,因为我需要这个特殊的无间距下支撑几次:

...
\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}}
$

答案1

\mathrlap我会使用和的组合\phantom来获得正确的定位。因为\mathrlap你需要mathtools包裹\vphantom{g}我没有使用 来使 URL 的其他部分垂直对齐正确,而是使用了\strut,尽管这可能只是偏好上的差异。

\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}​

使用 \mathrlap 进行正确的水平对齐

从视觉/排版角度来看,这很好,但嵌入的 URL 无法正确扩展,因为它被分成了几个部分(为了留出空间)。如果您想保留可点击的链接(使用url包裹,例如),你需要做更多的腿部工作。这里有一种方法:

...
\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
$
...

答案2

没有必要将文本装箱并重新设置,您只需要将{}其构造起来以停止\mathop间距。

在此处输入图片描述

\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}

相关内容