
Я используюалгоритм2eпакет для псевдокода. У меня есть несколько очень длинных строк, которые нужно было перенести. Есть ли способ сделать отступ для следующих (разорванных) строк и/или поставить метку в первой строке, чтобы обозначить, что произошел перенос строки (похоже нарешение для листингового пакета)
Я нашел похожеевопросдля пакета листингов, на который уже был дан ответ, но я ищу конкретноалгоритм2eрешение.
решение1
Следующий минимальный пример определяет
\nosemic
: удалить печать символа конца строки (EOL) (обычно;
);\dosemic
: восстановить печать символа EOL;\pushline
: отступ строки1em
(типичный отступ); предоставляетсяalgorithm2e
как\Indp
;\popline
: удалить отступ\pushline
(удалить отступ?); предоставленоalgorithm2e
как\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}
Поскольку я не знаком с вашимalgorithm2e
настройка, я не уверен, как вы хотели бы объединить четыре команды. Поэтому я оставил их раздельными для максимального (но ручного) использования.
решение2
На самом деле есть более простое решение: использовать классические \hangafter
и \hangindent
. Это прекрасно работает с algorithm2e
.
Примечания:
\skiptext
— длина отступа текста в алгоритме для нового блока; поэтому я выбираю 1/2 этого отступа;нет необходимости переопределять внутренний макрос, так
\algocf@endline
как почти все можно переопределить вalgorithm2e
. Используйте\SetEndCharOfAlgoLine
для переопределения конца строки
Ниже приведен пример использования \hangafter
and \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}
решение3
\Indp
и \Indm
может использоваться в пакете algorithm2e
для отступа и удаления отступа. Я это проверил.