0

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?

1
  • 1
    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
1

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}

\newcommand{\StateRed}[1]{\algrenewcommand{\alglinenumber}[1]{\footnotesize\textcolor{red}{##1}:}\State #1}
\newcommand{\StateBlack}[1]{\algrenewcommand{\alglinenumber}[1]{\footnotesize##1:}\State #1}

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

enter image description here

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

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.