algorithm2e 注释样式

algorithm2e 注释样式

我想知道如何在 algorithm2e 包中更改注释样式。我想让我的注释的字体更小,颜色也不同:

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

答案1

您需要定义一个带有提供字体规范的参数的命令,然后在参数中使用该命令的名称\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}

在此处输入图片描述

答案2

这个答案与@GonzaloMedina 提供的答案类似,但在评论中调整了空间。

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

在此处输入图片描述

相关内容