[Tex/LaTex] How to change the layout of comment in a pseudocode

algorithmicxformattingpseudocode

I'm writing a pseudocode with algorithm package.
Now algorithmicx package produce a comment with the triangle on the right of the line which is referred (this is the default layout).
I need to change this layout in the classic // or % symbol, insert the command in the previous line to which is referred. Furthermore, I don't want to print the number of line (\State) when is a comment.
My code is the following:

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\renewcommand{\algorithmicforall}{\textbf{for each}}
\renewcommand{\Function}[2]{%
  \csname ALG@cmd@\ALG@L @Function\endcsname{#1}{#2}%
  \def\jayden@currentfunction{#1}%
}
\newcommand{\funclabel}[1]{%
  \@bsphack
  \protected@write\@auxout{}{%
    \string\newlabel{#1}{{\jayden@currentfunction}{\thepage}}%
  }%
  \@esphack
}
%define my variables
\newcommand{\varu}{\emph{u}}
\newcommand{\vart}{\emph{t}}
\newcommand{\varT}{\emph{T}}
\newcommand{\varrt}{\emph{$r_{t}$}}
\newcommand{\varts}{\emph{ts}}
\newcommand{\varTripletta}{$u,\varrt, \varts$}
\newcommand{\varParamA}{\emph{ParamA}}
\newcommand{\varParamb}{\emph{ParamB}}
\newcommand{\varNameVariable}{nameVariable}
\begin{document}
In this section we define some variables \varTripletta that means that \varu is a user such that...
\begin{algorithm}
  \caption{MyAlgorithm}
  \label{algMyAlg}
     \begin{algorithmic}[1]
    \Procedure {MyAlgorithm} {$z, \varParamA, \varParamB$}
    \State \textit{\varNameVariable}    $\gets$ 0 \Comment {\emph{This comment is referred to this line}}
    \State  nameVariable $\gets $ 0
    ... 
    \EndProcedure
    \end{algorithmic}
\end{algorithm}

An other question is to the \newcommand. I define some varibales that I use in the document in order to use the same layout in the document.
For the variable \varNameVariable, I need that in the pseudocode is in \textit{\varNameVariable}. The problem it is printed in italic.

Can anyone help me?

Best Answer

The following implements \LineComment[<indent>]{<line comment>} which seems to provide what you're looking for:

enter image description here

\documentclass{article}
\usepackage{algorithm,algpseudocode}

%define my variables
\newcommand{\varParamA}{\varfont{ParamA}}
\newcommand{\varParamB}{\varfont{ParamB}}
\newcommand{\varNameVariable}{\varfont{nameVariable}}
\newcommand{\assign}{\gets}

% Algorithm definitions
\newcommand{\commentsymbol}{//}% or \% or $\triangleright$
\algrenewcommand\algorithmiccomment[1]{\hfill \commentsymbol{} #1}
\makeatletter
\newcommand{\LineComment}[2][\algorithmicindent]{\Statex \hspace{#1}\commentsymbol{} #2}
\makeatother
\newcommand{\varfont}{\texttt}

\begin{document}
In this section we define some variables \ldots
\begin{algorithm}
  \caption{MyAlgorithm}
  \begin{algorithmic}[1]
  \Procedure {MyAlgorithm} {$z$, \varParamA, \varParamB}
    \State $\varNameVariable \assign 0$ \Comment {This comment refers to this line}
    \If{test}
      \LineComment[2\dimexpr\algorithmicindent]{This comment refers to the following line}
      \State something
    \EndIf
    \LineComment{This comment refers to the following line}
    \State $\varNameVariable \assign 0$
    \State \ldots
  \EndProcedure
  \end{algorithmic}
\end{algorithm}

\end{document}

You can update \commentsymbol to suit your needs. It's original definition is/was \(\triangleright\).