定义为宏的子方程环境后出现不必要的缩进

定义为宏的子方程环境后出现不必要的缩进

在工作中,我经常需要在 Latex 中形成优化问题,因此我定义了一个包含所有我想要的特征的宏来简化程序。但是,由于我对设计此类宏不是很有经验,所以我面临着一个无法轻易摆脱的问题。也就是说,在等式后面的文本中经常会有缩进,如以下示例所示:

\documentclass[letterpaper, 10 pt, conference]{article}

\title{\LARGE \bf
A Scientific Paper
}

% % % % % % % % % % % % % % % % % %

\usepackage{amsmath}
\usepackage{twoopt} % to have two optional arguments
\usepackage{xifthen} % for if-then-else command

%------------------------------------------
% Optimization Problem
%------------------------------------------
% #1 = label (optional)
% #2 = equation numbering (optional - type anything to suppress sub equation numbering)
% #3 = optimization variables
% #4 = objective function
% #5 = constraints
% #6 = min / minimize / max / maximize (text for problem - set automatically)
% #7 = subject to / s.t. (text for constraints - set automatically)

\newcommandtwoopt{\optimize}[7][][]
{%
    \begin{subequations}
        \ifthenelse{\equal{#2}{}}
        {\begin{alignat}{5}
                \underset{#3}{\text{#6 }} & #4 \\
                \text{#7 } & #5 
        \end{alignat}}
        {\begin{alignat*}{5}
            \underset{#3}{\text{#6 }} & #4 \\
            \text{#7 } & #5 
        \end{alignat*}}
        \label{#1}
    \end{subequations}%
}%

\newcommandtwoopt{\minimize}[5][][]
{\optimize[#1][#2]{#3}{#4}{#5}{minimize}{subject to}}%

% % % % % % % % % % % % % % % % % %


\begin{document}

\maketitle

\section{INTRODUCTION}

Let's introduce some equation using the predifined macro to show the undesired indenting after the expression:
\minimize[eqtag]{x,y}
{\sum_{k=0}^N x_k+x_k^2+x_k^3&&+y_k+y_k^2+y_k^3}
{x_k<3 && k = 0,\ldots, N ,\\ &y_k <2 && k = 0,\ldots, N,}
here is the rest of the text that should not be indented. Here is the rest of the text that should not be indented. Here is the rest of the text that should not be indented. Here is the rest of the text that should not be indented. 

\end{document}

给出输出:

在此处输入图片描述

我将非常感激任何可以解决这个问题并可能改善我的宏功能的建议。提前感谢任何帮助!

答案1

首先,下面是我编写的程序,使用xparse而不是twoopts

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

%------------------------------------------
% Optimization Problem
%------------------------------------------
% #1 = min / minimize / max / maximize (text for problem - set automatically)
% #2 = subject to / s.t. (text for constraints - set automatically)
% #3 = equation numbering (* suppresses sub equation numbering)
% #4 = label (optional)
% #5 = optimization variables
% #6 = objective function
% #7 = constraints

\NewDocumentCommand{\optimize}{mmsommm}{%
  \begin{subequations}\IfValueT{#4}{\label{#4}}
  \IfBooleanTF{#3}
    {\optimizeaux{alignat*}{#1}{#5}{#6}{#2}{#7}}
    {\optimizeaux{alignat}{#1}{#5}{#6}{#2}{#7}}
  \end{subequations}
}
\NewDocumentCommand{\optimizeaux}{mmmmmm}{%
  \begin{#1}{5}
  \operatorname*{#2}_{#3} & #4 \\
    \textnormal{#5 } & #6
  \end{#1}
}
\NewDocumentCommand{\minimize}{}{\optimize{minimize}{subject to}}

\begin{document}

\title{A scientific paper}
\author{A. Uthor}
\maketitle

\section{INTRODUCTION}

Let's introduce some equation using the predefined macro to show the undesired 
indenting after the expression:
\minimize[eqtag]
  {x,y}
  {\sum_{k=0}^N x_k+x_k^2+x_k^3&&+y_k+y_k^2+y_k^3}
  {x_k<3 && k = 0,\ldots, N ,\\ &y_k <2 && k = 0,\ldots, N,}
here is the rest of the text that should not be indented. Here is the rest of
the text that should not be indented. Here is the rest of the text that should
not be indented. Here is the rest of the text that should not be indented.
\minimize*
  {x,y}
  {\sum_{k=0}^N x_k+x_k^2+x_k^3&&+y_k+y_k^2+y_k^3}
  {x_k<3 && k = 0,\ldots, N ,\\ &y_k <2 && k = 0,\ldots, N,}
here is the rest of the text that should not be indented. Here is the rest of
the text that should not be indented. Here is the rest of the text that should
not be indented. Here is the rest of the text that should not be indented.

\end{document}

在此处输入图片描述


虚假的空间隐藏在哪里?之后\label{#1}

\label{#1}您可以通过将其放置在适当的位置(即,就在之后)来解决问题\begin{subequations}

\newcommandtwoopt{\optimize}[7][][]
{%
    \begin{subequations}%
        \ifthenelse{\equal{#1}{}}{}{\label{#1}}
        \ifthenelse{\equal{#2}{}}
        {\begin{alignat}{5}
                \underset{#3}{\text{#6 }} & #4 \\
                \text{#7 } & #5 
        \end{alignat}}
        {\begin{alignat*}{5}
            \underset{#3}{\text{#6 }} & #4 \\
            \text{#7 } & #5 
        \end{alignat*}}
    \end{subequations}
}

我还添加了,\ifthenelse以避免出现有关空标签的虚假消息。

相关内容