我的列表环境有问题,基本上我想创建两个不同的环境并独立引用它们,例如程序 2.1、函数 2.1 等...我搜索了很多试图找到答案,但找不到解决我问题的答案。以下是说明问题的内容:
\section{Hello Worlds}
Here I will illustrate my problem by talking about how to write Hello World programs.
\begin{program}[caption={Hello World in Java}, label={prog:HelloWorldJava}, language={Java}]
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
\end{program}
As you can see, program \ref{prog:HelloWorldJava} is extremely sophisticated.
\begin{function}[caption={Hello World as Java Function}, label={func:HelloWorldJavaFunc}, language={Java}]
public void sayHello() {
System.out.println("Hello World!");
}
}
\end{function}
As you can see, we have here moved the line printing "Hello World" to a seperate function, namely function \ref{func:HelloWorldJavaFunc}.
\begin{program}[caption={Hello World in Scala}, label={prog:HelloWorldScala}, language={Scala}]
object HelloWorld {
def main(args: Array[String]) = {
println("Hello World!");
}
}
\end{program}
"Hello World"-programs can be written in other programming languages as well.
For example, program \ref{prog:HelloWorldScala} prints "Hello World" using Scala.
我的列表环境定义如下:
%Creating environment program
\newcounter{npro}[section]
\renewcommand{\thenpro}{\thesection .\arabic{npro}}
\DeclareCaptionLabelFormat{progcaption}{Program \thenpro}
\lstnewenvironment{program}[1][]{
\refstepcounter{npro}
\captionsetup{labelformat=progcaption}
\lstset{
frame=TB,
#1
}
}
{}
%Creating environment function
\newcounter{nfun}[section]
\renewcommand{\thenfun}{\thesection .\arabic{nfun}}
\DeclareCaptionLabelFormat{funccaption}{Function \thenfun}
\lstnewenvironment{function}[1][]{
\refstepcounter{nfun}
\captionsetup{labelformat=funccaption}
\lstset{
frame=TB,
#1
}
}
{}
如您所见,标题按预期工作。但是,在文中引用时,有些东西没有按我想要的方式工作。例如,函数 1.1 被引用为函数 2,程序 1.2 成为程序 3。我猜 \ref 运算符以某种方式使用了列表计数器,但我怎样才能让它们使用我定义的计数器呢?所以基本上,我怎样才能让文本中的引用看起来与标题中的引用相同?
提前谢谢您,如果答案已经在别处存在,请见谅。昨晚花了好几个小时试图解决这个问题,但一无所获。对乳胶非常陌生...
答案1
计数器 lstlisting 的使用在列表中是相当硬编码的。如果您需要多个环境,最好使用 tcolorbox 及其 listings 库,因为它提供了更大的灵活性:
\documentclass{article}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting[
auto counter,
number freestyle={\noexpand\arabic{section}.\noexpand\arabic{\tcbcounter}},
]{program}[2][]
{colframe=red!75!black,
fonttitle=\bfseries,
listing only,
title=Program \thetcbcounter: #2,
#1}
\newtcblisting[
auto counter,
number freestyle={\noexpand\arabic{section}.\noexpand\arabic{\tcbcounter}},
]{function}[2][]
{colframe=green!75!black,
fonttitle=\bfseries,
listing only,
title=Function \thetcbcounter: #2,
#1}
\begin{document}
\section{Hello Worlds}
\begin{program}[label=prog:java,listing options={language=Java}]{Java}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
\end{program}
program \ref{prog:java}
\begin{function}[label=func:function]{Function}
Function
\end{function}
function \ref{func:function}
\begin{program}[label=prog:scala]{Scala}
Program
\end{program}
program \ref{prog:scala}
\end{document}