2

I want to write in this algorithm if after for. How can I do that?

\begin{algorithm}[H]
\caption{Algorithm Gaussian elimination with patrial pivoting}
\SetAlgoLined
\For{$i=1:1:n$}{
        $p_{i} = i;$
    }       
    {
    }  
    \For{$k=1:1:n$}{
        $max=|a_{pk}k|$\\
        \For{$i\;=k+1:1:n$}{
            \if{$max<|a_{pk}k|$}

            {}
            {
            }
               $a_{i}{j}\;=a_{i}{j} - m_{i}{k}a_{k}{j};$\\

Like this algorithm

I have tried this

\begin{algorithm}[H]
\caption{Algorithm Gaussian elimination with patrial pivoting}
\SetAlgoLined
\For{$i=1:1:n$}{
        $p_{i} = i;$
    }       
    {
    }  
    \For{$k=1:1:n$}{
        $max=|a_{pk}k|$\\
        \For{$i\;=k+1:1:n$}{
            \if{$max<|a_{pk}k|$} 
            {
                $max<|a_{pk}k|$
            }
            }
            {
            }
               $a_{i}{j}\;=a_{i}{j} - m_{i}{k}a_{k}{j};$\\
        {             
    }   
  }
          $b_{i}=b_{i}-m_{i}{k}b_{k};$

but it tells me incomplete \if.

Werner
  • 603,163

2 Answers2

3

Adjust the keywords components associated with \For through \SetKwFor:

enter image description here

\documentclass{article}

\usepackage{algorithm2e}

\SetKwFor{For}{for if}{do}{end for}

\begin{document}

\begin{algorithm}
  \caption{Some algorithm}
  \SetAlgoLined
  \For{$i=1:1:n$}{
    Do something
  }
\end{algorithm}

\end{document}

If you want for if ... then use

\SetKwFor{For}{for if}{then}{end for}

enter image description here

Werner
  • 603,163
0

i find the solution

\begin{algorithm}[H]{
\caption{Algorithm Gaussian elimination with patrial pivoting}
\SetAlgoLined
\For{$i=1:1:n$}{
        $p_{i} = i;$
    }       
    {
    }  
    \For{$k=1:1:n$}{
        $max=|a_{pk}k|$\\
        \For{$i\;=k+1:1:n$}{
           \If{$max=|a_{pk}k|$}{$l= i;$\\$max = |a_{pk}k|$}
            }
              $r=pl;$\\
              $pl = pk$\\
              $pk = r;$  
              \For{$i=k+1:1:n$}{
                $a_{pi}j = a_{pi}ka_{pj}ka_{pk}j;$  
              }

           }