7

in the elegant solution provided by gernot found here, I wanted to reset the counters for the listings in each chapter. In the code's current configuration, the listings continue on from one chapter to the next.

Here is the code:

\documentclass[a4paper, 10pt]{book}

\newcommand{\codeimg}{\includegraphics[scale=0.1]{example-image-a}}

\newcounter{datacounter}[chapter]


\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{listings}
\usepackage{filecontents}
\usepackage[numbered,framed]{matlab-prettifier}

\definecolor{light-gray}{gray}{0.92}
\definecolor{myblueiii}{RGB}{199,234,253}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueiii}{RGB}{199,234,253}

\lstdefinestyle{mycode}{
    language=Python,
    numbers=left,
    numbersep= 10mm,
    numberstyle=\color{Black},
    stepnumber=1,
    tabsize=3,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    basicstyle=\color{Black}\ttfamily,
    commentstyle=\color{LimeGreen},
    keywordstyle=\color{BurntOrange}\bfseries,
    stringstyle=\color{WildStrawberry},
    keywords={var, func, extends},
    frame=leftline,
    framesep=0mm,
    xleftmargin=3mm,
    framesep=2mm,
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

\usepackage{tcolorbox}
\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}
\newtcblisting[use counter=pythoncode]{codeblock}[2][]{%listing options={style=mycode},%
        enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
        fonttitle=\bfseries,before upper={\hspace*{-1em}\includegraphics[height=\baselineskip]{example-image-a}~#2},%
        title after break={\centering\footnotesize\itshape\strut Python Code~\thechapter.\thepythoncode~--~continued},%
        listing only,listing options={xleftmargin=-1mm,#1,style=mycode},after upper={\centering\strut Python Code~\thechapter.\thepythoncode:~#2},
        frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},#1}
\newtcbinputlisting[use counter=data]{\inputdata}[3][]{%listing options={style=weka},%
    title after break={\centering\footnotesize\itshape\strut Data~\thechapter.\thedata~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=weka},after upper={\centering\strut Data~\thechapter.\thedata:~#2},%
     listing file={#3}}
\newtcbinputlisting[use counter=result]{\inputresult}[3][]{%listing options={style=mystyleresults},%
enhanced,noparskip,breakable,colback=myblueiii,opacitybacktitle=.8,%
    title after break={\centering\footnotesize\itshape\strut Result~\thechapter.\theresult~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=mystyleresults},after upper={\centering\strut Result~\thechapter.\theresult:~#2},%
     listing file={#3}}
\newtcbinputlisting[use counter=matlab]{\inputmatlab}[3][]{%listing options={style=Matlab-editor},%
    title after break={\centering\footnotesize\itshape\strut Matlab Code~\thechapter.\thematlab~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor},after upper={\centering\strut Matlab Code~\thechapter.\thematlab:~#2},%
     listing file={#3}}

\lstdefinestyle{mystyleresults}{
    commentstyle=\color{codegreen},
    keywordstyle=\color{black},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{black},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=none,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=6
}

\lstdefinestyle{weka}{
    %backgroundcolor=\color{DarkSlateGray},
    numbers=none,
    numbersep=5pt,
    numberstyle=\tiny\color{codegray},
    commentstyle=\color{codegreen},
    keywordstyle=\color{blue},
    keywordstyle={[2]\color{magenta}},
    stringstyle=\color{codepurple},
    basicstyle=\footnotesize,
    comment=[l]{\%},
    keywords={@relation,@attribute,@data},
    morekeywords=[2]{real,integer,numeric,string,date},
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}

\begin{filecontents}{result1.txt}
=== Run information ===

Correlation coefficient                  0.5941
Mean absolute error                      2.2173
Root mean squared error                  3.7905
Relative absolute error                 76.7091 %
Root relative squared error             81.3406 %
Total Number of Instances              188

\end{filecontents}

\begin{filecontents}{result2.txt}
% 9 attributes
% 188 instances

@relation FIT

@attribute NUMUORS real   % Number of unique operators
@attribute NUMUANDS real  % Number of unique operands
@attribute TOTOTORS real  % Total number of operators

\end{filecontents}

\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}


\begin{document}
\chapter{Data}
\section{Data1}
\inputdata{Code Snippet}{result2.txt}
\inputdata{Code Snippet}{result2.txt}
\inputresult{Result from run 1}{result1.txt}

\begin{codeblock}{Les bases de GDScript}
var nombreDeGardes = 4 #déclaration d'une variable
nombreDeGardes = plusDeux(nombreDeGardes) #appel d'une fonction avec la variable nombreDeGardes passée en paramètre
\end{codeblock}

\inputmatlab{Sample matlab code}{sample.m}

\chapter{Analysis}
\section{Data2}
\inputdata{Code Snippet}{result2.txt}
\inputdata{Code Snippet}{result2.txt}
\inputresult{Result from run 1}{result1.txt}
\end{document} 
  • 2
    Next time please remember to make your code a MWE (M stands for minimal!). – TeXnician Feb 12 '17 at 17:58
  • @TeXnician...you are correct...I sincerely apologize! – Joe Feb 12 '17 at 18:03
  • 2
    Please do not use a tag lstdefinestyle -- this is too localized and has nothing to do with your question/issue actually – user31729 Feb 12 '17 at 18:10
  • I think the tcolorbox key auto counter is more effective instead of defining the counter outside, unless the counters are defined already – user31729 Feb 14 '17 at 23:39
7

You introduce a counter you never use (datacounter) which is not necessary.

Probably you should switch to the chngcntr package. Then you could write (instead of the current counter definitions):

\usepackage{chngcntr}
\newcounter{data}\counterwithin{data}{chapter}
\newcounter{result}\counterwithin{result}{chapter}
\newcounter{pythoncode}\counterwithin{pythoncode}{chapter}
\newcounter{matlab}\counterwithin{matlab}{chapter}

Alternatively you could avoid extra packages and use

\makeatletter
\newcounter{data}\@addtoreset{data}{chapter} % equals \newcounter{data}[chapter]
\newcounter{result}\@addtoreset{result}{chapter} % equals \newcounter{result}[chapter]
\newcounter{pythoncode}\@addtoreset{pythoncode}{chapter} % equals \newcounter{pythoncode}[chapter]
\newcounter{matlab}\@addtoreset{matlab}{chapter} % equals \newcounter{matlab}[chapter]
\makeatother

And the third way (suggested by @clemens): Using amsmath and \numberwithin:

\usepackage{amsmath}
\newcounter{data}\numberwithin{data}{chapter}
\newcounter{result}\numberwithin{result}{chapter}
\newcounter{pythoncode}\numberwithin{pythoncode}{chapter}
\newcounter{matlab}\numberwithin{matlab}{chapter}
  • 1
    Third way: \usepackage{amsmath} and \numberwithin{data}{chapter} – cgnieder Feb 12 '17 at 18:04
  • 2
    \newcounter{data}\@addtoreset{data}{chapter} is a complicated way of saying \newcounter{data}[chapter] – cgnieder Feb 12 '17 at 18:07
  • 1
    @clemens You're right, I'll edit the post. – TeXnician Feb 12 '17 at 18:08
  • 2
    Knowing \@addtoreset is useful nonetheless :) – cgnieder Feb 12 '17 at 18:09
  • 1
    @TeXnician: Don't be silly. That's by far less effective ;D – user31729 Feb 12 '17 at 18:20
10

Use the number within key of the init (i.e. bootstrap) section of a \newtcblisting box, i.e. the first optional argument -- don't confuse it with the options that are given to the real tcblisting environment later on.

i.e \newtcblisting[use counter=foo,number within=chapter]{...}

\documentclass[a4paper, 10pt]{book}

\newcommand{\codeimg}{\includegraphics[scale=0.1]{example-image-a}}

\newcounter{datacounter}[chapter]


\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{listings}
\usepackage{filecontents}
\usepackage[numbered,framed]{matlab-prettifier}

\definecolor{light-gray}{gray}{0.92}
\definecolor{myblueiii}{RGB}{199,234,253}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueiii}{RGB}{199,234,253}

\lstdefinestyle{mycode}{
    language=Python,
    numbers=left,
    numbersep= 10mm,
    numberstyle=\color{Black},
    stepnumber=1,
    tabsize=3,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    basicstyle=\color{Black}\ttfamily,
    commentstyle=\color{LimeGreen},
    keywordstyle=\color{BurntOrange}\bfseries,
    stringstyle=\color{WildStrawberry},
    keywords={var, func, extends},
    frame=leftline,
    framesep=0mm,
    xleftmargin=3mm,
    framesep=2mm,
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

\usepackage{tcolorbox}
\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}
\newtcblisting[use counter=pythoncode,number within=chapter]{codeblock}[2][]{%listing options={style=mycode},%
        enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
        fonttitle=\bfseries,before upper={\hspace*{-1em}\includegraphics[height=\baselineskip]{example-image-a}~#2},%
        title after break={\centering\footnotesize\itshape\strut Python Code~\thechapter.\thepythoncode~--~continued},%
        listing only,listing options={xleftmargin=-1mm,#1,style=mycode},after upper={\centering\strut Python Code~\thechapter.\thepythoncode:~#2},
        frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},#1}
\newtcbinputlisting[use counter=data,number within=chapter]{\inputdata}[3][]{%listing options={style=weka},%
    title after break={\centering\footnotesize\itshape\strut Data~\thechapter.\thedata~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=weka},after upper={\centering\strut Data~\thechapter.\thedata:~#2},%
     listing file={#3}}
\newtcbinputlisting[use counter=result,number within=chapter]{\inputresult}[3][]{%listing options={style=mystyleresults},%
enhanced,noparskip,breakable,colback=myblueiii,opacitybacktitle=.8,%
    title after break={\centering\footnotesize\itshape\strut Result~\thechapter.\theresult~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=mystyleresults},after upper={\centering\strut Result~\thechapter.\theresult:~#2},%
     listing file={#3}}
\newtcbinputlisting[use counter=matlab,number within=chapter]{\inputmatlab}[3][]{%listing options={style=Matlab-editor},%
    title after break={\centering\footnotesize\itshape\strut Matlab Code~\thechapter.\thematlab~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor},after upper={\centering\strut Matlab Code~\thechapter.\thematlab:~#2},%
     listing file={#3}}

\lstdefinestyle{mystyleresults}{
    commentstyle=\color{codegreen},
    keywordstyle=\color{black},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{black},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=none,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=6
}

\lstdefinestyle{weka}{
    %backgroundcolor=\color{DarkSlateGray},
    numbers=none,
    numbersep=5pt,
    numberstyle=\tiny\color{codegray},
    commentstyle=\color{codegreen},
    keywordstyle=\color{blue},
    keywordstyle={[2]\color{magenta}},
    stringstyle=\color{codepurple},
    basicstyle=\footnotesize,
    comment=[l]{\%},
    keywords={@relation,@attribute,@data},
    morekeywords=[2]{real,integer,numeric,string,date},
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}

\begin{filecontents}{result1.txt}
=== Run information ===

Correlation coefficient                  0.5941
Mean absolute error                      2.2173
Root mean squared error                  3.7905
Relative absolute error                 76.7091 %
Root relative squared error             81.3406 %
Total Number of Instances              188

\end{filecontents}

\begin{filecontents}{result2.txt}
% 9 attributes
% 188 instances

@relation FIT

@attribute NUMUORS real   % Number of unique operators
@attribute NUMUANDS real  % Number of unique operands
@attribute TOTOTORS real  % Total number of operators

\end{filecontents}

\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}


\begin{document}
\chapter{Data}
\section{Data1}
\inputdata{Code Snippet}{result2.txt}
\inputdata{Code Snippet}{result2.txt}
\inputresult{Result from run 1}{result1.txt}

\begin{codeblock}{Les bases de GDScript}
var nombreDeGardes = 4 #déclaration d'une variable
nombreDeGardes = plusDeux(nombreDeGardes) #appel d'une fonction avec la variable nombreDeGardes passée en paramètre
\end{codeblock}

\inputmatlab{Sample matlab code}{sample.m}

\chapter{Analysis}
\section{Data2}
\inputdata{Code Snippet}{result2.txt}
\inputdata{Code Snippet}{result2.txt}
\inputresult{Result from run 1}{result1.txt}
\end{document} 

enter image description here

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.