我怎样才能拥有编号的逐字环境?

我怎样才能拥有编号的逐字环境?

我怎样才能拥有一个自动编号的逐字环境,就像方程式一样?我试着这样说:

\begin{equation}
\begin{verbatim}
bla... bla... bla...
\end{verbatim}
\end{equation}

但它给出了一个错误。我相信一定有更好的方法。

答案1

这是包中的示例文件numberedblock。它使标签与公式编号略有不同,这样如果在文档中同时使用它们,就不会混淆它们。但是,标签位置和样式可以更改。文档在 .sty 文件本身以及此示例文件中简要介绍。

可重置的参数默认值包括:

\setlength\maxblocklabelsize{-0.4in}

\setlength\blockindent{0.2in}

\newcommand\blocklabel[1]{[\textit{\arabic{#1}}]}

以下是软件包下载中的示例,已使用 Werner 的精彩答案进行了编辑如何引用方程的一部分?,以提供与 hyperref 兼容的标签/引用功能。在的情况下\numblock,您将一个放置在参数\nblabel{}\numblock。在环境中numVblock,您可以向其传递一个可选参数[\nbVlabel{}]

\documentclass[10pt]{article}
\usepackage{numberedblock}

\makeatletter
\AtBeginDocument{\let\nb@label\label}% https://tex.stackexchange.com/q/9939/5764
\newcounter{nb@counter}
\newcommand{\nblabel}[1]{\def\@currentlabel{\theblocknum}\nb@label{#1}}
\newcommand{\nbVlabel}[1]{\setcounter{nb@counter}{\theblocknum}\stepcounter{nb@counter}%
  \def\@currentlabel{\thenb@counter}\nb@label{#1}}
\makeatother


\begin{document}

\parindent 0.3in
%\setlength\maxblocklabelsize{-.4in}
\setlength\blockindent{0.0in}

This is a test of the \textsf{numberedblock} style package, which is
specially designed to produce sequentially numbered BLOCKS of code (note
the individual code lines are not numbered, but the whole block gets a
single number, for later reference (much in the same way that equations
can get numbered in a document).  While specialized for numbering code
blocks, the commands can actually number other items, as well, in fact
anything that fits in a \LaTeX{} box.

If the code block contains no special characters, one can simply use the
command form, called \verb,\numblock,.  It cannot handle verbatim text,
but must use standard \LaTeX{} escape sequences (for line breaks,
contiguous spaces, special characters, etc.).  It puts the output in a
tt font , which is the same used in the verbatim environment:

\numblock{This text is the\\argument to the command\\where double
slashes have been\\used for line breaks\nblabel{vb:i}}

Most useful, however, there is also the \verb,numVblock, environment,
which handles verbatim text, as seen in the next example: referencing code block~\ref{vb:i}

\begin{numVblock}[\nbVlabel{vb:ii}]
This is the numVblock 
environment, which         (<--see contiguous spaces here)
succeeds in
incorporating verbatim text like
@##$%*$%$()||}{?><\\\
\end{numVblock}

Referencing code block~\ref{vb:ii}.
As envisioned the \verb,numVblock, environment would be ideally suited
for displaying small code blocks as part of documentation.  The code can
contain contiguous spaces and special characters:

\begin{numVblock}[\nbVlabel{vb:iii}]
      program test
      implicit none
      integer a, x
c$$$$$$$$$$$$$$$$$$$$$$$$$
      a = 0
      x = 1
   10 a = a + x
      if (a .eq. 100) stop
      goto 10
      end
\end{numVblock}

Referencing code block~\ref{vb:iii}.
Below, I test the \verb,\numblock, command with the argument as a
box, rather than as formatted text.

\numblock{\fbox{Testing, 1,2,3 testing a box}\nblabel{vb:iv}}

Don't forget, there are settable parameters to define the block
left-indent, the format of the label, and (if needed) the labels' max
width/placement. CB~\ref{vb:iv}

\end{document}

在此处输入图片描述

答案2

这是一个开始,您可以修改它以满足其他需求。

\documentclass{article}

\usepackage{amsthm}

\newtheorem{VerbatimText}{}

\begin{document}

\begin{VerbatimText} \label{v:test}
\begin{verbatim}
This is my numbered verbatim environment
\end{verbatim}
\end{VerbatimText}

I can refer to it with a reference: \ref{v:test}.
\end{document}

在此处输入图片描述

答案3

我有 3 个建议。请看:

第一个...

\documentclass{article}
\usepackage{amsmath}\begin{document}
\begin{equation}
  \verb!f(x)=ax^2+bx+c! \label{eq:1}
\end{equation}
In order to typeset $f(x)=ax^2+bx+c$, see~\eqref{eq:1}.
\end{document}

在此处输入图片描述

第二个...

\documentclass{article}
\usepackage{amsmath}
\usepackage{fancyvrb}
\begin{document}
\DefineShortVerb{\|}
\SaveVerb{VerbA}|f(x)=ax^2+bx+c|
\SaveVerb{VerbB}|g(x)=dx^2+ex+f|
\begin{align}
  \UseVerb{VerbA} \label{eq:1} \\
  \UseVerb{VerbB} \label{eq:2}
\end{align}
In order to typeset $f(x)=ax^2+bx+c$, see~\eqref{eq:1}. There is also~\eqref{eq:2} that shows you how to typeset $g(x)=dx^2+ex+f$.
\end{document}

在此处输入图片描述

第三个。

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\begin{equation}
  \left.\begin{array}{r@{}l}
    \verb!f(x)!&\verb!=ax^2+bx+c! \\
    \verb!g(x)+2f(x)!&\verb!=y!
  \end{array}\right\} \label{eq:input}
\end{equation}
One could use~\eqref{eq:input} as input.
\end{document}

在此处输入图片描述

答案4

我的解决方案是一个新的环境,numbenv以编号环境命名。请参阅随附的图像文件以快速查看。以下代码位于 LaTeX 序言中。

\usepackage{verbatim}
\newenvironment{numbenv}{\refstepcounter{equation}\medskip\par\noindent\begin{
minipage}{0.9\textwidth}}
{\end{minipage}\hfill(\arabic{equation})\medskip\par\noindent\
ignorespacesafterend}

我使用了现有的方程计数器,但可以使用任何其他计数器。以下代码示例位于主节中的\begin{document}\end{document}中。它对应于图像中的方程 (2)。

\begin{numbenv}\label{eq:numbenv:eg1}
\begin{verbatim}
for i in $ {a[@]}; do echo -n < $ i>; done; echo
\end{verbatim}
\end{numbenv}

当然,要引用逐字标签,只需写入(\ref{eq:numbenv:eg1})

在此处输入图片描述

相关内容