4

I have a problem with my listings environments, basically I want to create two different environments and reference them independently, e.g. program 2.1, function 2.1 etc... I have searched a lot trying to find the answer, but haven't been able to find an answer solving my issue. Here is something to illustrate the problem:

\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.

My listings environments are defined in the following manner:

%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
    }
}
{}

This produces the following output: enter image description here

As you can see the captions work as intended. However, when referencing in-text, something is not working as I want to. For instance function 1.1 is referenced as function 2 and program 1.2 becomes program 3. I am guessing that somehow the \ref operator uses a listings-counter instead, but how can I make them use the counters I defined? So basically, how can I have the referencing in text look the same as in the captions?

Thank you in advance, and sorry if the answer already exists somewhere else. Spent a good few hours last night trying to figure this out, but got nowhere. Very new to latex...

1

The use of the counter lstlisting is quite hard coded in listings. You should better use tcolorbox and its listings library if you want more than one environment as it offers much more flexibility:

\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}

enter image description here

  • Thanks for the quick answer. Nice workaround, and the boxes look nicer than the thing I had before anyway. – Stenix Sep 6 '16 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.