我想使用包中的\tcc
命令。\tcp
algorithmicx
有什么办法可以做到这一点?
\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}
答案1
为了回答代码中注释的请求,请创建类似的内容\LineComment{<comment>}
,这将设置注释右对齐,并且斜体:
\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}
您可以\LineComment
按自己想要的方式格式化。例如,使用
\newcommand{\LineComment}[1]{\Statex \hfill/* \textit{#} */}
将行注释包装在/*
...中*/
:
遵循一些指导原则在纯 TeX 宏中使用行尾分隔符,你也可以使用
\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
以便激活/
。如果连续使用//
,它会捕获内容,直到到达行尾字符,并将其放置在斜体字体:
\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}
上述对^^M
(行尾字符)的瞬间重新定义可能会有问题,但实际上取决于具体情况。无论如何,以下方法可能更安全:
\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