这个问题主要与在列表环境中定义新标题。我想定义默认的标题设置lstlisting
,但也想定义新列表环境的标题设置。但不知何故\captionsetup[lstlisting]
覆盖了为新列表环境定义的其他标题设置。我借用了相关问题的答案中的例子。
我怎样才能使以下代码正常运行?
\documentclass{article}
\usepackage{listings}
\usepackage{color}
\usepackage{caption}
\definecolor{listinggray}{gray}{0.98}
\definecolor{lbcolor}{rgb}{0.98,0.98,0.98}
\lstset{
% ...
float=tp,
% ...
}
\usepackage{caption}
% caption setup for default listing
\DeclareCaptionFont{listingcapfont}{\color{green}}
\DeclareCaptionFormat{listingcapfor}{
\colorbox[cmyk]{0.9, 0.1, 0.9,0.5}{
\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}
}
}
\captionsetup[lstlisting]{
format=listingcapfor,
labelfont=listingcapfont,
textfont=listingcapfont,
singlelinecheck=false,
margin=0pt,
font={bf,footnotesize}
}
% caption setup for code
\DeclareCaptionFont{codecapfont}{\color{white}}
\DeclareCaptionFormat{codecapfor}{
\colorbox[cmyk]{0.43,0.35,0.35,0.01}{
\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}
}
}
\captionsetup[code]{
format=codecapfor,
labelfont=codecapfont,
textfont=codecapfont,
singlelinecheck=false,
margin=0pt,font={bf,footnotesize}
}
\lstnewenvironment{code}[1][]%
{\noindent\minipage{\linewidth}%
\lstset{#1}%
\captionsetup{options=code}% execute options set with \captionsetup[code]{...}
}
{\endminipage}
% caption setup for tip
\DeclareCaptionFont{tipcapfont}{\color{red}}
\DeclareCaptionFormat{tipcapfor}{
\colorbox[cmyk]{0.0,0.35,0.35,0.01}{
\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}
}
}
\captionsetup[tip]{
format=tipcapfor,
labelfont=tipcapfont,
textfont=tipcapfont,
singlelinecheck=false,
margin=0pt,
font={bf,footnotesize}
}
\makeatletter
\newcounter{tip}
\lstnewenvironment{tip}[1][]%
{%
\noindent\minipage{\linewidth}%
\def\lstlistingname{Tip}%
\def\lstlistingautorefname{Tip}%
\let\c@lstlisting=\c@tip
\let\thelstlisting=\thetip
\lstset{#1,keywordstyle=\color{black},
numberbychapter=false,
basicstyle=\footnotesize\ttfamily\color{black}\bfseries,
backgroundcolor=\color{white},frame=none,captionpos=tc}%
\captionsetup{options=tip}% execute options set with \captionsetup[tip]{...}
}
{\endminipage}
\makeatother
\begin{document}
\begin{lstlisting}[caption=This is a lstlisting block]
Default listing code...
\end{lstlisting}
....Look, just because I don't be givin' no man a foot massage don't make it
right for Marsellus....
\begin{code}[caption=This is a code block]
Wonderfull code...
\end{code}
....Look, just because I don't be givin' no man a foot massage don't make it
right for Marsellus....
\begin{tip}[caption=This is a tip block]
A tip!
\end{tip}
\end{document}
答案1
我认为我已经解决了这个问题。我不确定这是否正确/合适,不过我还是为遇到这个问题的人发布了解决方案。
根据手动的标题包(见第 4 页和第 19 页),
很高兴知道 \captionsetup 仅对当前环境有影响。
因此,我遇到了\clearcaptionsetup
摆脱标记为在特定环境中自动使用的参数的情况。
\clearcaptionsetup{lstlisting}
因此,在环境定义中使用就足够了。
\lstnewenvironment{code}[1][]%
{\noindent\minipage{\linewidth}%
\lstset{#1}%
\clearcaptionsetup{lstlisting}
\captionsetup{options=code}
}
{\endminipage}
和
\makeatletter
\newcounter{tip}
\lstnewenvironment{tip}[1][]%
{%
\noindent\minipage{\linewidth}%
\def\lstlistingname{Tip}%
\def\lstlistingautorefname{Tip}%
\let\c@lstlisting=\c@tip
\let\thelstlisting=\thetip
\clearcaptionsetup{lstlisting}
\lstset{#1,keywordstyle=\color{black},
numberbychapter=false,
basicstyle=\footnotesize\ttfamily\color{black}\bfseries,
backgroundcolor=\color{white},frame=none,captionpos=tc}%
\captionsetup{options=tip}
}
{\endminipage}
\makeatother
因此,、lstlisting
和code
块tip
可以互相不干扰地工作。