2

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

Moriambar
  • 11,466
geo909
  • 790

1 Answers1

3

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}
David Carlisle
  • 757,742