我想要将我的优化问题格式化如下(结果见图):
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\DeclareMathOperator*{\minimize}{minimize}
\begin{document}
\begin{alignat}{-1}
&\mathrlap{\minimize_{x \in \mathbb{R}^n} \quad \sum_{t=1}^T f_t(x) + \sum_{t=1}^T g_t(x) + \sum_{t=1}^T h_t(x) + \sum_{i=1}^n \sum_{j=1}^n \sum_{k=1}^n F_{ijk}(x) } \\
&\text{subject to} & \quad && \sum_{i=1}^n a_{ij} x_i &\leq b_j, \quad && j = 1, \dots, m \\
& & \quad && \sum_{i=1}^n c_{ij} x_i &\leq d_j, \quad && j = 1, \dots, m
\end{alignat}
\end{document}
问题是第一行与方程编号冲突。一种解决方案是添加\hspace{...}
其中一行约束以确保有足够的空间进行编号,但有没有更简洁的方法,不需要手动调整?
答案1
您可以使用来打破最小化目标aligned
,如下所示:
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\DeclareMathOperator*{\minimize}{minimize}
\begin{document}
\begin{alignat}{-1}
&\mathrlap{\begin{aligned}
\minimize_{x \in \mathbb{R}^n}
\quad \sum_{t=1}^T f_t(x) + \sum_{t=1}^T g_t(x) +
\sum_{t=1}^T h_t(x)\\
+ \sum_{i=1}^n \sum_{j=1}^n \sum_{k=1}^n F_{ijk}(x)
\end{aligned}} \\
&\text{subject to} & \quad && \sum_{i=1}^n a_{ij}
x_i &\leq b_j, \quad && j = 1, \dots, m \\
& & \quad && \sum_{i=1}^n c_{ij}
x_i &\leq d_j, \quad && j = 1, \dots, m
\end{alignat}
\end{document}
选择
以下是保持第一行不分割的替代方法:
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\DeclareMathOperator*{\minimize}{minimize}
\begin{document}
\begin{alignat}{2}
&{\minimize_{x \in \mathbb{R}^n}} && \quad
\sum_{t=1}^T f_t(x) + \sum_{t=1}^T g_t(x) +
\sum_{t=1}^T h_t(x)+ \sum_{i=1}^n \sum_{j=1}^n \sum_{k=1}^n F_{ijk}(x)
\\
&\text{subject to} && \quad \sum_{i=1}^n a_{ij}
x_i \leq b_j, \quad j = 1, \dots, m \\
& && \quad \sum_{i=1}^n c_{ij}
x_i \leq d_j, \quad j = 1, \dots, m
\end{alignat}
\end{document}
答案2
你应该考虑一下optidef
。
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{optidef}
\usepackage{xpatch}
\makeatletter
% use consecutive numbers rather than subequations
\xpatchcmd{\env@BaseMiniExclam@process}{\begin{subequations}}{}{}{}
\xpatchcmd{\env@BaseMiniExclam@process}{\end{subequations}}{}{}{}
% the subscript to minimize should not be in display style
\xpatchcmd{\bodyobjLong}{\displaystyle}{}{}{}
\xpatchcmd{\bodyobjLong}{\displaystyle}{}{}{}
\makeatother
\begin{document}
\begin{mini!}
{x \in \mathbb{R}^n}{
\sum_{t=1}^T f_t(x) + \sum_{t=1}^T g_t(x) + \sum_{t=1}^T h_t(x)}{}{}
\breakObjective{\qquad + \sum_{i=1}^n \sum_{j=1}^n \sum_{k=1}^n F_{ijk}(x)}
\addConstraint{\sum_{i=1}^n a_{ij} x_i}{\leq b_j,}{j = 1, \dots, m}
\addConstraint{\sum_{i=1}^n c_{ij} x_i}{\leq d_j,}{j = 1, \dots, m}
\end{mini!}
\end{document}
我做了一些修正:默认情况下,mini!
环境会将行编号为 (1a)、(1b)、(1c) 和 (1d)。如果您喜欢这种格式,请注释掉前两\xpatchcmd
行。
第二个修复是将下标“最小化”:我发现它是显示样式而不是标准下标,这是错误的。
您还可以定义自己的环境:
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{optidef}
\usepackage{xpatch}
% define mini!! for consecutive numbering (not subequations)
\NewEnviron{BaseMiniExclamExclam}[7]{%
\selectConstraintMult{#1}%
\renewcommand{\localOptimalVariable}{#2}%
\ifthenelse{\equal{#7}{b}}{\begingroup\allowdisplaybreaks}{}%
#4
\begin{alignat}{5}
\bodyobj{#2}{#3}{#6}{#5}
\BODY
\end{alignat}
\ifthenelse{\equal{#7}{b}}{\endgroup}{}%
\setStandardMini
}
\NewDocumentEnvironment{mini!!}{
D||{\defaultProblemFormat} O{\defaultConstraintFormat} D<>{} m m m m
}{%
\ifthenelse{\equal{#1}{s}}%
% Short version problem
{\setFormatShort{min}{#2}\BaseMiniExclamExclam{#2}{#4}{#5}{#6}{#7}{min}{#3}}%
% Long version problem
{\setFormatLong{minimise}{#2}\BaseMiniExclamExclam{#2}{#4}{#5}{#6}{#7}{minimise}{#3}}%
}{\endBaseMiniExclam\toggletrue{bodyCon}}
\makeatletter
% the subscript to minimize should not be in display style
\xpatchcmd{\bodyobjLong}{\displaystyle}{}{}{}
\xpatchcmd{\bodyobjLong}{\displaystyle}{}{}{}
\makeatother
\begin{document}
\begin{mini!!}
{x \in \mathbb{R}^n}{
\sum_{t=1}^T f_t(x) + \sum_{t=1}^T g_t(x) + \sum_{t=1}^T h_t(x)}{}{}
\breakObjective{\qquad + \sum_{i=1}^n \sum_{j=1}^n \sum_{k=1}^n F_{ijk}(x)}
\addConstraint{\sum_{i=1}^n a_{ij} x_i}{\leq b_j,}{j = 1, \dots, m}
\addConstraint{\sum_{i=1}^n c_{ij} x_i}{\leq d_j,}{j = 1, \dots, m}
\end{mini!!}
\end{document}
请注意,我使用了“最小化”。遗憾的是,软件包作者硬性规定了这些关键字,而不是让它们可自定义。
答案3
有没有更清洁的方法,不需要手动调整?
我认为约束不需要与目标函数行中的元素在视觉上对齐。因此,我会将约束放在环境中alignat{3}
,并将此环境嵌入gather
环境中。我甚至想\\[2\jot]
在目标函数行后面添加一些额外的垂直空白 -- via --。
\documentclass{article}
\usepackage{amssymb,amsmath}
\DeclareMathOperator*{\minimize}{minimize}
\begin{document}
\begin{gather}
\minimize_{x\in\mathbb{R}^n} \quad
\sum_{t=1}^T f_t(x) + \sum_{t=1}^T g_t(x) + \sum_{t=1}^T h_t(x)
+ \sum_{i=1}^n \sum_{j=1}^n \sum_{k=1}^n F_{ijk}(x) \\[2\jot]
\begin{alignat}{3}
\text{subject to} \quad
&\sum_{i=1}^n a_{ij}x_i &&\leq b_j, &\quad& j=1,\dots,m \\
&\sum_{i=1}^n c_{ij}x_i &&\leq d_j, && j=1,\dots,m
\end{alignat}
\end{gather}
\end{document}
答案4
一种方法是添加几个\span
这样的命令:
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\DeclareMathOperator*{\minimize}{minimize}
\begin{document}
\begin{subequations}
\begin{alignat}{-1}
&\minimize_{x \in \mathbb{R}^n} \quad \sum_{t=1}^T f_t(x) + \sum_{t=1}^T g_t(x) + \sum_{t=1}^T h_t(x) + \sum_{i=1}^n \sum_{j=1}^n \sum_{k=1}^n F_{ijk}(x) \span \span \span \span \span \span \span \span \\
&\text{subject to} & \quad && \sum_{i=1}^n a_{ij} x_i &\leq b_j, \quad && j = 1, \dots, m \hspace{0cm} \\
& & \quad && \sum_{i=1}^n x_i &\leq d_j, \quad && j = 1, \dots, m
\end{alignat}
\end{subequations}
\end{document}