我已在文档中创建了lstnewenvironment
。我无法交叉引用它。
以下是代码
\lstnewenvironment{customlistings}[3]{
\lstset{
numbers=left,
language=#3,
breaklines=true ,
keywordstyle=\color{blue}\bfseries ,
numberstyle=\tiny\color{gray} ,
commentstyle=\color{green!30!black},
stringstyle = \color{violet},
morekeywords={\begin, \label, \documentclass
, \usepackage, \caption},
label=#2
}
\refstepcounter{counterA}
\begin{center}
\textbf{\large{Listing \thecounterA: #1}} \\
\textbf{\normalsize{#2}}
\end{center}
}{}
以下是我的使用方法。
\begin{customlistings}{[LaTeX]TeX}{The listings package}{TeX}
\begin{lstlisting}
%Your code goes here
%This is usually how you present code in \LaTex, without worrying about accidentally ..
\end{lstlisting}
\label{trickq}
\end{customlistings}
我想引用它,它应该显示为“清单 1”,其中 1 是引用,我尝试了很多方法,包括在谷歌上查找,尝试将此代码放在另一个环境中,然后使用 \label,但我的标签继续从上一个标签开始,即它在文档中显示 4.2 而不是 1。请帮忙
答案1
我会使用该listing
包,而不是手动执行操作。
\documentclass{article}
\usepackage{listings,listing}
\usepackage{xcolor}
\lstnewenvironment{customlisting}[2][]
{\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
numbers=left,
language=#2,
breaklines=true,
keywordstyle=\color{blue}\bfseries,
numberstyle=\tiny\color{gray},
commentstyle=\color{green!30!black},
stringstyle = \color{violet},
morekeywords={\begin, \label, \documentclass, \usepackage, \caption},
#1,
}%
}{}
\begin{document}
\begin{listing}
\begin{customlisting}{[LaTeX]TeX}
%Your code goes here
%This is usually how you present code in \LaTex, without worrying about accidentally ..
\end{customlisting}
\caption{The listing package}\label{TeX}
\end{listing}
\ref{TeX} is a listing
\end{document}
您可以使用包自定义标题的外观caption
。