[Tex/LaTex] algorithm2e comment style

algorithm2ealgorithms

I wanted to know how to change the comment style in the algorithm2e package. I want to make my comments of a smaller font and a different color:

\documentclass{article}

\usepackage{color}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}

\begin{document}

\begin{algorithm}[H]
\DontPrintSemicolon
\CommentSty{\color{blue}}
  \KwData{Training set $x$}
  $\Delta_{ji}^l := 0$ \tcp*{will be used to compute $\partial x$}
  \tcc{iterate over all training examples}
\caption{Example code}
\end{algorithm}

\end{document}

Best Answer

You need to define a command with an argument giving the font specification and then use the name of this command in the argument for \SetCommentSty:

\documentclass{article}
\usepackage{xcolor}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}

\begin{document}

\begin{algorithm}[H]
\DontPrintSemicolon
  \KwData{Training set $x$}
  $\Delta_{ji}^l := 0$ \tcp*{will be used to compute $\partial x$}
  \tcc{iterate over all training examples}
\caption{Example code}
\end{algorithm}

\end{document}

enter image description here