Recuando linhas de código no algoritmo

Recuando linhas de código no algoritmo

Estou escrevendo um algoritmo que tem dois estágios. Como posso recuar o código para o estágio um e para o estágio dois?

\begin{algorithm}[H]
\caption*{my algorithm}
\begin{algorithmic}
    \STATE \textbf{Stage one:} this is stage one
    \FORALL{i}
        \STATE do something
    \ENDFOR
    \STATE \textbf{Stage two:} this is stage two
    \STATE Update the trie: 
    \FORALL{j}
    \STATE do something
    \ENDFOR
\end{algorithmic}
\end{algorithm}

Responder1

No código de exemplo a seguir, defini dois novos comandos que permitem alterar o recuo; simplesmente coloque o fragmento desejado usando \bindent, \eindent; o comprimento \myindentcontrola o valor do recuo:

\documentclass{article}
\usepackage{algorithm,algorithmic}
\usepackage{caption}

\newlength\myindent
\setlength\myindent{2em}
\newcommand\bindent{%
  \begingroup
  \setlength{\itemindent}{\myindent}
  \addtolength{\algorithmicindent}{\myindent}
}
\newcommand\eindent{\endgroup}

\begin{document}

\begin{algorithm}[H]
\caption*{my algorithm}
\begin{algorithmic}
    \STATE \textbf{Stage one:} this is stage one
    \bindent
    \FORALL{i}
        \STATE do something
    \ENDFOR
    \eindent
    \STATE \textbf{Stage two:} this is stage two
    \bindent
    \STATE Update the trie: 
    \FORALL{j}
    \STATE do something
    \ENDFOR
    \eindent
\end{algorithmic}
\end{algorithm}

\end{document}

insira a descrição da imagem aqui

Alguns comentários ao código:

\newlength\myindent % define a new length \myindent
\setlength\myindent{6em} % assign the length 2em to \myindet
\newcommand\bindent{%
  \begingroup % starts a group (to keep changes local)
  \setlength{\itemindent}{\myindent} % set itemindent (algorithmic internally uses a list) to the value of \mylength
  \addtolength{\algorithmicindent}{\myindent} % adds \mylength to the default indentation used by algorithmic
}
\newcommand\eindent{\endgroup} % closes a group

Responder2

Definir:

\algdef{SE}[SUBALG]{Indent}{EndIndent}{}{\algorithmicend\ }%
\algtext*{Indent}
\algtext*{EndIndent}

Então, no bloco algorítmico, escreva:

\begin{algorithmic}[1]
    \State Outside indent block
    \Indent
         \State Inside indent block
    \EndIndent
\end{algorithmic}

Responder3

O problema da resposta de Gonzalo é com a numeração, pois os números também são recuados.

Encontrou uma solução melhor e também mais simples para \algorithmico ambiente embutidoALC@g

\begin{ALC@g}
   % Indent what you need
\end{ALC@g}

Para compará-lo no mesmo documento

\newlength\myindent
\setlength\myindent{2em}
\newcommand\bindent{%
    \begingroup
    \setlength{\itemindent}{\myindent}
    \addtolength{\algorithmicindent}{\myindent}
}
\newcommand\eindent{\endgroup}

\begin{algorithmic}[1]
    \STATE \textbf{Gonsalo's answer}
    \bindent
    \STATE First
    \STATE Second 
    \eindent
    \STATE \textbf{Proposed answer}

    \begin{ALC@g}
        \STATE First
        \STATE Second 
    \end{ALC@g}
    \STATE Something else
\end{algorithmic}

E resultado

Teste

Claro que você pode incluir solução para recuo maior \addtolengthdentro \setlengthda minha proposta.

Responder4

Se você usar o algorithm2epacote, a resposta será \Indpe \Indm. O primeiro cria recuo e o segundo cria recuo negativo, removendo assim o criado anteriormente. Veresta respostapara mais detalhes.

informação relacionada