[Tex/LaTex] Include a line break in algorithmic while maintaining indentation

algorithmsindentationline-breaking

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.

Best Answer

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:

enter image description here

\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).

enter image description here

\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.