27

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.

2
  • To clarify the question, please compose a small MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. Nov 21, 2011 at 17:57
  • Although this does not specifically address comments, if you are still trying to choose which package to use, an excellent summary is available at: print programs with its proper syntax Nov 21, 2011 at 18:33

4 Answers 4

21

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.

10

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

1
  • 3
    algorithm2e is not algorithmic from the alogrithms bundle, so I don't see how this answers the question. May 4, 2018 at 18:39
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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.