计算对齐环境中的剩余水平空间

计算对齐环境中的剩余水平空间

align*经常使用,有时最好在等式的右边添加文本,而不是使用\intertext、 或\shortintertext,这往往会打断数学流程。这很简单,使用\text{}除了如果文本长度超过可用空间量。在这种情况下,我使用\parbox并调整其宽度,直到它适合,如第一组方程所示:

在此处输入图片描述

但是,我想知道是否有办法将\RemainingSpace下面的宏定义为某种\dimexpr表达式,以便它计算可用的水平空间量\parbox

或者,有更好的方法吗?我知道tabularx解决方案是可行的,但当align涉及到方程式时,它确实更可取。


小页面:

  • 当工作宽度减小时,问题会更加严重minipage。下图是不是包含在这里,但位于下面 MWE 生成的输出的第 2 页。

笔记:

  • 我并不真正担心编号方程的问题,但这可能对其他人有用。
  • 我假设相同的解决方案可以适用于alignat*,但为了以防万一,我添加了一个测试用例。

代码:

\documentclass{article}
\usepackage{showframe}
\usepackage[fleqn]{amsmath}

\newcommand*{\RemainingSpace}{9.0cm}%
\newcommand{\Note}[1]{\parbox[t]{\RemainingSpace}{#1}}%

\newcommand*{\RubbishText}{This was obvious once it was determined that the earlier versions $E = mc$ and $E = mc^3$ were not quite accurate.}%

\newcommand{\TestCases}{%
    First using \textbf{align*} with a \textbf{quad} for spacing:
    \begin{align*}
        E &= mc^2 \quad\Note{\RubbishText}\\
        F &= ma + \sin^2 x + \cos^2 x - 1.
    \end{align*}
    Or could use double ampersand for spacing (but less important than the above version):
    \begin{align*}
        E &= mc^2 &&\Note{\RubbishText}\\
        F &= ma + \sin^2 x + \cos^2 x - 1
    \end{align*}
    Or, using \textbf{alignat*}
    \begin{alignat*}{2}
        E &= mc^2 \quad\Note{\RubbishText}\\
        F &= ma + \sin^2 x + \cos^2 x - 1
    \end{alignat*}%
}%


\begin{document}\noindent
\TestCases

\clearpage\noindent
Same code as above but put in a \verb|\minipage{0.70\linewidth}|:
\bigskip

\hspace*{0.25\linewidth}%
\begin{minipage}{0.70\linewidth}
    \TestCases
\end{minipage}%
\end{document}

答案1

需要运行几次:

在此处输入图片描述

\documentclass{article}
\usepackage{showframe}
\usepackage[fleqn]{amsmath}

\newcommand*{\RubbishText}{This was obvious once it was determind that the earlier versions $E = mc$ and $E = mc^3$ were not quite accurate.}%

\makeatletter
\newcount\zzz
\def\foo{%
\global\advance\zzz\@ne
\ifmeasuring@
\else
\expandafter\ifx\csname zz\romannumeral\zzz\endcsname\relax
\else
\edef\RemainingSpace{\the\dimexpr 30000000sp - 
         \csname zz\romannumeral\zzz\endcsname sp\relax}%
\typeout{\the\zzz: \RemainingSpace}%
\fi
\pdfsavepos
\edef\tmp{\write\@auxout{%
  \gdef\noexpand\noexpand\expandafter\noexpand
    \csname zz\romannumeral\zzz\endcsname{\noexpand\the\pdflastxpos}}}
\tmp
\fi
}
\makeatother

\newcommand*{\RemainingSpace}{9.0cm}%

\begin{document}\noindent


First using \verb|align*| with a \verb|\quad| for spacing:
\begin{align*}
    E &= mc^2 \quad\foo\parbox[t]{\RemainingSpace}{\RubbishText}\\
    F &= ma + \sin^2 x + cos^2 x - 1.
\end{align*}
Or could use \verb|&&| for spacing (but less important than the above version):
\begin{align*}
    E &= mc^2 &&\foo\parbox[t]{\RemainingSpace}{\RubbishText}\\
    F &= ma + \sin^2 x + cos^2 x - 1
\end{align*}
Or, using \verb|alignat*|
\begin{alignat*}{2}
    E &= mc^2 \quad\foo\parbox[t]{\RemainingSpace}{\RubbishText}\\
    F &= ma + \sin^2 x + cos^2 x - 1
\end{alignat*}
\end{document}

答案2

包裹linegoal并且其\linegoal宏在两次编译后产生了正确的输出。但它们会抛出错误和警告,这可能是由于amsmath的环境处理其内容的方式所致。

egreg 使我免于查看答案和代码,并提供了\ifmeasuring@条件amsmath

我定义了一个宏,用于隐藏测量中的文本。遗憾的是,这对于需要知道宽度才能正确对齐列的版本\Note不起作用。&&align*amsmath

因此,我提供了一个带星号的版本,\Note其功能与以前相同,仍然会产生警告和错误。

请注意,我已将 包含\quad在未加星号的版本中\Note

代码

\documentclass{article}
\usepackage{showframe}
\usepackage{linegoal}
\usepackage[fleqn]{amsmath}

\newcommand*{\RubbishText}{This was obvious once it was determind that the earlier versions $E = mc$ and $E = mc^3$ were not quite accurate.}%

\makeatletter
\newcommand*{\Note}{\@ifstar\s@Note\@Note}
  \newcommand*{\@Note}[1]{\ifmeasuring@\else\quad\parbox[t]{\linegoal}{#1}\fi}
  \newcommand*{\s@Note}[1]{\parbox[t]{\linegoal}{#1}}
\makeatother

\begin{document}\noindent
First using \verb|align*| with a \verb|\quad| for spacing:
\begin{align*}
    E &= mc^2 \Note{\RubbishText} \\
    F &= ma + \sin^2 x + cos^2 x - 1.
\end{align*}
Or could use \verb|&&| for spacing (but less important than the above version):
\begin{align*}
    E &= mc^2 & \Note*{\RubbishText}\\
    F &= ma + \sin^2 x + cos^2 x - 1
\end{align*}
Or, using \verb|alignat*|
\begin{alignat*}{2}
    E &= mc^2 \Note{\RubbishText}  \\
    F &= ma + \sin^2 x + cos^2 x - 1
\end{alignat*}
\end{document}

输出

在此处输入图片描述

相关内容