避免算法中的行溢出

避免算法中的行溢出

我怎样才能阻止我的算法行溢出,如下图所示,我想将太长的行分成两行,如果一行无法容纳在列中,则直接转到下一行。

在此处输入图片描述

 \begin{algorithm}
\caption{Iterative Process}
\begin{algorithmic}[1]
    \raggedright    

\STATE  $OwnerClassPrediction()$
    \STATE {iteration}=1
    \STATE {Predictions(iteration-1)}=methodTraceList.predictions
    \STATE {Predictions(iteration)}=$\emptyset$
    \WHILE {$Predictions(iteration) \neq Predictions(iteration-1)$}    


    \STATE$Predictions(iteration)$=SurroundednessPurePattern($Predictions(iteration)$); 
    \STATE$Predictions(iteration)$=SurroundednessMixedPattern($Predictions(iteration)$); 
    \STATE $Predictions(iteration)$=InheritanceInterfacesPattern($Predictions(iteration)$); 
    \STATE $Predictions(iteration)$=AllCallersPattern($Predictions(iteration)$); 
    \STATE $Predictions(iteration)$=AllCalleesPattern($Predictions(iteration)$); 
    \STATE{iteration}++;        
    \ENDWHILE
\end{algorithmic}

\label{算法:MYALG} \end{算法}

答案1

您错误地使用了数学模式,因此尽管使用了 ,TeX 也不知道应该把换行符放在哪里\raggedright。下面我以正确的方式格式化了方程式,允许在关系周围放置换行符(如=)。我还添加了一些格式样式,可以让您的代码更具可读性。

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm,algorithmic}

\newcommand{\var}{\textit}
\newcommand{\proc}{\textbf}
\newcommand{\prop}{\texttt}
\newcommand{\plusplus}{{+}{+}}% Other options: https://tex.stackexchange.com/q/4302/5764

\begin{document}

\begin{algorithm}
  \caption{Iterative Process}
  \begin{algorithmic}[1]
    \raggedright
    \STATE \proc{OwnerClassPrediction}()
    \STATE $\var{iteration} = 1$
    \STATE $\proc{Predictions}(\var{iteration} - 1) = \prop{methodTraceList.predictions}$
    \STATE $\proc{Predictions}(\var{iteration}) = \emptyset$
    \WHILE{$\proc{Predictions}(\var{iteration}) \neq \proc{Predictions}(\var{iteration} - 1)$}
      \STATE $\proc{Predictions}(\var{iteration}) = \proc{SurroundednessPurePattern}(\proc{Predictions}(\var{iteration}))$
      \STATE $\proc{Predictions}(\var{iteration}) = \proc{SurroundednessMixedPattern}(\proc{Predictions}(\var{iteration}))$
      \STATE $\proc{Predictions}(\var{iteration}) = \proc{InheritanceInterfacesPattern}(\proc{Predictions}(\var{iteration}))$
      \STATE $\proc{Predictions}(\var{iteration}) = \proc{AllCallersPattern}(\proc{Predictions}(\var{iteration}))$
      \STATE $\proc{Predictions}(\var{iteration}) = \proc{AllCalleesPattern}(\proc{Predictions}(\var{iteration}))$
      \STATE $\var{iteration}\plusplus$
    \ENDWHILE
  \end{algorithmic}
\end{algorithm}

\end{document}

我不太喜欢与 自动换行符 相关的缩进\raggedright。您可以考虑手动拆分内容并\quad手动插入一些缩进 ( )。

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm,algorithmic}

\newcommand{\var}{\textit}
\newcommand{\proc}{\textbf}
\newcommand{\prop}{\texttt}
\newcommand{\plusplus}{{+}{+}}% Other options: https://tex.stackexchange.com/q/4302/5764

\begin{document}

\begin{algorithm}
  \caption{Iterative Process}
  \begin{algorithmic}[1]
    \STATE \proc{OwnerClassPrediction}()
    \STATE $\var{iteration} = 1$
    \STATE $\proc{Predictions}(\var{iteration} - 1) = \prop{methodTraceList.predictions}$
    \STATE $\proc{Predictions}(\var{iteration}) = \emptyset$
    \WHILE{$\proc{Predictions}(\var{iteration}) \neq \proc{Predictions}(\var{iteration} - 1)$}
      \STATE $\proc{Predictions}(\var{iteration}) = {}$ \\
        \quad $\proc{SurroundednessPurePattern}(\proc{Predictions}(\var{iteration}))$
      \STATE $\proc{Predictions}(\var{iteration}) = {}$ \\
        \quad $\proc{SurroundednessMixedPattern}(\proc{Predictions}(\var{iteration}))$
      \STATE $\proc{Predictions}(\var{iteration}) = {}$ \\
        \quad $\proc{InheritanceInterfacesPattern}(\proc{Predictions}(\var{iteration}))$
      \STATE $\proc{Predictions}(\var{iteration}) = {}$ \\
        \quad $\proc{AllCallersPattern}(\proc{Predictions}(\var{iteration}))$
      \STATE $\proc{Predictions}(\var{iteration}) = {}$ \\
        \quad $\proc{AllCalleesPattern}(\proc{Predictions}(\var{iteration}))$
      \STATE $\var{iteration}\plusplus$
    \ENDWHILE
  \end{algorithmic}
\end{algorithm}

\end{document}

相关内容