0

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!

3
  • 3
    Add singlelinecheck=false to the caption options for lstlisting.
    – user205700
    Oct 3, 2020 at 18:28
  • I can't find \providecaptionname in any package I have installed. Oct 3, 2020 at 19:21
  • It's defined in KOMA-Script: 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.
    – user205700
    Oct 3, 2020 at 20:21

1 Answer 1

0

This creates a java environment to move the \stepcounter outside lstlisting. See page 43 of manual.

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.