我想要创建一个环境,比如说myalign
,类似于align
环境,但满足以下附加要求:
myalign
启用tag
和myalign*
禁用它- 如果方程的宽度大于
\textwidth
,它会自动调整其宽度以\textwidth
- 否则,它保留宽度
- 有一个可选参数允许将宽度更改为
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}
这似乎效果很好,但我不确定这是否是一种有效的实现方法......