如何对代码清单进行注释

如何对代码清单进行注释

是否可以对源代码进行如下注释?

在此处输入图片描述

答案1

您需要做两件事:(1) 向列表添加脚注,以及 (2) 更改脚注的外观。让我们同时完成这两项工作。

该包fancyvrb允许向逐字文本添加任意 TeX 命令。因此

\begin{Verbatim}[\commandchars=\\\{\}]
  text\footnote{this is a footnote}
\end{Verbatim}

会将脚注添加到逐字片段中。由于我们希望脚注靠近列表,因此让我们将片段放在 中minipage,然后脚注会自动变为小页面脚注。现在,您希望脚注标记成为 dingbat,并将脚注标记排版为右对齐。第一个是通过

\makeatletter
\renewcommand{\thempfootnote}{\ding{\numexpr(181+\c@mpfootnote)}}
\makeatother

第二种方法是定义一个特殊的命令

\newcommand{\commentcode}[1]{\hfill\footnote{#1}}

全部一起:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{pifont}
\makeatletter
\renewcommand{\thempfootnote}{\ding{\numexpr(181+\c@mpfootnote)}}
\makeatother
\newcommand{\commentcode}[1]{\hfill\footnote{#1}}
\begin{document}
\thispagestyle{empty}
\begin{minipage}{\textwidth}
\begin{Verbatim}[commandchars=\\\{\}]
wget ftp://ftp.gnu.org/gsl/gsl-1.16.tar.gz\commentcode{Download the zipped archive}
tar xzvf \commentcode{Unzip the archive}
cd gsl-1.16
./configure
make
sudo make install
\end{Verbatim}
\end{minipage}
\end{document}

结果:

在此处输入图片描述

相关内容