![列表标题灰色框代码含义](https://linux22.com/image/268595/%E5%88%97%E8%A1%A8%E6%A0%87%E9%A2%98%E7%81%B0%E8%89%B2%E6%A1%86%E4%BB%A3%E7%A0%81%E5%90%AB%E4%B9%89.png)
前段时间,我从 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}{...}...
列表中央的标题