# break an long line in an algorithm

I have a long line in an algorithm and want to break it into two lines. For similar case in equations, split can be used. How do I do this within an algorithm?

\documentclass{article}
\usepackage{amsmath}
\usepackage[boxed,vlined,linesnumbered,noresetcount]{algorithm2e}
\begin{document}
\begin{algorithm}[tp]
out := foo(x$\rightarrow$child[which]
,$\langle$1,0,0,old$\rangle$
,$\langle$0,0,0,new$\rangle$)\;
\end{algorithm}
\end{document}


• Try \linebreak at the place you want to break the line. – Jesse Aug 5 '14 at 2:23
• the problem with linebreak is, it does not indent the next line. In my case that might be misleading. If the second line is indented inside, then it would be easy to understand that it is part of the previous line – arunmoezhi Aug 5 '14 at 2:24

## 3 Answers

Setting the breakable portion in a \parbox of sufficient width allows you to break it at will as well as properly align the content:

\documentclass{article}
\usepackage{mathtools}
\usepackage[boxed,vlined,linesnumbered,noresetcount]{algorithm2e}
\begin{document}
\begin{algorithm}[tp]
\texttt{out} $\vcentcolon=$ \textsc{foo}($\texttt{x} \rightarrow \texttt{child[which]}$, $\langle 1,0,0,\texttt{old} \rangle$, $\langle 0,0,0,\texttt{new} \rangle$\;
\texttt{out} $\vcentcolon=$ \textsc{foo}(\parbox[t]{.6\linewidth}{%
$\texttt{x} \rightarrow \texttt{child[which]}$, \\
$\langle 1,0,0,\texttt{old} \rangle$, \\
$\langle 0,0,0,\texttt{new} \rangle$)\;}
\end{algorithm}
\end{document}

• Or maybe varwidth in order to avoid enlarging the total width of the construction. – egreg Aug 5 '14 at 9:38

Something like this, with adjustable indentation. The solution defines a new command called \myindent constituted with \newline and \makebox{#1}{}, taking one argument for length.

Code

\documentclass{article}
\usepackage{amsmath}
\usepackage[boxed,vlined,linesnumbered,noresetcount]{algorithm2e}
\newcommand{\myindent}[1]{
\newline\makebox[#1cm]{}
}
\begin{document}
\begin{algorithm}[tp]
out := foo(x$\rightarrow$child[which]
\myindent{0.3},$\langle$1,0,0,old$\rangle$
\myindent{0.3},$\langle$0,0,0,new$\rangle$)\;
\end{algorithm}
\end{document}

• I figured out another way to do it using \Indp and \Indm. It looked good. But is that a right way to do it? – arunmoezhi Aug 5 '14 at 19:55
• A problem can be tackled in many ways. As long as it is in the realm of LaTeX, sure. – Jesse Aug 5 '14 at 23:18

You can define a new command \WRP like that:

\newcommand{\WRP}{\par\qquad$$\hookrightarrow$$\enspace}
...
\begin{document}
...
\begin{algorithm}[H]
\begin{algorithmic}[1]
...
\STATE This line\WRP needs to be wrapped


Syntax: \newcommand{\name}[args]{definition}

Note: Make sure the command isn't already defined.

Result: