如何结合使用 hfill 和 underbrace

如何结合使用 hfill 和 underbrace

我基本上是在尝试实现如何使嵌套表格跨越所有底层单元格但这次在 1 2 3 下方有一个水平括号。但只要我使用\underbrace, 1 2 3 就会折叠。似乎\underbrace正在取消 的效果\hfill

答案1

\documentclass{article}
\begin{document}
\noindent
\makebox[0.75\textwidth]{left\hfill right}\\
$\underbrace{\makebox[0.75\textwidth]{1\hfill2\hfill3}}_{abc}$

\end{document}

enter image description here

对于宽度未知的表格也是如此:

\documentclass{article}
\newsavebox\TBox
\begin{document}

\savebox\TBox{%
\begin{tabular}{lcr}
left & & right\\
l & center & r\\
\end{tabular}}

\noindent\usebox\TBox\\
$\underbrace{\makebox[\wd\TBox]{\kern\tabcolsep%
l\hfill2\hfill r\kern\tabcolsep}}_{abc}$

\end{document}

enter image description here

答案2

为了防止包含“1 2 3”元素的“框”的宽度折叠,可以使用\makebox[<width>]{<text>}命令。要使用此命令,您必须告诉 LaTeX有多宽这个框应该是。在下面的 MWE 中,我创建了一个长度变量,\tabularwidth用于存储框的预期宽度值。此外,我使用tabular*而不是tabular环境来指示 LaTeX 将表格环境的宽度设置为相同的值。

\documentclass{article}
\usepackage{amsmath}
\newlength\tabularwidth
\setlength\tabularwidth{0.5\textwidth} %% or whatever value you want
\begin{document}
\noindent
\begin{tabular*}{\tabularwidth}{l@{\extracolsep{\fill}}r}
  some text & more text \\
  \multicolumn{2}{l}{$\underbrace{
     \makebox[\tabularwidth]{1 \hfill 2 \hfill 3}}_{\text{abc}}$} \\
\end{tabular*}
\end{document}

enter image description here

附录:如果您不能(或不想)tabular预先设置环境的宽度,而希望使用 LaTeX 在构建环境时给出的任何宽度tabular,则可能需要按以下步骤操作。首先构建一个与应该获得下支撑材料的材料宽度相同的“隐形框”,然后将生成的框的宽度保存为长度参数。最后,此长度参数用于后续实际环境的构建tabular。修订后的 MWE 如下所示:

\documentclass{article}
\usepackage{amsmath}
\newlength\UBwidth % "UB" stands for "underbrace"
\newsavebox\UBbox

\begin{document}

% use TeX's \savebox command to construct a box whose width is of interest
\savebox\UBbox{%
\begin{tabular}{lr}
  some text & more text \\ % assume this is the relevant line of the table
\end{tabular}}  
\setlength\UBwidth{\wd\UBbox} % save width of \UBbox

\noindent 
\begin{tabular}{lr}
  some text & more text \\
  \multicolumn{2}{l}{$\underbrace{
     \makebox[\UBwidth]{1 \hfill 2 \hfill 3}}_{\text{abc}}$} \\
\end{tabular}
\end{document}

enter image description here

如果您觉得这种方法有点复杂,请放心,我同意您的观点。但是,采用这种方法(或其他生成宽度的方法)是必需的,因为 LaTeX 没有提供一种简单的机制来“实时”检索表格环境的整体宽度(或表格环境中任何非空列子集的宽度),即在表格环境仍在构建时。顺便说一句,这也是为什么在我的第一个回答中,我建议您采用而tabular*不是tabular方法...

相关内容