Quebra de linha no algoritmo de pacote2e

Quebra de linha no algoritmo de pacote2e

estou usando oalgoritmo2epacote para pseudocódigo. Eu tenho algumas filas muito longas que precisavam ser embrulhadas. Existe uma maneira de recuar as linhas seguintes (quebradas) e/ou colocar uma marca na primeira linha para sinalizar que ocorreu uma quebra de linha (semelhante asolução para pacote de listagens)

encontrei um semelhanteperguntapara o pacote de listagens, que foi respondido antes, mas estou procurando explicitamente por umalgoritmo2esolução.

Responder1

O exemplo mínimo a seguir define

  • \nosemic: remove a impressão do caractere de fim de linha (EOL) (normalmente ;);
  • \dosemic: restabelecer a impressão do caractere EOL;
  • \pushline: recuos linha por 1em(o recuo típico); fornecido por algorithm2ecomo \Indp;
  • \popline: remover recuo de \pushline(recuar?); fornecido por algorithm2ecomo\Indm

insira a descrição da imagem aqui

\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 não estou familiarizado com o seualgorithm2esetup, não tenho certeza de como você gostaria de combinar os quatro comandos. Como tal, mantive-os separados para uso máximo (embora manual).

Responder2

Na verdade, existe uma solução mais simples: use o clássico \hangaftere o \hangindent. Isso funciona bem com algorithm2e.

Notas:

  1. \skiptexté o comprimento do recuo do texto no algoritmo para um novo bloco; então escolho 1/2 deste recuo;

  2. não há necessidade de redefinir a macro interna, pois \algocf@endlinequase todas podem ser redefinidas em algorithm2e. Use \SetEndCharOfAlgoLinepara redefinir o fim da linha

Veja abaixo um exemplo de uso de \hangaftere \hangindent:

insira a descrição da imagem aqui

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

Responder3

\Indpe \Indmpode ser usado no pacote algorithm2epara recuo e remoção de recuo. Eu testei.

informação relacionada