我正在尝试排版线性程序:
\usepackage{amsmath,amsthm,amssymb}
\usepackage{array}
\begin{document}
\begin{align*}
\text{maximize}\quad 3x + 2y &+ 3x + 2y \\
\text{subject to}\quad 2x + y &\leq 18 \\
2x + 3y &\leq 42 \qquad\forall x\\
3x + y &\leq 2 \qquad\forall y\\
x, y &\geq 0
\end{align*}
\begin{center}
\begin{tabular}{r>{\(\displaystyle}r<{\)}@{\(\;\)}>{\(\displaystyle}c<{\)}@{\(\;\)}>{\(\displaystyle}l<{\)}>{\(\displaystyle}l<{\)}}
maximize &\multicolumn{4}{>{\(\displaystyle}l<{\)}}{3x + 2y + 3x + 2y}\\
subject to& 2x + y & \leq & 18 \\
& 2x + 3y & \leq & 42 & \forall x\\
& 3x + y & \leq & 2 & \forall y\\
& x, y & \geq & 0 \\
\end{tabular}
\end{center}
\end{document}
tabular
更容易正确对齐,但正如您所见,它比更紧凑align
。
我希望获得与表格显示数学相同的垂直间距(或行间间距)。我找到了该\arraystretch
属性,但我不知道需要设置什么值。
答案1
对齐会增加行距, 1\jot
因此
\documentclass{article}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{array}
\begin{document}
\begin{align*}
\text{maximize}\quad 3x + 2y &+ 3x + 2y \\
\text{subject to}\quad 2x + y &\leq 18 \\
2x + 3y &\leq 42 \qquad\forall x\\
3x + y &\leq 2 \qquad\forall y\\
x, y &\geq 0
\end{align*}
\begin{center}
\openup\jot
\setbox\strutbox\hbox{\vrule height.7\baselineskip depth.3\baselineskip width 0pt}
\begin{tabular}{r>{\(\displaystyle}r<{\)}@{\(\;\)}>{\(\displaystyle}c<{\)}@{\(\;\)}>{\(\displaystyle}l<{\)}>{\(\displaystyle}l<{\)}}
maximize &\multicolumn{4}{>{\(\displaystyle}l<{\)}}{3x + 2y + 3x + 2y}\\
subject to& 2x + y & \leq & 18 \\
& 2x + 3y & \leq & 42 & \forall x\\
& 3x + y & \leq & 2 & \forall y\\
& x, y & \geq & 0 \\
\end{tabular}
\end{center}
\end{document}
答案2
答案3
optidef
您有一个专门用于优化问题的包 ( )。您还可以轻松地将其与alignat*
环境对齐。以下是两种解决方案:
\documentclass{article}
\usepackage{amsmath}
\usepackage{optidef}
\begin{document}
\begin{alignat*}{3}
& \text{maximize} & \quad 3x + 2y &+ 3x + 2y \\
& \text{subject to} & 2x + y &\leq 18 \\
& & 2x + 3y &\leq 42 & & \forall x\\
& & 3x + y &\leq 2 & & \forall y\\
& & x, y &\geq 0
\end{alignat*}
\begin{maxi*}
{}{3x + 2y + 3x + 2y}{}{}
\addConstraint{2x + y }{\leq 18}
\addConstraint{2x +3y }{\leq 42}{\qquad\forall x}
\addConstraint{3x + y }{\leq 2}{\qquad\forall y}
\addConstraint{x, y }{\geq 0}
\end{maxi*}
\end{document}