TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the algorithm2e package for pseudocode. I got some very long lines that needed to be wrapped. Is there a way to indent the following (breaked) lines and/or put a mark at the first line to signalize that there happened a linebreak (similar to solution for listings package)

I found a similar question for the listings package, that was answered before, but I'm looking explicitly for an algorithm2e solution.

share|improve this question
    
See also the related question if you want to skip line numbers for manually wrapped lines. – M.S. Dousti Jan 14 '14 at 22:04
up vote 8 down vote accepted

The following minimal example defines

  • \nosemic: remove printing of the end-of-line (EOL) character (typically ;);
  • \dosemic: reinstate printing of the EOL character;
  • \pushline: indents line by 1em (the typical indent); provided by algorithm2e as \Indp;
  • \popline: remove indent of \pushline (undent?); provided by algorithm2e as \Indm

enter image description here

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

Since I am unfamiliar with your algorithm2e setup, I'm not sure how you would like to combine the four commands. As such, I've kept them separate for maximum (yet manual) usage.

share|improve this answer

In fact there is a simpler solution: use classical \hangafter and \hangindent. This works fine with algorithm2e.

Notes:

  1. \skiptext is the length of text indent in algorithm for a new block; so I choose 1/2 of this indent;

  2. no need to redefine internal macro as \algocf@endline as almost all can be redefined in algorithm2e. Use \SetEndCharOfAlgoLine to redefine end of line

See below for an example of use of \hangafter and \hangindent:

enter image description here

\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}​
share|improve this answer
    
Welcome to TeX - LaTeX! – Werner Oct 11 '15 at 6:26

\Indp and \Indm can be used in the package algorithm2e for indent and removing indent. I have tested it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.