前段时间,我从 Stackexchange 复制了此代码,以便在导入的源代码顶部有一个漂亮的灰色框,我用它显示listings
:
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\lstset{ aboveskip={0.0\baselineskip}, belowskip={0.0\baselineskip} }
问题是我已经切换到minted
显示代码,但我想保留代码上方的灰色标题框。你能帮我摆脱对包的需求listings
吗?这是一个典型的文档:
\documentclass[a4paper,oneside]{article}
\usepackage{a4wide}
\usepackage[icelandic]{babel}
\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{minted}
\usemintedstyle{perldoc}
\setlength\partopsep{-\topsep}
\usepackage{listings}
\usepackage{caption}
\usepackage{xcolor}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\lstset{ aboveskip={0.0\baselineskip}, belowskip={0.0\baselineskip} }
\begin{document}
\begin{lstlisting}[caption=\textbf{Title} -- This is the title I want for my code]
\end{lstlisting}
\begin{minted}{java}
public class demo {
public static void main(String[] args) {
System.out.println("My source code.");
}
}
\end{minted}
\end{document}
答案1
\DeclareFloatingEnvironment
您可以使用包提供的选项定义您自己的(浮动)lstlisting 环境newfloat
,例如:
\documentclass[a4paper,oneside]{article}
\usepackage{a4wide}
\usepackage[icelandic]{babel}
%\usepackage{fontspec}
%\usepackage{xunicode}
\usepackage{minted}
\usemintedstyle{perldoc}
\setlength\partopsep{-\topsep}
\usepackage{caption,newfloat}
\usepackage{xcolor}
\DeclareFloatingEnvironment[fileext=lol]{lstlisting}[Listing][List of Listings]
\captionsetup[lstlisting]{box=colorbox,boxcolor=gray,font={color=white},labelsep=endash,skip=5pt}
\begin{document}
\begin{lstlisting}
\caption{This is the title I want for my code}
\begin{minted}{java}
public class demo {
public static void main(String[] args) {
System.out.println("My source code.");
}
}
\end{minted}
\end{lstlisting}
\end{document}
请注意,我已更改自v3.3box=colorbox
以来可用的标题格式。如果您的软件包比该版本更旧,则需要更新或将其改回。另请参阅:caption.sty
caption
\DeclareCaptionFormat{listing}{...}...
列表中央的标题