# algorithm2e comment style

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
\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}


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}}

\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}


• Hi @GonzaloMedina, when I try \tcp it adds a new line after the code comment, so my code skips a line. Why is this? I am using algorithm2e and the line looks like: set $cost\_vector \; \vv{c_{p,v}} = \left\lbrace \right\rbrace$\tcp*{will be used to compute $\partial x$}\; – Dhruv Ghulati Sep 9 '16 at 16:20
• How to reduce space between examples and */? – Mr.EU Jul 1 '17 at 10:16

This answer is similar to the one provided by @GonzaloMedina with space adjusted within comments.

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

\begin{document}

\begin{algorithm}[h]
\DontPrintSemicolon
\SetAlgoLined
\SetNoFillComment
\LinesNotNumbered
%\SetSideCommentLeft
\tcc{iterate over all training examples}
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current \tcp*[l]{will be used to compute $\partial x$}
\eIf{understand}{
go to next section \tcp*{will be used to compute $\partial x$}
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}

\end{document}