如何在伪代码中更改注释的布局?

如何在伪代码中更改注释的布局?

我正在用包编写伪代码algorithm。现在algorithmicx包会在引用行的右侧生成一个带有三角形的注释(这是默认布局)。我需要在经典//%符号中更改此布局,在引用的上一行中插入命令。此外,当是注释时,我不想打印行号(\State)。我的代码如下:

\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}

另一个问题是\newcommand。我定义了一些我在文档中使用的变量,以便在文档中使用相同的布局。对于变量\varNameVariable,我需要伪代码中的 \textit{\varNameVariable}。问题是它打印在斜体

谁能帮我?

答案1

以下工具\LineComment[<indent>]{<line comment>}似乎可以提供您正在寻找的内容:

在此处输入图片描述

\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}

您可以更新\commentsymbol以满足您的需要。它的原始定义是/曾经是\(\triangleright\)

相关内容