确定 align* 的自然宽度:varwidth 使用 \linewidth,即使显示正确

确定 align* 的自然宽度:varwidth 使用 \linewidth,即使显示正确

在之前的一个问题中,align*似乎varwidth忽略了fleqn选项,它似乎align*没有被环境正确处理varwidth。提供了一个“修复”,导致正确的视觉显示。但是,测量结果仍然报告该框为\linewidth

请注意,两个varwidth报告的框宽度相同:

在此处输入图片描述

问题:

  • 如何测量包含使用的内容的自然宽度align*

笔记:

相关问题:

代码:

\documentclass[fleqn]{article}
\usepackage{showframe}
\usepackage{varwidth}
\usepackage{mathtools}
\usepackage{printlen}
\uselengthunit{cm}

\newcommand*{\MyContent}{%
    Using align*:
    \begin{align*}
        E &= mc^2
        \shortintertext{and}
        F &= ma
    \end{align*}%
}%

\newsavebox{\MySaveBox}
\newlength{\WidthOfSaveBox}

\newcommand{\ShowWidth}[1]{%
    \savebox{\MySaveBox}{#1}%
    \settowidth{\WidthOfSaveBox}{\usebox{\MySaveBox}}%
    \par\usebox{\MySaveBox}\par
    WidthOfSaveBox=\printlength{\WidthOfSaveBox}%
    \par\hrule
}%

\begin{document}

linewidth=\printlength{\linewidth}\hrule

\medskip
Inside of \verb|\parbox{0.5\linewidth}|:
\ShowWidth{%
    \parbox{0.5\linewidth}{%
        \MyContent%
    }%
}

\medskip
Inside \verb|varwidth| (without patch):
\ShowWidth{%
    \begin{varwidth}{\linewidth}
        \MyContent
    \end{varwidth}%
}

\medskip% Works visually, but still uses full width
%% https://tex.stackexchange.com/questions/394778/align-in-varwidth-seems-to-ignore-fleqn-option
\makeatletter\let\@vwid@eqmodetrue\@vwid@eqmodefalse\makeatother
Inside \verb|varwidth| (with patch):
\ShowWidth{%
    \begin{varwidth}{\linewidth}
        \MyContent
    \end{varwidth}%
}

\medskip%% This works fine.
Inside \verb|varwidth| with non-display content:
\ShowWidth{Some text}
\end{document}

答案1

这个答案可能对你没用,因为你特别想要它用于align*。但是tabstackengine,可以align在 中构建 类似实体的包hbox会跟踪各个单元格的内容、宽度、高度和深度,这些内容可以在以后调用。

[确保与软件包的 V1.5 结合使用listofitems。]

\documentclass[12pt]{scrartcl}
\usepackage{tabstackengine}
\TABstackMath
\fboxsep=1pt\relax
\setstackgap{S}{8pt}
\begin{document}
\[
\alignShortstack{E=&mc^2\\F=&ma}
\]
The logical size of the align structure is
   \TABcells{} rows/\TABcells{1} columns.\\
The width of column 1 is \TABwd{1}\\
The width of column 2 is \TABwd{2}\\
The height of row 1 is \TABht{1}\\
The height of row 2 is \TABht{2}\\
with an \Sstackgap{} gap between rows.\\
The tokens of cell 1,2 are \getTABcelltoks[1,2]``%
  \detokenize\expandafter{\the\TABcelltoks}''\\
The tokens of cell 2,2 are ``\detokenize\expandafter\expandafter\expandafter{\TABcellRaw[2,2]}''\\
That cell's contents (in the prevailing TAB mode/style)\\
\mbox{}~~~can be recovered as ``\TABcell{2}{2}''\\
\fboxsep=0pt\relax
That cell, accounting for its presented row height \&\\
\mbox{}~~column width, may be recovered as \fbox{\TABcellBox{2}{2}}\\
In the case of cell 2,1, that would be \fbox{\TABcellBox{2}{1}}
\end{document}

在此处输入图片描述

因此,可以得到整体对齐结构的自然宽度为

\dimexpr\TABwd{1} + \TABwd{2}\relax

相关内容