Home > Blockchain >  Latex nested loop not displaying correctly
Latex nested loop not displaying correctly

Time:05-24

Hi I'm new to latex I use here the Algorithmic package to write my pseudo code the problem I faced is that 'some text' is displayed correctly under the second loop but the 'return' statement which needs to be outside the first for loop isn't showing correctly also it does not mark the end of each loop (the vertical tic is missing), the execution result is shown in the image: enter image description here

\documentclass{article}
\usepackage[utf8,linesnumbered,ruled,vlined]{algorithm2e}
\usepackage {algpseudocode}
\usepackage{algorithmicx}
\usepackage{algcompatible}

\begin{document}
\begin{algorithm}
\ContinuedFloat

\caption{My algorithm}
\textbf{Input:} solution,bound, data\_matrix, vehicle\_capacity, demand\_data,k\_max,operations\_data, move\_type,tenure, max\_number\_of\_moves,max\_iter,non\_improvement\_maxiter,itermax,epsilon\\
\textbf{Output:} $best$ $solution$ \\[0.1in]

routes = extract routes from \textbf{solution}\\
oldfitness = fitness(\textbf{solution})\\

ls\_move\_type = inversion\\
best\_solution = routes\\[0.1in]

\For{0 \leq i \leq itermax}{
    new\_routes = [ ]\\
    desc = 'normal route'\\
    
    
    \For{route \textbf{in} routes}{
        n=length(route)\\
        comb = int($\frac{n!}{(n-2)!}$)\\
        \If{n \geq 4}{
            tabu\_list\_tenure = $\frac{comb}{5}$\\
            ls\_maxiteration = 50 \\
            ls\_move\_type = 'inversion'\\
        }
        \If{3 \leq n \leq 4}{
            tabu\_list_tenure = $\frac{comb}{4}$ \\
            ls\_maxiteration = 25\\
            ls\_move\_type = 'relocation'\\
        }
       \Else{
         append \textbf{route} to \textbf{new\_routes}\\
         desc = 'short route'\\
        }\\[0.1in]
        }
        
        
    some action
    
        
        
        
    }



return






\end{algorithm}

\end{document}

CodePudding user response:

There is no point in wondering about the output as long as you get errors in your .log file. After an error, latex only recovers enough to syntax check the rest of the document, not necessarily producing sensible output.

Some of the most critical problems:

  • never ignore error messages!

  • utf8 isn't a valid option for the algorithm2e package

  • \ContinuedFloat is not defined by default. If you want to use it, you need a package which defines it. Maybe you want to use the caption package?

  • never ever use math mode to fake italic text as in $best$ $solution$. This completely messes up the kerning

  • some of your _ are not escaped

  • you mustn't use math commands like 0 \leq i \leq outside of math mode

  • use something like \Return to properly format it

  • using \\ for line breaks is already quite questionable, but using them two times in a row is simply an error.

  • because one can't say it often enough: never ignore error messages!


\documentclass{article}
\usepackage[
%utf8,
linesnumbered,ruled,vlined]{algorithm2e}
\usepackage {algpseudocode}
\usepackage{algorithmicx}
\usepackage{algcompatible}


\begin{document}
\begin{algorithm}
%\ContinuedFloat
\caption{My algorithm}
\textbf{Input:} solution,bound, data\_matrix, vehicle\_capacity, demand\_data,k\_max,operations\_data, move\_type,tenure, max\_number\_of\_moves,max\_iter,non\_improvement\_maxiter,itermax,epsilon

\textbf{Output:} \emph{best solution}

\medskip

routes = extract routes from \textbf{solution}

oldfitness = fitness(\textbf{solution})

ls\_move\_type = inversion

best\_solution = routes

\medskip

\For{$0 \leq i \leq$ itermax}{
    new\_routes = [ ]
    
    desc = 'normal route'
    
    \For{route \textbf{in} routes}{
        n=length(route)
        
        comb = int($\frac{n!}{(n-2)!}$)
        
        \If{$n \geq 4$}{
            tabu\_list\_tenure = $\frac{comb}{5}$
            
            ls\_maxiteration = 50 
            
            ls\_move\_type = 'inversion'
        }
        \If{$3 \leq n \leq 4$}{
            tabu\_list\_tenure = $\frac{comb}{4}$
            
            ls\_maxiteration = 25
            
            ls\_move\_type = 'relocation'
        }
       \Else{
         append \textbf{route} to \textbf{new\_routes}
         
         desc = 'short route'         
        }
        
        \medskip
        
        }
    some action   
    }
\Return
\end{algorithm}

\end{document}

enter image description here

  • Related