0

I need to color part of an algorithm with \textcolor. I have the following code using the algorithmic package:

\usepackage{algorithmic}
\usepackage{xcolor}
...
\begin{algorithmic}    
    \FOR{$s = 1, \dots, S$}
    \textcolor{red} {
    \IF {$t<T'$} 
        \STATE do foo
    \ELSE 
        \STATE do bar
    \ENDIF
    }
    \ENDFOR
\end{algorithmic}

However this produces an error: Something's wrong--perhaps a missing \item. and the indentation of the IF statement is broken:

enter image description here

I can eliminate the error with a \STATE command before the \textcolor command like so:

\begin{algorithmic}    
    \FOR{$s = 1, \dots, S$}
    \STATE \textcolor{red} {
    \IF {$t<T'$} 
        \STATE do foo
    \ELSE 
        \STATE do bar
    \ENDIF
    }
    \ENDFOR
\end{algorithmic}

But this injects an extra empty line into the algorithm:

enter image description here

What is the correct way to selectively color parts of an algorithm (or perhaps a subset of items in a list in general?)

1
  • Welcome to TeX.SE!
    – Mensch
    Commented Mar 13, 2023 at 17:51

1 Answer 1

1

Instead of \textcolor, which is more appropriate for short sequences of text, you could use {\color{red} ... }:

\documentclass{article}

\usepackage{algorithmic}
\usepackage{xcolor}
\begin{document}
\begin{algorithmic}    
    \FOR{$s = 1, \dots, S$}
    {\color{red}
    \IF {$t<T'$} 
        \STATE do foo
    \ELSE 
        \STATE do bar
    \ENDIF
    }
    \ENDFOR
\end{algorithmic}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .