I've wanted to create custom listings style with its own counter. The problem I have right now is that the counter is not increased by 1 but by 2.
Here is an example: Output: First listings counter is 2 and second one is 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}
Thanks in advance!
singlelinecheck=false
to the caption options forlstlisting
.tex/latex/koma-script/scrbase.sty:\newcommand*{\providecaptionname}{%
But I don't see why switching the single-line-check off for listings affects the usage of\providecaptionname
, especially since your example code does not use a KOMA-Script class. Could you please update your example so it shows the problem? This would be kind.