算法中多行方程的对齐

算法中多行方程的对齐

我想对齐环境multline中的方程式,以使其适合当前块(与下面示例中的algorithm行首对齐):# Solve the equation:

在此处输入图片描述

代码的最小版本:

\documentclass[journal, a4paper]{IEEEtran}

\usepackage{amsmath}
\usepackage{mathtools}

\usepackage[ruled,norelsize]{algorithm}
\usepackage{algpseudocode,algorithmicx}

\algnewcommand{\LineComment}[1]{\State \(\#\) #1}

\begin{document}

\begin{figure}[!t]
  \begin{algorithm}[H]
    \begin{algorithmic}
      \Statex
      \For{$i = 1, ..., n$}
        \LineComment{Solve the equation:}
        \begin{equation}
        \begin{multlined}[c]
          a = 222222222 + 222222222222222 + 222222 +  \\
          %
          + 222222222222222222
        \end{multlined}
        \end{equation}
      \EndFor        \end{algorithmic}
  \end{algorithm}
\end{figure}

\end{document}

更新:表明建议的解决方案在嵌套循环中不起作用:

\documentclass[journal, a4paper]{IEEEtran}

\usepackage{amsmath}
\usepackage{mathtools,nccmath}

\usepackage[ruled,norelsize]{algorithm}
\usepackage{algpseudocode,algorithmicx}

\algnewcommand{\LineComment}[1]{\State \(\#\) #1}

\begin{document}

\begin{figure}[!t]
  \begin{algorithm}[H]
    \begin{algorithmic}
      \While{not converged}
        dnwjkfn
        \Statex
        \For{$i = 1, ..., n$}
          \LineComment{Solve the equation:}
          \State\vspace*{-\baselineskip}
          \begin{fleqn}[\dimexpr\leftmargini-\labelsep]
          \setlength\belowdisplayskip{0pt}
          \begin{equation}
              \begin{multlined}[c]
                a = 222222222 + 222222222222222 + 222222 \\
                  + 222222222222222222
              \end{multlined}
          \end{equation}
          \end{fleqn}%
        \EndFor
      \EndWhile
    \end{algorithmic}
  \end{algorithm}
\end{figure}

\end{document}

在此处输入图片描述

答案1

像这样吗?

\documentclass[journal, a4paper]{IEEEtran}

\usepackage{amsmath}
\usepackage{mathtools,nccmath}

\usepackage[ruled,norelsize]{algorithm}
\usepackage{algpseudocode,algorithmicx}

\algnewcommand{\LineComment}[1]{\State \(\#\) #1}

\begin{document}

\begin{figure}[!t]
  \begin{algorithm}[H]
    \begin{algorithmic}
      \Statex
      \For{$i = 1, ..., n$}
        \LineComment{Solve the equation:}
        \State\vspace*{-\baselineskip}
        \begin{fleqn}[\dimexpr\leftmargini-\labelsep]
        \setlength\belowdisplayskip{0pt}
        \begin{equation}
            \begin{multlined}[c]
              a = 222222222 + 222222222222222 + 222222 \\
                + 222222222222222222
            \end{multlined}
        \end{equation}
        \end{fleqn}%
      \EndFor \end{algorithmic}
  \end{algorithm}
\end{figure}

\end{document} 

在此处输入图片描述

编辑:对于嵌套环境的情况,您可以增加环境\leftmargin--\labelsep可选参数中的数字 og fleqn

\documentclass[journal, a4paper]{IEEEtran}

\usepackage{amsmath}
\usepackage{mathtools,nccmath}

\usepackage[ruled,norelsize]{algorithm}
\usepackage{algpseudocode,algorithmicx}

\algnewcommand{\LineComment}[1]{\State \(\#\) #1}

\begin{document}

\begin{figure}[!t]
  \begin{algorithm}[H]
    \begin{algorithmic}
      \While{not converged}
        dnwjkfn
        \Statex
        \For{$i = 1, ..., n$}
          \LineComment{Solve the equation:}
          \State\vspace*{-\baselineskip}
          \begin{fleqn}[\dimexpr(\leftmargini-\labelsep)*2]
          \setlength\belowdisplayskip{0pt}
 \begin{equation}
                \begin{multlined}[c]
                a = 222222222 + 222222222222222 + {}\\
                  + 222222 +222222222222222222
              \end{multlined}
          \end{equation}
          \end{fleqn}%
        \EndFor
      \EndWhile
    \end{algorithmic}
  \end{algorithm}
\end{figure}

\end{document} 

在此处输入图片描述

相关内容