1

In the code below, I lost the formatting and colors for my code environments. Can you please tell me how to get the colors and formatting according to the code that it is supposed to show? Thanks.

Code:

\documentclass[a4paper, 10pt]{report}

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

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

%\renewcommand\lstlistingname{Code}
\lstdefinestyle{mycode}{
    language=Python,
    numbers=left,
    numbersep= 7mm,
    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~\thepythoncode~--~continued},%
        listing only,listing options={xleftmargin=-1mm,#1},after upper={\centering\strut Python Code~\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~\thedata~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1},after upper={\centering\strut Data~\thedata:~#2},%
     listing file={#3}}
\newtcbinputlisting[use counter=result]{\inputresult}[3][]{listing options={style=mystyleresults},%
    title after break={\centering\footnotesize\itshape\strut Result~\theresult~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1},after upper={\centering\strut Result~\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~\thematlab~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1},after upper={\centering\strut Matlab Code~\thematlab:~#2},%
     listing file={#3}}

\lstdefinestyle{mystyleresults}{
    backgroundcolor=\color{backcolour},
    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,
    backgroundcolor=\color{myblueiii},
    tabsize=6
}

\lstdefinestyle{weka}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{blue},
    keywordstyle={[2]\color{magenta}},
    numberstyle=\tiny\color{codegray},
    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,
    numbers=left,
    numbersep=5pt,
    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}

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

\end{document} 
3
  • The obsession shifted from orange to some strange colour scheme of the listings -- don't you think that you waste too much time for eye-candy?
    – user31729
    Commented Feb 12, 2017 at 14:44
  • @ChristianHupfer, is it the color scheme that is the issue? If I stuck with the normal colors it should work?
    – Joe
    Commented Feb 12, 2017 at 14:59
  • You're wasting a lot of time for finding out schemes -- time you could use to write a thesis or whatever. Concentrate on the content first.
    – user31729
    Commented Feb 12, 2017 at 15:00

1 Answer 1

3

In the options of your \newtcblistings, you have the key listing options twice, with the second taking precedence. Add your style=... declarations to the second listing options.

enter image description here

\documentclass[a4paper, 10pt]{report}

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

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

%\renewcommand\lstlistingname{Code}
\lstdefinestyle{mycode}{
    language=Python,
    numbers=left,
    numbersep= 7mm,
    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~\thepythoncode~--~continued},%
        listing only,listing options={xleftmargin=-1mm,#1,style=mycode},after upper={\centering\strut Python Code~\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~\thedata~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=weka},after upper={\centering\strut Data~\thedata:~#2},%
     listing file={#3}}
\newtcbinputlisting[use counter=result]{\inputresult}[3][]{%listing options={style=mystyleresults},%
    title after break={\centering\footnotesize\itshape\strut Result~\theresult~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=mystyleresults},after upper={\centering\strut Result~\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~\thematlab~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor},after upper={\centering\strut Matlab Code~\thematlab:~#2},%
     listing file={#3}}

\lstdefinestyle{mystyleresults}{
    backgroundcolor=\color{backcolour},
    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,
    backgroundcolor=\color{myblueiii},
    tabsize=6
}

\lstdefinestyle{weka}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{blue},
    keywordstyle={[2]\color{magenta}},
    numberstyle=\tiny\color{codegray},
    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,
    numbers=left,
    numbersep=5pt,
    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}

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

\end{document} 
1
  • @gernot...THANK you for your solution. I sincerely appreciate it. How do you go about adding the listings to the TOC? I tried \lstlistoflistings but they do not show up. Thanks again!
    – Joe
    Commented Feb 12, 2017 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .