我一直想创建具有自己的计数器的自定义列表样式。我现在遇到的问题是计数器不是增加 1,而是增加 2。
这是一个例子:输出:第一个列表计数器是 2,第二个列表计数器是 4。
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{caption}
% Colors
\definecolor{captioncolor}{cmyk/RGB/HTML}{0,0,0,.4/181,181,181/B5B5B5}
\definecolor{backgroundcolor}{rgb}{0.95, 0.95, 0.92}
\definecolor{commentcolor}{rgb}{0.55, 0.55, 0.55}
\definecolor{identifiercolor}{rgb}{0, 0, 0}
\definecolor{keywordcolor}{rgb}{0, 0, 0.5}
\definecolor{numbercolor}{rgb}{0, 0, 0}
\definecolor{stringrcolor}{rgb}{0, 0.5, 0}
\newcounter{javacounter}
\stepcounter{javacounter}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{captioncolor}{\parbox{\dimexpr\linewidth-2\fboxsep\relax}{#1#2#3}}}
\captionsetup[lstlisting]{
format = listing,
labelfont = white,
textfont = white
}
\lstdefinestyle{Java}{
basicstyle = \linespread{1.1}\mdseries,
backgroundcolor = \color{backgroundcolor},
breaklines = true,
commentstyle = \color{commentcolor},
identifierstyle = \color{identifiercolor},
keywordstyle = \color{keywordcolor},
language = Java,
literate =
{\_}{}{0\discretionary{\_}{}{\_}}
{Ä}{{\"A}}1
{Ö}{{\"O}}1
{Ü}{{\"U}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
{ü}{{\"u}}1
{ß}{{\ss}}1,
morekeywords = {assert},
numbers = left,
numbersep = 5pt,
numberstyle=\color{numbercolor},
showspaces = false,
showstringspaces = false,
showtabs = false,
stringstyle=\color{stringrcolor},
tabsize = 4,
title = Java~(\thejavacounter):~\lst@@caption\stepcounter{javacounter},
xleftmargin = \parindent
}
\begin{document}
\begin{lstlisting}[style = Java]
public static void main(String[] args) {
}
\end{lstlisting}
\begin{lstlisting}[style = Java]
public static void main(String[] args) {
}
\end{lstlisting}
\end{document}
提前致谢!
答案1
这将创建一个java
移动\stepcounter
外部的环境lstlisting
。请参阅手册第 43 页。
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{caption}
% Colors
\definecolor{captioncolor}{cmyk/RGB/HTML}{0,0,0,.4/181,181,181/B5B5B5}
\definecolor{backgroundcolor}{rgb}{0.95, 0.95, 0.92}
\definecolor{commentcolor}{rgb}{0.55, 0.55, 0.55}
\definecolor{identifiercolor}{rgb}{0, 0, 0}
\definecolor{keywordcolor}{rgb}{0, 0, 0.5}
\definecolor{numbercolor}{rgb}{0, 0, 0}
\definecolor{stringrcolor}{rgb}{0, 0.5, 0}
\newcounter{javacounter}
\lstnewenvironment{java}{\stepcounter{javacounter}%
\lstset{style=Java}}{}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{captioncolor}{\parbox{\dimexpr\linewidth-2\fboxsep\relax}{#1#2#3}}}
\captionsetup[lstlisting]{
format = listing,
labelfont = white,
textfont = white
}
\lstdefinestyle{Java}{
basicstyle = \linespread{1.1}\mdseries,
backgroundcolor = \color{backgroundcolor},
breaklines = true,
commentstyle = \color{commentcolor},
identifierstyle = \color{identifiercolor},
keywordstyle = \color{keywordcolor},
language = Java,
literate =
{\_}{}{0\discretionary{\_}{}{\_}}
{Ä}{{\"A}}1
{Ö}{{\"O}}1
{Ü}{{\"U}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
{ü}{{\"u}}1
{ß}{{\ss}}1,
morekeywords = {assert},
numbers = left,
numbersep = 5pt,
numberstyle=\color{numbercolor},
showspaces = false,
showstringspaces = false,
showtabs = false,
stringstyle=\color{stringrcolor},
tabsize = 4,
title = Java~(\thejavacounter):~,
xleftmargin = \parindent
}
\begin{document}
\begin{java}
public static void main(String[] args) {
}
\end{java}
\begin{java}
public static void main(String[] args) {
}
\end{java}
\end{document}