
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 por1em
(la típica sangría); proporcionado poralgorithm2e
como\Indp
;\popline
: eliminar sangría de\pushline
(¿sin sangría?); proporcionado 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 no estoy familiarizado con tualgorithm2e
setup, 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 \hangafter
y \hangindent
. Esto funciona bien con algorithm2e
.
Notas:
\skiptext
es la longitud de la sangría del texto en el algoritmo para un nuevo bloque; entonces elijo la mitad de este guión;No es necesario redefinir la macro interna, ya
\algocf@endline
que casi todas se pueden redefinir enalgorithm2e
. Usar\SetEndCharOfAlgoLine
para redefinir el final de la línea
Vea a continuación un ejemplo de uso de \hangafter
y \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}
Respuesta3
\Indp
y \Indm
se puede utilizar en el paquete algorithm2e
para sangrar y eliminar sangrías. Lo he probado.