Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The fancyvrb allows me to customize line number style which I find very useful. I'm using it to give line numbers a different color and then use that same color in the text describing the code. I believe this makes it easier to locate the text describing a particular image.

Colored line numbers example.

Is there a way to do the same thing for pseudo code listings using algorithmicx?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

You need to redefine what \alglinenumber does. More specifically, use something like

\algrenewcommand{\alglinenumber}[1]{\color{<color>}\footnotesize#1:}

The following example was taken from the algorithmicx documentation:

enter image description here

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algpseudocode
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\begin{document}
\begin{algorithm}
  \caption{Euclid’s algorithm}\label{euclid}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
      \State $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
      \EndWhile
      \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
  \end{algorithmic}

  \bigskip

\algrenewcommand{\alglinenumber}[1]{\color{red!80!blue}\footnotesize#1:}

  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
      \State $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
      \EndWhile
      \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
  \end{algorithmic}

\end{algorithm}
\end{document}

Note that \alglinenumber prints the entire line number construction, including the ending :. You can modify this to suit, if you don't want the separator.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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