3

I want to use \tcc and \tcp commands in algorithmicx package.

Is there any way to do this?

\documentclass[12pt,a4paper]{report}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{enumerate}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\usepackage{etoolbox}
\begin{document}
\begin{figure}

\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{/*We have the answer if r is 0*/} %%%I want the comment in a separate line and italic.
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$

\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\caption{Euclid’s algorithm}\label{euclid}
\end{figure}
\end{document}
2

In answer to your commented request in the code, create something like \LineComment{<comment>}, which will set a comment right-aligned and italicized:

enter image description here

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\newcommand{\LineComment}[1]{\Statex \hfill\textit{#1}}

\begin{document}
\begin{algorithm}
  \begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
    \State $r \gets a \bmod b$
    \While{$r \neq 0$}\LineComment{We have the answer if~$r$ is~$0$}
      \State $a \gets b$
      \State $b \gets r$
      \State $r \gets a \bmod b$
    \EndWhile\label{euclidendwhile}
    \State \textbf{return} $b$\Comment{The g.c.d.\ is~$b$}
  \EndProcedure
  \end{algorithmic}
  \caption{Euclid’s algorithm}\label{euclid}
\end{algorithm}
\end{document}

You can format \LineComment the way you want. For example, using

\newcommand{\LineComment}[1]{\Statex \hfill/* \textit{#} */}

would wrap the line comment in /*...*/:

enter image description here

Following some guidelines in Using end-of-line delimiter in plain TeX macro, you could also use

\makeatletter
\let\fslash=/
\catcode`\/=\active
\newcommand{/}{\@ifnextchar/{\begingroup\catcode`\^^M=12 \fslash@}\fslash}
{\catcode`\^^M=12 %
 \gdef\fslash@/#1^^M{\hfill \fslash\fslash{\itshape#1}\endgroup}}
\makeatother

in order to make / active. If used in succession //, it captures the contents until the end-of-line character is reached, placing it in an italicized font:

enter image description here

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\newcommand{\LineComment}[1]{\Statex \hfill/* \textit{#1} */}

% https://tex.stackexchange.com/q/125549/5764
\makeatletter
\let\fslash=/
\catcode`\/=\active
\newcommand{/}{\@ifnextchar/{\begingroup\catcode`\^^M=12 \fslash@}\fslash}
{\catcode`\^^M=12 %
 \gdef\fslash@/#1^^M{\hfill \fslash\fslash{\itshape#1}\endgroup}}
\makeatother

\begin{document}
\begin{algorithm}
  \begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
    \State $r \gets a \bmod b$
    \While{$r \neq 0$}\LineComment{We have the answer if~$r$ is~$0$}
      \State $a \gets b$ // Another comment
      \State $b \gets r$
      \State $r \gets a \bmod b$
    \EndWhile\label{euclidendwhile}
    \State \textbf{return} $b$\Comment{The g.c.d.\ is~$b$}
  \EndProcedure
  \end{algorithmic}
  \caption{Euclid’s algorithm}\label{euclid}
\end{algorithm}
\end{document}

The above momentary redefinition of ^^M (the end-of-line char) could be problematic, but really depends on the circumstances. Regardless, the following might be safer:

\usepackage{etoolbox}
\makeatletter
\let\fslash=/
\catcode`\/=\active
\newcommand{/}{\@ifnextchar/{\fslash@}\fslash}
\def\fslash@/{\hfill \fslash\fslash\itshape}
\AtBeginEnvironment{algorithmic}{%
  \let\olditem\item
  \renewcommand{\item}{\upshape\olditem}
}
\makeatother
  • This is indeed a good trick. However, not exactly what I want. I want the comment to be surrounded /*...*/. Moreover, using //... should be an option. – padawan Feb 1 '15 at 21:41
  • @cagirici: I've added some more pizazz. – Werner Feb 1 '15 at 22:34

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.