1

How can I stop lines of my algorithm to be overflowing as shown in the picture below, I would like to break the lines that are too long into 2 lines and just go down to the next line if one line cannot fit in the column .

enter image description here

 \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{alg:MYALG} \end{algorithm}

0

1 Answer 1

1

You're using math mode incorrectly, and therefore TeX has no idea where a good line break should be placed despite using \raggedright. Below I've formatted the equations in the proper way, allowing for a line break to be placed around a relation (like =). I've also added some formatting style that should make your code more readable.

enter image description here

\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}

I don't really enjoy the indentation associated with the automatic line breaks from \raggedright. You could consider manually breaking the content and inserting some indentation (\quad) manually.

enter image description here

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .