3

I'm having trouble with the legend of the lstlisting and listing the latex file when I reference them both are appearing as listing 1. How do I display the number of correctly caption for listing 1 and listing 2? Where am I wrong?

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{inconsolata}

\usepackage{color}

\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}

\usepackage{minted}
\usepackage{listings}
\lstset{language=Java,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily,
  moredelim=[il][\textcolor{pgrey}]{$$},
  moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}

\begin{document}

\begin{lstlisting}[caption={Description of Java code},label=useless, language=Java]
/**
 * This is a doc comment.
 */
package com.ociweb.jnb.lombok;

import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;

$$@Data
$$@EqualsAndHashCode(exclude={"address","city","state","zip"})
public class Person {
    enum Gender { Male, Female }

    // another comment

    %%@NonNull%% private String firstName;
    %%@NonNull%% private String lastName;
    %%@NonNull%% private final Gender gender;
    %%@NonNull%% private final Date dateOfBirth;

    private String ssn;
    private String address;
    private String city;
    private String state;
    private String zip;
}
\end{lstlisting}

Listing \ref{useless} contains an example of a listing

\begin{listing}[H]
\begin{minted}{xml}
<xml>
    <person age="22" sex="female">Ann</person>
</xml>
\end{minted}
\caption{Description of the XML Code}
\label{lst:example}
\end{listing}

Listing \ref{lst:example} contains an example of a listing

\end{document}

The output:

enter image description here

2
  • 2
    Are you sure those two constructions use the same counter? You have something from listings and something from minted. Even though they use the same caption name, I don't think they use the same counter underneath.
    – daleif
    Commented Oct 2, 2015 at 10:12
  • Exactly daleif, I also agree, however when using references \ref{useless} for lstlisting and \ref{lst:example} for listing with minted is showing the same number and do not know what to do so that he understands the numbering correct listing 2 and listing 1. Commented Oct 2, 2015 at 10:37

1 Answer 1

4

Add to your preamble

\makeatletter
\AtBeginDocument{\let\c@listing\c@lstlisting}
\makeatother

which will use the same counter for both float types.

Full example:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{inconsolata}

\usepackage{color}

\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}

\usepackage{minted}
\usepackage{listings}
\lstset{language=Java,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily,
  moredelim=[il][\textcolor{pgrey}]{$$},
  moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}

\makeatletter
\AtBeginDocument{\let\c@listing\c@lstlisting}
\makeatother

\begin{document}

\begin{lstlisting}[caption={Description of Java code},label=useless, language=Java]
/**
 * This is a doc comment.
 */
package com.ociweb.jnb.lombok;

import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;

$$@Data
$$@EqualsAndHashCode(exclude={"address","city","state","zip"})
public class Person {
    enum Gender { Male, Female }

    // another comment

    %%@NonNull%% private String firstName;
    %%@NonNull%% private String lastName;
    %%@NonNull%% private final Gender gender;
    %%@NonNull%% private final Date dateOfBirth;

    private String ssn;
    private String address;
    private String city;
    private String state;
    private String zip;
}
\end{lstlisting}

Listing \ref{useless} contains an example of a listing

\begin{listing}[H]
\begin{minted}{xml}
<xml>
    <person age="22" sex="female">Ann</person>
</xml>
\end{minted}
\caption{Description of the XML Code}
\label{lst:example}
\end{listing}

Listing \ref{lst:example} contains an example of a listing

\end{document}

Don't use the [H] option: it just does harm.

enter image description here

1
  • Thank​ you so much for your help egreg, now it's all working! Commented Oct 2, 2015 at 11:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .