自动调整对齐环境的宽度

自动调整对齐环境的宽度

我想要创建一个环境,比如说myalign,类似于align环境,但满足以下附加要求:

  1. myalign启用tagmyalign*禁用它
  2. 如果方程的宽度大于\textwidth,它会自动调整其宽度以\textwidth
  3. 否则,它保留宽度
  4. 有一个可选参数允许将宽度更改为1.1\textwidth默认值 1

例如,考虑以下align环境:

\begin{align}
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x+ x + x + x + x + x + x\notag\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x \notag
\end{align}

我可以2.使用以下代码

\[\resizebox{1\textwidth}{!}{
$\begin{aligned}
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x+ x + x + x + x + x + x\notag\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x \notag
\end{aligned}$}\]

但它不能令人满意1.3.而且我不知道如何将其定义为一个新的环境)

此外,对于4.,当我将其更改1\textwidth为时1.1\textwidth,它给出的结果不居中。我也想修复此行为。

在此处输入图片描述


平均能量损失

\documentclass[10pt]{article}
\usepackage{amsmath, graphicx}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\begin{document}

\verb|\begin{align}...|
\begin{align}
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x+ x + x + x + x + x + x\notag\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x \notag
\end{align}

\verb|\resizebox{1\textwidth}{!}{...|
\[\resizebox{1\textwidth}{!}{
$\begin{aligned}
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x+ x + x + x + x + x + x\notag\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x \notag
\end{aligned}$}\]

\verb|\resizebox{1.1\textwidth}{!}{...|
\[\resizebox{1.1\textwidth}{!}{
$\begin{aligned}
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x+ x + x + x + x + x + x\notag\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x \notag
\end{aligned}$}\]

\end{document}

根据Mico的建议,我提出了以下解决方案:

\documentclass[10pt]{article}
\usepackage{amsmath, graphicx}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{adjustbox, calc}

\makeatletter % https://tex.stackexchange.com/q/59925
\newcommand{\settowidthofalign}[2]{%
  \setbox\z@=\vbox{
    \begin{align}
    #2
    \ifmeasuring@\else\global\let\got@maxcolwd\maxcolumn@widths\fi
    \end{align}
  }%
  \begingroup
  \def\or{+}\edef\x{\endgroup#1=\dimexpr\got@maxcolwd\relax}\x}

\newcommand{\settowidthofalignstar}[2]{%
  \setbox\z@=\vbox{
    \begin{align*}
    #2
    \ifmeasuring@\else\global\let\got@maxcolwd\maxcolumn@widths\fi
    \end{align*}
  }%
  \begingroup
  \def\or{+}\edef\x{\endgroup#1=\dimexpr\got@maxcolwd\relax}\x}

\makeatother

\newlength{\mylen}

\NewDocumentEnvironment{myalign}{O{1} b}{
    \settowidthofalign{\mylen}{#2}%
    \[\begin{adjustbox}{max width=#1\textwidth, center}%
    \parbox{\maxof{\mylen}{#1\textwidth}}{%
    \setlength{\abovedisplayskip}{0pt}
    \setlength{\belowdisplayskip}{0pt}
    \setlength{\abovedisplayshortskip}{0pt}
    \setlength{\belowdisplayshortskip}{0pt}
    \begin{align}
    #2
    \end{align}}\end{adjustbox}\notag\]}{}
    
\NewDocumentEnvironment{myalign*}{O{1} b}{
    \settowidthofalignstar{\mylen}{#2}%
    \[\begin{adjustbox}{max width=#1\textwidth, center}%
    \parbox{\maxof{\mylen}{#1\textwidth}}{%
    \setlength{\abovedisplayskip}{0pt}
    \setlength{\belowdisplayskip}{0pt}
    \setlength{\abovedisplayshortskip}{0pt}
    \setlength{\belowdisplayshortskip}{0pt}
    \begin{align*}
    #2
    \end{align*}}\end{adjustbox}\notag\]}{}

    
\begin{document}


\begin{align}
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x+ x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x \notag
\end{align}

\begin{myalign}
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x+ x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x \notag
\end{myalign}

\begin{myalign*}[1.1]
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x+ x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x\\
x +y &= x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x \notag
\end{myalign*}

\end{document}

这似乎效果很好,但我不确定这是否是一种有效的实现方法......

相关内容