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.

In a tabular environment, one can achieve an alternating color of rows by using the rowcolors command like so:

\documentclass[11pt,a4paper]{article}
\usepackage[table]{xcolor}  

\begin{document}
\rowcolors{2}{gray!25}{white}
\begin{tabular}{cc}
        \rowcolor{gray!50}
        Table head & Table head \\
        Some values & Some values \\
        Some values & Some values \\
        Some values & Some values \\
        Some values & Some values
\end{tabular}
\end{document}

This will produce:

enter image description here

How can I have a similar effect inside the algorithmic environment? I'm using the algorithmicx package.

EDIT: A MWE of a how I use algorithmicx follows.

\documentclass[a4paper]{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}    
\begin{algorithm}
     \begin{algorithmic}[1] % [1] provides numbers in every line
          \If {$i\geq maxval$}
          \State $i\gets 0$
          \Else
          \If {$i+k\leq maxval$}
          \State $i\gets i+k$
          \EndIf
          \EndIf
     \end{algorithmic}
     \caption{My algorithm}
\end{algorithm}
\end{document}

This yields:

enter image description here

share|improve this question
    
See tex.stackexchange.com/q/64674/27635. –  karlkoeller Aug 14 '13 at 6:36
    
@karlkoeller I saw that, I was hoping for something less involved, as I don't want to use color on specific rows, only to alternate it.. –  geo909 Aug 14 '13 at 7:55
    
@karlkoeller That question and its answers are quite different, actually. –  geo909 Aug 14 '13 at 8:00
add comment

1 Answer

up vote 3 down vote accepted

enter image description here

\documentclass[a4paper]{article}
\usepackage{color}
\usepackage{algorithm}
\usepackage{algpseudocode}


\renewcommand\alglinenumber[1]%
 {\ifodd\value{ALG@line}\relax
  \noindent{\color{yellow}\leaders\vrule\hskip\linewidth\hbox{}\hskip-\linewidth\hbox{}}%
  \fi
\arabic{ALG@line}}


\begin{document}    
\begin{algorithm}
     \begin{algorithmic}[1] % [1] provides numbers in every line
          \If {$i\geq maxval$}
          \State $i\gets 0$
          \Else
          \If {$i+k\leq maxval$}
          \State $i\gets i+k$
          \EndIf
          \EndIf
     \end{algorithmic}
     \caption{My algorithm}
\end{algorithm}
\end{document}
share|improve this answer
1  
\footnotesize\arabic{ALG@line}: is more appropriate than \arabic{ALG@line} –  karlkoeller Aug 23 '13 at 11:16
    
@karlkoeller er yes. –  David Carlisle Aug 23 '13 at 11:32
add comment

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.