Salto de línea en el algoritmo de paquete2e

Salto de línea en el algoritmo de paquete2e

estoy usando elalgoritmo2epaquete para pseudocódigo. Tengo algunas líneas muy largas que necesitaban ser envueltas. ¿Hay alguna manera de sangrar las siguientes líneas (quebradas) y/o poner una marca en la primera línea para indicar que ocurrió un salto de línea (similar apaquete de solución para listados)

encontré uno parecidopreguntapara el paquete de listados, eso fue respondido antes, pero estoy buscando explícitamente unaalgoritmo2esolución.

Respuesta1

El siguiente ejemplo mínimo define

  • \nosemic: elimina la impresión del carácter de fin de línea (EOL) (normalmente ;);
  • \dosemic: restablecer la impresión del carácter EOL;
  • \pushline: sangrías línea por 1em(la típica sangría); proporcionado por algorithm2ecomo \Indp;
  • \popline: eliminar sangría de \pushline(¿sin sangría?); proporcionado por algorithm2ecomo\Indm

ingrese la descripción de la imagen aquí

\documentclass{article}
\usepackage{algorithm2e}% http://ctan.org/pkg/algorithm2e
\makeatletter
\newcommand{\nosemic}{\renewcommand{\@endalgocfline}{\relax}}% Drop semi-colon ;
\newcommand{\dosemic}{\renewcommand{\@endalgocfline}{\algocf@endline}}% Reinstate semi-colon ;
\newcommand{\pushline}{\Indp}% Indent
\newcommand{\popline}{\Indm\dosemic}% Undent
\makeatother
\begin{document}
\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
    initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      \nosemic this is a very long statement that has to be\;
        \pushline\dosemic wrapped over two lines\;
      \popline current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}
\end{document}​

Como no estoy familiarizado con tualgorithm2esetup, no estoy seguro de cómo le gustaría combinar los cuatro comandos. Como tal, los he mantenido separados para un uso máximo (aunque manual).

Respuesta2

De hecho, existe una solución más sencilla: utilizar el clásico \hangaftery \hangindent. Esto funciona bien con algorithm2e.

Notas:

  1. \skiptextes la longitud de la sangría del texto en el algoritmo para un nuevo bloque; entonces elijo la mitad de este guión;

  2. No es necesario redefinir la macro interna, ya \algocf@endlineque casi todas se pueden redefinir en algorithm2e. Usar \SetEndCharOfAlgoLinepara redefinir el final de la línea

Vea a continuación un ejemplo de uso de \hangaftery \hangindent:

ingrese la descripción de la imagen aquí

\documentclass{article}
\usepackage{algorithm2e}% http://ctan.org/pkg/algorithm2e
%
\newcommand{\nosemic}{\SetEndCharOfAlgoLine{\relax}}% Drop semi-colon ;
\newcommand{\dosemic}{\SetEndCharOfAlgoLine{\string;}}% Reinstate
\newcommand{\pushline}{\Indp}% Indent
\newcommand{\popline}{\Indm\dosemic}% Undent
%
\begin{document}
\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
    initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      \nosemic this is a very long statement that has to be\;
        \pushline\dosemic wrapped over two lines\;
      \popline current section becomes this one\;
      \hangindent=.5\skiptext\hangafter=1
      this is a very long statement that should be indented automatically on the next
      lines if this statement is longer than one line\;
      Following lines should be indented normally\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}
\end{document}​

Respuesta3

\Indpy \Indmse puede utilizar en el paquete algorithm2epara sangrar y eliminar sangrías. Lo he probado.

información relacionada