4

I'd like to increase the amount of horizontal space that after comes each line number in my algorithm (typeset with algpseudocode).

enter image description here

Because algorithmicx in general, and algpseudocode in particular, don't seem to provide any means of such customisation to the user, I rolled up my sleeves and got ready to get my hands dirty: I looked up the source code of algpseudocode and found the following definition (buried in another macro definition, hence the double #):

\algnewcommand\alglinenumber[1]{\footnotesize ##1:}

In order to get a bit more space, I redefined \alglinenumber as follows:

\algrenewcommand\alglinenumber[1]{\footnotesize #1:\hspace{1em}}

This seemed to do the trick, but I've noticed that

  1. lines numbers are no longer flushed right,
  2. line contents get pushed to the right when the number of digits in line numbers increases; as a result, lines labelled by a two-digit number are not horizontally aligned with lines labelled by a one-digit number.

    This undesired behaviour is clearly visible on the screenshot below; see the additional, undesired indent from line 10 onwards, compared to the preceding lines.

    enter image description here

Because this does not happen with the original definition of \alglinenumber (see the first screenshot), I can only conclude that my redefinition is inappropriate.

What should I do to add a bit more of horizontal space after the line number but preserve horizontal alignment?

\documentclass{article}

\usepackage{algpseudocode}
\algrenewcommand\alglinenumber[1]{\footnotesize #1:\hspace{1em}} % comment/uncomment

\begin{document}
\begin{algorithmic}[1]
\State aaa
\State bbb
\State ccc
\State ddd
\State eee
\State fff
\State ggg
\State hhh
\State iii
\State jjj
\State kkk
\State lll
\end{algorithmic}
\end{document}
0
5

You need to alter the value of \labelsep for the list. The default is 0.5em. This changes that to 1.5em:

\documentclass{article}
\usepackage{regexpatch}
\usepackage{algpseudocode}
\makeatletter
\xpatchcmd{\algorithmic}{\labelsep 0.5em}{\labelsep 1.5em}{\typeout{Success!}}{\typeout{Oh dear!}}
\makeatother
\begin{document}
  \begin{algorithmic}[1]
    \State aaa
    \State bbb
    \State ccc
    \State ddd
    \State eee
    \State fff
    \State ggg
    \State hhh
    \State iii
    \State jjj
    \State kkk
    \State lll
    \State aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaa
  \end{algorithmic}
\end{document}

greater label sep

0

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.