34

Is there a special formatting to add comments to code? I mean I want to show comments in pseudocode that I write in LaTeX.

this is code // this is comment part (the part that I want)

Edit: To be more clear, what I mean is in "algorithmic" syntax I write some pseudo code. However, I am not sure if there is a standard comment used in that context.

Werner
  • 603,163
polerto
  • 441

4 Answers4

26

Since you use the term algorithmic, I assume you're using the algorithms bundle (which provides the algorithmic package and environment). Then the \COMMENT{...} macro typesets a comment in pseudo-code:

enter image description here

\documentclass{article}
\usepackage{algorithmic}% http://ctan.org/pkg/algorithms
\begin{document}
\begin{algorithmic}[1]
  \STATE this is code \COMMENT{this is a comment}
\end{algorithmic}
\end{document}​

If you're using the (more advanced) algorithmicx package (and use \usepackage{algpseudocode}, which also provides an algorithmic environment), then the \Comment{...} macro typesets a comment in pseudo-code:

enter image description here

\documentclass{article}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}
\begin{algorithmic}[1]
  \State this is code \Comment{this is a comment}
\end{algorithmic}
\end{document}​

In both these environments/packages, you are able to modify the formatting (or typesetting) of the comment. Alternatively, you could create your own \comment{...} macro as well, based on the existing commenting macros.

Werner
  • 603,163
12

You can use also \tcp{...} to add comments in algorithm2e environment.

Hadij
  • 360
2

I found next possibilities:

  1. Tab characters seem to work even in flexible columns.

  2. escapechar=\% in options will turn on escape to LaTeX. Then, using %\mbox[2in][r]{comment}% should work, too.

1

You can try using

\tcc{the comment}

Example Latex code:

\begin{algorithm}
\caption{Algorithm 1 title}
\tcc{the comment}
\end{algorithm}

How it should be rendered:

Rendered Latex

842Mono
  • 111