算法和控制注释行(向右或向左移动)

算法和控制注释行(向右或向左移动)

我需要将评论向右移动。基本上,在下面的图片中

在此处输入图片描述

该注释分散了伪代码的注意力,因此,我希望得到下面的图片

在此处输入图片描述

如何将注释移到右侧?这是我的代码

\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{algorithm,algpseudocode,float}

%-----------
\makeatletter
\newenvironment{breakablealgorithm}
  {% \begin{breakablealgorithm}
   \begin{center}
     \refstepcounter{algorithm}% New algorithm
     \hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
     \renewcommand{\caption}[2][\relax]{% Make a new \caption
       {\raggedright\textbf{\ALG@name~\thealgorithm} ##2\par}%
       \ifx\relax##1\relax % #1 is \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%
       \else % #1 is not \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%
       \fi
       \kern2pt\hrule\kern2pt
     }
  }{% \end{breakablealgorithm}
     \kern2pt\hrule\relax% \@fs@post for \@fs@ruled
   \end{center}
  }
\makeatother

\begin{document}

\begin{breakablealgorithm}
  \caption{PPPPPPPPPPPPP}
  \begin{algorithmic}[1]
  \makeatletter\setcounter{ALG@line}{0}\makeatother
  \If { $\phi < 10^\circ$}
        \State{$A = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$} \Comment{ where \textbf{a} is aaaaaaaaaa   constant.}
        \Else
           \State{$B =  bbbbbbbbb$ }
        \EndIf  
\end{algorithmic}
\end{breakablealgorithm}


\end{document}

答案1

您可以使用\parbox\raggedright如果太窄则带有文本):

\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{algorithm,algpseudocode,float}

%-----------
\makeatletter
\newenvironment{breakablealgorithm}
  {% \begin{breakablealgorithm}
   \begin{center}
     \refstepcounter{algorithm}% New algorithm
     \hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
     \renewcommand{\caption}[2][\relax]{% Make a new \caption
       {\raggedright\textbf{\ALG@name~\thealgorithm} ##2\par}%
       \ifx\relax##1\relax % #1 is \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%
       \else % #1 is not \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%
       \fi
       \kern2pt\hrule\kern2pt
     }
  }{% \end{breakablealgorithm}
     \kern2pt\hrule\relax% \@fs@post for \@fs@ruled
   \end{center}
  }
\makeatother

\begin{document}

\begin{breakablealgorithm}
  \caption{PPPPPPPPPPPPP}
  \begin{algorithmic}[1]
  \makeatletter\setcounter{ALG@line}{0}\makeatother
  \If { $\phi < 10^\circ$}
        \State{$A = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$} \Comment{\parbox[t]{4cm}{\raggedright where \textbf{a} is some unknown constant.}}
        \Else
           \State{$B =  bbbbbbbbb$ }
        \EndIf  
\end{algorithmic}
\end{breakablealgorithm}

\end{document}

在此处输入图片描述

相关内容