Перенос строки в пакете algorithm2e

Перенос строки в пакете algorithm2e

Я используюалгоритм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.

Примечания:

  1. \skiptext— длина отступа текста в алгоритме для нового блока; поэтому я выбираю 1/2 этого отступа;

  2. нет необходимости переопределять внутренний макрос, так \algocf@endlineкак почти все можно переопределить в algorithm2e. Используйте \SetEndCharOfAlgoLineдля переопределения конца строки

Ниже приведен пример использования \hangafterand \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для отступа и удаления отступа. Я это проверил.

Связанный контент