# Indentation in algorithmic package when having multiple lines

I'm using algorithmic package and I've got this code:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithmic}[1]
\State $i \leftarrow 0, f \leftarrow \sqrt[T-1]{\frac{\left | P \right |}{\left | N \right |}}$, some very very very very very very very very very very very very very very very very long goes here, so this will create a few new lines
\EndFunction
\end{algorithmic}
\end{document}


And I've got the following result: Is it there a way to indent the new lines to be under the start of the first one?

Update: If I do the solution proposed by @Bobyandbob the lines get too closer: Theres no space between lines and end function.

• Welcome to TeX.SX! So the gap/indent in line 2 is should be smaller? – Bobyandbob May 22 '18 at 17:08
• Hi @Bobyandbob - No, the gap/indent in line 3 and 4 (talking about real lines) should be bigger to start in the same point as lines 2. – Néstor May 22 '18 at 17:10
• Please add a minimal working example (MWE), that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass and ending with \end{document}. MAybe related Indenting lines of code in algorithm – Bobyandbob May 22 '18 at 17:16
• You could add \usepackage{varwidth} and try \State \begin{varwidth}[t]{\linewidth} $i \leftarrow 0, f \leftarrow \sqrt[T-1]{\frac{\left | P \right |}{\left | N \right |}}$, some very very very very very very very very very very very very very very very very long goes here, so this will create a few new lines \end{varwidth} ... solution from Include a line break in algorithmic while maintaining indentation – Bobyandbob May 22 '18 at 17:25
• @Bobyandbob if I do that, the lines get too closer, see the original question. – Néstor May 22 '18 at 17:32

First I show what you had, then I show it in a \parbox, trying to use some horizontal list lengths to get the width right (it is close, but not quite right). Finally, I again use a \parbox, but specify the width manually to match the right margin.

To get the proper vertical placement and spacing, I had to use the [t] option of \parbox and also end it with a \strut.

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithmic}[1]
\State $i \leftarrow 0, f \leftarrow \sqrt[T-1]{\frac{\left | P \right |}{% \left | N \right |}}$, some very very very very very very very very very
very very very very very very very long goes here, so this will create a
few new lines
\EndFunction

\State \parbox[t]{\dimexpr\textwidth-\leftmargin-\labelsep-\labelwidth}{%
$i \leftarrow 0, f \leftarrow \sqrt[T-1]{\frac{\left | P \right |}{% \left | N \right |}}$, some very very very very very very very very very
very very very very very very very long goes here, so this will create a
few new lines\strut}
\EndFunction

$i \leftarrow 0, f \leftarrow \sqrt[T-1]{\frac{\left | P \right |}{% \left | N \right |}}$, some very very very very very very very very very

• @Néstor If I take my MWE and change article to book, it works out very similarly. However, as I noted, I don't think my "calculation" of \dimexpr\textwidth-\leftmargin-\labelsep-\labelwidth is precise. The good news is, if your margins are different, I think you only have to manually figure out a number like 313pt (in my example) once, and then use the same number throughout your document. – Steven B. Segletes May 22 '18 at 17:54