3

I would like to write a tiny comment in algorithm2e. I typed

\tcp*[l]{ {\tiny Here is my comment.} }

My problem is that the symbol // right before the comment is large. I tried to make it tiny but I could not. I typed

\SetKwComment{tcp}{ {\tiny \\} }{}

but I get the error ! LaTeX Error: Something's wrong--perhaps a missing \item.

How can I make the symbol \\ tiny?

As an alternative solution, I used the number sign symbol # instead of \\ by typing

\SetKwComment{tcp}{ {\tiny \#} }{}

and it worked!

2

2 Answers 2

3

From revision 2.40 of algorithm2e, it provides \SetKwComment{<cmd>}{<before>}{<after>} which creates the command <cmd>, setting some content <before> and some <after> the comment. You can redefine the way the existing macros function using the above. Here's how \tcp is defined originally:

\SetKwComment{tcp}{// }{}%

Instead one can use

\SetKwComment{tcp}{\tiny // }{}%

If you further want the entire comment to be set in \tiny, you'll have to add

\SetCommentSty{tiny}

Here's a complete example, taken from the algorithm2e documentation:

enter image description here

\documentclass{article}

\usepackage{algorithm2e}

\SetKwComment{tcp}{\tiny // }{}%
\SetCommentSty{tiny}

\begin{document}

\IncMargin{1em}
\begin{algorithm}
  \SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
  \SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
  \SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
  \Input{A bitmap $Im$ of size $w\times l$}
  \Output{A partition of the bitmap}
  \BlankLine
  \emph{special treatment of the first line}\;
  \For{$i\leftarrow 2$ \KwTo $l$}{
    \emph{special treatment of the first element of line $i$}\;
    \For{$j\leftarrow 2$ \KwTo $w$}{\label{forins}
      \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
      \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\;
      \This$\leftarrow$ \FindCompress{$Im[i,j]$}\;
      \If(\tcp*[h]{O(\Left,\This)==1}){\Left compatible with \This}{\label{lt}
        \lIf{\Left $<$ \This}{\Union{\Left,\This}}
        \lElse{\Union{\This,\Left}}
      }
      \If(\tcp*[f]{O(\Up,\This)==1}){\Up compatible with \This}{\label{ut}
        \lIf{\Up $<$ \This}{\Union{\Up,\This}}
          \tcp{\This is put under \Up to keep tree as flat as possible}\label{cmt}
        \lElse{\Union{\This,\Up}}\tcp*[h]{\This linked to \Up}\label{lelse}
      }
    }
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}}
    }
  \caption{disjoint decomposition}\label{algo_disjdecomp}
\end{algorithm}
\DecMargin{1em}

\end{document}
1

You need to provide \\ as a string, not \\, otherwise it would be interpreted as \newline instruction, causing the error about Missing item

\documentclass{article}
\usepackage[lined,boxed,commentsnumbered]{algorithm2e}

\SetKwComment{tcp}{\tiny \string\\}{}
\begin{document}
\begin{algorithm}
\tcp*[l]{ {\tiny Here is my comment.} }
\end{algorithm}
\end{document}

enter image description here

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