
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 por1em
(o recuo típico); fornecido poralgorithm2e
como\Indp
;\popline
: remover recuo de\pushline
(recuar?); fornecido poralgorithm2e
como\Indm
\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 seualgorithm2e
setup, 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 \hangafter
e o \hangindent
. Isso funciona bem com algorithm2e
.
Notas:
\skiptext
é o comprimento do recuo do texto no algoritmo para um novo bloco; então escolho 1/2 deste recuo;não há necessidade de redefinir a macro interna, pois
\algocf@endline
quase todas podem ser redefinidas emalgorithm2e
. Use\SetEndCharOfAlgoLine
para redefinir o fim da linha
Veja abaixo um exemplo de uso de \hangafter
e \hangindent
:
\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
\Indp
e \Indm
pode ser usado no pacote algorithm2e
para recuo e remoção de recuo. Eu testei.