如何删除使用时添加的额外空间\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}
从视觉/排版角度来看,这很好,但嵌入的 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}