2

By default, the line numbers in algorithmicx are inside the float rules.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithm, algpseudocode}

\begin{document}
\begin{algorithm}
  \begin{algorithmic}[1]
    \Procedure{Input1}{$s_0 \ldots s_7, c$}
    \For{$i \gets 0$ to 7}
    \State $arr[i][0] \gets s_i$
    \EndFor
    \State $color \gets c$
    \EndProcedure
  \end{algorithmic}
\end{algorithm}
\end{document}

enter image description here

Is it possible to change their indentation to make them display outside the float rules, like in the listings package? (Details omitted in picture) If the solution would change the indentation of the pseudocode as well so that they would be indented where the line numbers originally were, that would be great. enter image description here

3

Add the following lines below \usepackage{algorithmicx} (or \usepackage{algpseudocode} in your case).

\usepackage{xpatch}
\xpatchcmd\algorithmic{\leftmargin\labelwidth}{\leftmargin-\labelsep}{}{}

enter image description here

\documentclass{article}
\usepackage{blindtext}
\usepackage{algorithm,algpseudocode}
\usepackage{xpatch}
\xpatchcmd\algorithmic{\leftmargin\labelwidth}{\leftmargin-\labelsep}{}{}
\begin{document}
\blindtext
\begin{algorithm}
  \begin{algorithmic}[1]
    \Procedure{Input1}{$s_0 \ldots s_7, c$}
    \For{$i \gets 0$ to 7}
    \State $arr[i][0] \gets s_i$
    \EndFor
    \State $color \gets c$
    \EndProcedure
  \end{algorithmic}
\end{algorithm}
\blindtext
\end{document}
  • Thank you! I'm very interested in understanding how this works. I know how \xpatchcmd works, but I don't fully understand the replaced/replacement code. I also don't know how to figure out that a solution is to patch the algorithmic command. May I request some links or insight on how the label manipulation works and how to arrive at this fix? – John Nov 26 '20 at 12:27
  • 1
    @John I checked the implementation of the algorithmic environment in algorithmicx.sty. There, I found that the indentation is hard-coded as \leftmargin\labelwidth \addtolength{\leftmargin}{\labelsep}. This means that one either has to redefine the whole environment with a slightly modified code, or patch it in a way that \leftmargin is 0pt in the end. There are several ways to do this. One could also replace \leftmargin\labelwidth \addtolength{\leftmargin}{\labelsep} by \leftmargin0pt. – gernot Nov 26 '20 at 14:15
  • 1
    @John Moreover, the code for \begin{env} is implemented by \env and the one for \end{env} by \endenv. – gernot Nov 26 '20 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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