# Algorithms: change the style of individual line numbers

I would like to change the style of individual lines in an algorithm. For example, I'd like surround key line numbers with a box, color them, or otherwise distinguish them.

It is possible to change the style of all the line numbers with algorithm2e, as detailed here (and in the manual): Algorithm2e modify line numbers

Is there a way to modify the style of an arbitrary subset of the lines?

• From what I remember from the answer to this question, it's not really easy… But you could try to build on that! – Vser Feb 15 '16 at 19:20

Building on this answer this answer, this can be accomplished by updating the \alglinenumber command, as follows. This isn't yet an optimal solution, as demonstrated by the last line.

\documentclass{article}
\usepackage{algpseudocode}
\usepackage{xcolor}


\begin{document}
\begin{algorithmic}[1]
\StateRed $x = 1$
\StateBlack $y = 2$
\StateRed $z = 3$
\State you probably didn't want this to be red!
\end{algorithmic}
\end{document}


This also doesn't work perfectly when adding annotations of non-zero width to the line numbers, as the spacing is affected when going from line 9 to 10 (and presumably 99 to 100, though I have not checked).