Sangrar líneas de código en algoritmo

Sangrar líneas de código en algoritmo

Estoy escribiendo un algoritmo que tiene dos etapas. ¿Cómo puedo sangrar el código para la etapa uno y la etapa dos?

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

Respuesta1

En el siguiente código de ejemplo, definí dos nuevos comandos que le permiten cambiar la sangría; simplemente incluya el fragmento deseado usando \bindent, \eindent; la longitud \myindentcontrola la cantidad de sangría:

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

ingrese la descripción de la imagen aquí

Algunos comentarios al 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

Respuesta2

Definir:

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

Luego en el bloque algorítmico, escribe:

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

Respuesta3

El problema de la respuesta de Gonzalo es la numeración, ya que los números también tienen sangría.

Encontré una solución mejor y también más sencilla para \algorithmicel entorno integrado.ALC@g

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

Para compararlo en el mismo 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}

y resultado

Prueba

Por supuesto, puede incluir una solución para una sangría más grande dentro \addtolengthy \setlengthdentro de mi propuesta.

Respuesta4

Si usa el algorithm2epaquete, la respuesta es \Indpy \Indm. El primero crea sangría y el segundo crea sangría negativa, eliminando así la creada previamente. Veresta respuestapara más detalles.

información relacionada