使用 \mdframed 为 \lstnewenvironment 添加标题

使用 \mdframed 为 \lstnewenvironment 添加标题

我在网上找到了一段我非常喜欢的 LaTeX 代码:

\documentclass{article}
\usepackage{listings,mdframed,xcolor}

\definecolor{codeBackground}{rgb}{0.9, 0.9, 0.8}
\lstnewenvironment{mylisting}{
  \lstset{
    moredelim=**[is][\bfseries]{|}{|},% bold everything in between ||
    moredelim=**[is][\itshape]{*}{*}  % italic everything in between **
  }%
\mdframed[backgroundcolor=codeBackground,shadow=true,shadowsize=2pt,shadowcolor=black!30]%
}{%
  \endmdframed\ignorespaces
}

\begin{document}


\begin{mylisting}
public |class| Test {
  public *static |void|* main(String[] argumente) {
    System.out.println("Hallo Welt!");
  }
}
\end{mylisting}

\end{document}

我想在这个源代码中添加编号标题,但

\begin{mylisting}{caption=code caption}
public |class| Test {
  public *static |void|* main(String[] argumente) {
    System.out.println("Hallo Welt!");
  }
}
\end{mylisting}

也不:

\begin{mylisting} \caption{code caption}
public |class| Test {
  public *static |void|* main(String[] argumente) {
    System.out.println("Hallo Welt!");
  }
}
\end{mylisting}

确实有效。有什么想法吗?

答案1

您可以向您的环境添加一个包含标题的新参数,并\captionofcaption包中使用:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{listings,mdframed,xcolor}
\usepackage{caption}

\definecolor{codeBackground}{rgb}{0.9, 0.9, 0.8}
\lstnewenvironment{mylisting}[1]{
  \lstset{
    moredelim=**[is][\bfseries]{|}{|},% bold everything in between ||
    moredelim=**[is][\itshape]{*}{*}  % italic everything in between **
  }%
\mdframed[backgroundcolor=codeBackground,shadow=true,shadowsize=2pt,shadowcolor=black!30]%
}{%
  \endmdframed
  \captionof{lstlisting}{#1}
  \ignorespaces
}

\begin{document}


\begin{mylisting}{My Caption}
public |class| Test {
  public *static |void|* main(String[] argumente) {
    System.out.println("Hallo Welt!");
  }
}
\end{mylisting}
\end{document}

答案2

您可以添加caption\lstset

\documentclass{article}
\usepackage{listings,mdframed,xcolor}

\definecolor{codeBackground}{rgb}{0.9, 0.9, 0.8}
\lstnewenvironment{mylisting}[1][]{%
  \lstset{
    moredelim=**[is][\bfseries]{|}{|},% bold everything in between ||
    moredelim=**[is][\itshape]{*}{*},  % italic everything in between **
    caption={[#1]{#1}}        %%<---------------------- here
  }%
\mdframed[backgroundcolor=codeBackground,shadow=true,shadowsize=2pt,shadowcolor=black!30]%
}{%
  \endmdframed
  \ignorespaces
}

\begin{document}


\begin{mylisting}[My Caption]
public |class| Test {
  public *static |void|* main(String[] argumente) {
    System.out.println("Hallo Welt!");
  }
}
\end{mylisting}

Some text here
\begin{mylisting}
public |class| Test {
  public *static |void|* main(String[] argumente) {
    System.out.println("Hallo Welt!");
  }
}
\end{mylisting}
\end{document}

在此处输入图片描述

你可以通过以下方式使其更加通用

\lstnewenvironment{mylisting}[1][]{%
  \lstset{
    moredelim=**[is][\bfseries]{|}{|},% bold everything in between ||
    moredelim=**[is][\itshape]{*}{*},  % italic everything in between **
    #1
  }%

这样就lstset可以随时添加更多选项,例如

\begin{mylisting}[caption={My Caption}]

相关内容