# Include a line break in algorithmic while maintaining indentation

I have this code:

\DeclareCaptionFormat{algor}{%
\hrulefill\par\offinterlineskip\vskip1pt%
\textbf{#1#2}#3\offinterlineskip\hrulefill}
\DeclareCaptionStyle{algori}{singlelinecheck=off,format=algor,labelsep=space}
\captionsetup[algorithm]{style=algori}

\begin{document}
\section{Struttura dati}
\begin{floatleft}
\captionof{algorithm}{Leggi file .gpx}\label{getgpx}
\begin{algorithmic}[1]
\State $speed \gets computeSpeed(\linebreak gpx.track(i).segment(j).delta\_s(q), \linebreak gpx.track(i).segment(j).delta\_t(q));$
\end{algorithmic}
\end{floatleft}
\end{document}


This is how code is shown:

I want the lines gpx.track....delta_s(q),, gpx.track....delta_t(q), and ); to be indented exactly as the beginning of line 27 is. I've tried using the \indent command but the indentation space is not the same as in the rest of the document and the final result is chaotic.

-
It is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages. – Peter Grill Nov 7 '11 at 16:51
I have the same problem, and the solution by Werner is too cumbersome to use. So, I start a bounty. – Boris Bukh Dec 16 '11 at 18:33
@BorisBukh: I've added an alternative that might be more suitable and hopefully less cumbersome. – Werner Dec 16 '11 at 20:38

The default indent for each block in the algorithmic environment (from the algorithmicx package) is \algorithmicindent. As such, you can place the entire line of code in a top-aligned \parbox[t] of adequate width or use the varwidth environment from the varwidth package, and indent as needed:

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\begin{document}
\section{Struttura dati}
\begin{algorithm}
\caption{Leggi file .gpx}\label{getgpx}
\begin{algorithmic}[1]
\State \begin{varwidth}[t]{\linewidth}
speed~$\gets$~computeSpeed(\par
\hskip\algorithmicindent gpx.track(i).segment(j).delta\_s(q),\par
\hskip\algorithmicindent gpx.track(i).segment(j).delta\_t(q));
\end{varwidth}
\end{algorithmic}
\end{algorithm}
\end{document}​


Here is another alternative that could be used instead. It utilized the a modified version of \Statex, also supplied by algorithmicx. It now takes an optional argument indicating the number of indents to apply to the specific line, without numbering it (default for \Statex).

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\makeatletter
\let\OldStatex\Statex
\renewcommand{\Statex}[1][3]{%
\setlength\@tempdima{\algorithmicindent}%
\OldStatex\hskip\dimexpr#1\@tempdima\relax}
\makeatother
\begin{document}
\section{Struttura dati}
\begin{algorithm}
\caption{Leggi file .gpx}\label{getgpx}
\begin{algorithmic}[1]
\State speed~$\gets$~computeSpeed(
\Statex gpx.track(i).segment(j).delta\_s(q),
\Statex[2] gpx.track(i).segment(j).delta\_t(q));
\end{algorithmic}
\end{algorithm}
\end{document}


Note that I've stripped the unnecessary preamble content from the minimal working example (MWE) posted above. This is encouraged when posting problems/question - something that can compile that reproduces the problem.

-
It works. But it's normal that compiler gave me again badbox errror, even if line is perfectly bounded? – Mariano Nov 7 '11 at 22:38
What did you specify as the width <len> for your \parbox{<len>}{...}? Note that this quantity should be less than what is available on the line. – Werner Nov 7 '11 at 22:39
@Werner: I'm having a similar issue, but this solution is less than perfect, since you have to add this parbox for every line that doesn't fit into the line width. Plus, you have to know the line width to specify for the parbox - which may be ok for a piece of code, but not ok for text which needs to be justified. – einpoklum Nov 23 '11 at 18:42
@Werner Why not using \begin{varwidth}[t]{\linewidth} instead of \parbox? Of course \usepackage{varwidth} is needed. – egreg Dec 16 '11 at 18:43
@Werner: you rang? – einpoklum Dec 16 '11 at 20:26
show 5 more comments

I had a similar problem, except that my long expression was the condition of an If statement. I fixed it by using a LaTeX array environment for my expression, the output looks a little strange because it does not look like idomatic pesudocode.

\If{$\left( \begin{array}{l} (current\_context \neq \text{NULL})\, \vee \\ (MR\_num\_outstanding\_contexts < MR\_max\_contexts) \\ \end{array} \right)$}


If the math-style output is acceptable then this is pretty easy.

-

This is an addition to Werner's answer. As noted in the comments, that solution does not work with \For and \While loops since they include a "do" at the end. One can define alternative versions of \For and \While as in the example below

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\makeatletter
\newcommand{\StatexIndent}[1][3]{%
\setlength\@tempdima{\algorithmicindent}%
\Statex\hskip\dimexpr#1\@tempdima\relax}
\algdef{S}[WHILE]{WhileNoDo}[1]{\algorithmicwhile\ #1}%
\makeatother
\begin{document}
\begin{algorithm}[H]
\caption{Cool algorithm}
\begin{algorithmic}[1]
\WhileNoDo{everything is all right, and}
\StatexIndent[2] the sun is shining bright \algorithmicdo
\State Sing and dance
\EndWhile
\end{algorithmic}
\end{algorithm}
\end{document}


I hope it is helpful to someone else too.

-