如何在列表中正确添加标题?

如何在列表中正确添加标题?

我有两个块 lstlisting 和另一个列表,如下所示:

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

但是,minted block 在代码底部显示图例。我如何获取代码上部的图例?

在此处输入图片描述

答案1

minted使用newfloat选项加载

\usepackage[newfloat]{minted}

\caption并在实际列出代码之前写入:

\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[newfloat]{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]
\caption{Description of the XML Code}
\label{lst:example}
\begin{minted}{xml}
<xml>
    <person age="22" sex="female">Ann</person>
</xml>
\end{minted}
\end{listing}

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

\end{document}

在此处输入图片描述

相关内容