TransWikia.com

Listings custom counter

TeX - LaTeX Asked by Nyanyan on December 15, 2020

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{dimexprlinewidth-2fboxseprelax}{#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 =
        {_}{}{0discretionary{_}{}{_}}
        {Ä}{{"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@@captionstepcounter{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!

One Answer

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{dimexprlinewidth-2fboxseprelax}{#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 =
        {_}{}{0discretionary{_}{}{_}}
        {Ä}{{"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}

Answered by John Kormylo on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP