My question goes in the same direction as this related one. I have already achieved some syntax highlighting in algorithm2e
by redefining \KwSty
, here is a MWE:
\documentclass{article}
% Math
\usepackage{amsmath}
\usepackage{amsfonts}
% Colors
\usepackage{xcolor}
\definecolor{mycolor}{RGB}{153, 0, 0}
% Algorithms
\usepackage{algorithm2e}
\renewcommand{\KwSty}[1]{\textnormal{\textcolor{mycolor}{\bfseries #1}}\unskip}
\begin{document}
\begin{algorithm}
\SetAlgoLined\DontPrintSemicolon
\ForEach{\(n \in \mathbb{K}\)}{
\If{\(n < \tau\)}{
\(print(\text{``Lower than''})\)\;
}
\Else{
\(print(\text{``Greater than equal''})\)\;
}
}
\end{algorithm}
\end{document}
which generates:
But I wonder if a more fine-grained control is possible, i.e., having different colors for, say, the if
and foreach
blocks. I could not find this information on the manual.