如何在 lstlisting 和列出同一个 latex 文件之间正确编号?

如何在 lstlisting 和列出同一个 latex 文件之间正确编号?

我对列表和列表 latex 文件的图例感到困惑,当我引用它们时,它们都显示为列表 1。我该如何正确显示列表 1 和列表 2 的标题数量?我错在哪里?

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

输出:

在此处输入图片描述

答案1

添加到你的序言中

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

对于两种浮点类型将使用相同的计数器。

完整示例:

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

不要使用该[H]选项:它只会造成危害。

在此处输入图片描述

相关内容