2

This snippet of code is derived from Harvey Sheppard.

I was attempting to get his lstlisting environment to work and appear like this:

enter image description here

I am not getting the \codeimg to show up (the graphic before the code title). Can you tell me what has to be done so that the graphic will show up before the code title like the image above?

Thanks!

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\usepackage[no-math]{fontspec}

\usepackage{polyglossia}
\setdefaultlanguage{french}
\setotherlanguages{english}

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

\usepackage{metalogo} % typeset xelatex!
\usepackage{microtype}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{wrapfig}
\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages}
% \usepackage{fancyhdr}
\usepackage{amsmath, amsfonts, amssymb}
\usepackage{mdframed}
\usepackage{listings}
\definecolor{light-gray}{gray}{0.92}

% infoBulle skeleton
%\newenvironment{@infoBulleSkeletonEnv}[3]{
%   \begin{infoBulle}[style=#1, frametitle=\@titleContent{#2}{#3}]
%       \vspace*{-1mm}
%   }{
%\end{infoBulle}
%}
%\newcommand{\@infoBulleSkeleton}[4]{
%   \begin{@infoBulleSkeletonEnv}{#1}{#2}{#3}
%       #4
%   \end{@infoBulleSkeletonEnv}
%}
%\newcommand{\@codeTitle}{Title unchanged}
\newcommand{\codeTitle}[1]{\renewcommand{\@codeTitle}{#1}}

\BeforeBeginEnvironment{yCode}{
    \begin{@infoBulleSkeletonEnv}{code}{\codeimg}{\@codeTitle}
}

\AfterEndEnvironment{yCode}{
    \end{@infoBulleSkeletonEnv}
}

\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Coding environment (redefining lstlisting)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\renewcommand{\codeTitle}[2]{Code}
\renewcommand{\codeTitle}{\hspace*{3mm}\begin{minipage}{.75cm}
        \includegraphics[width=\linewidth]{\pathToInfoBulleImages code2.png}
    \end{minipage}\hspace*{1mm}\begin{minipage}{\textwidth-1.05cm}
    {\sffamily\Large \codeTitle}
\end{minipage}}

\renewcommand*{\lstlistlistingname}{Liste des codes}
\BeforeBeginEnvironment{lstlisting}{ %ce code met tous les lstlistings dans un mdframed
    \begin{mdframed}[
        linecolor=Gray,
        backgroundcolor=light-gray,
        skipabove=4mm,
        skipbelow=0mm,
        innertopmargin=2mm,
        innerbottommargin=0mm,
        innerleftmargin=0mm,
        innerrightmargin=10pt,
        leftmargin=0mm,
        rightline=false,
        topline=false,
        bottomline=false,
        linewidth=1mm
        ]
        \codeTitle
        \vspace*{-2mm}
    }
    \AfterEndEnvironment{lstlisting}{
    \end{mdframed}
}

\renewcommand\lstlistingname{Code}
\lstset{
    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,% marge ajouté à gauche du tableau (à configurer en dernier pour l'alignement global du tableau)
    framesep=2mm, %distance texte bord du cadre (limite de la background color)
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

\begin{document}

\renewcommand{\codeTitle}{Les bases de GDScript}
\begin{lstlisting}[caption=basics.gd]
var nombreDeGardes = 4 #déclaration d'une variable
nombreDeGardes = plusDeux(nombreDeGardes) #appel d'une fonction avec la variable nombreDeGardes passée en paramètre

func maFonction(): #cette ligne déclare une nouvelle fonction nommée maFonction
    var variable1 = 7 #nouvelle variable dont la valeur est 7
    variable2 = ['un', 'deux', 'trois'] #variable assignée à un tableau contenant trois chaines de caractères

func plusDeux(argument1):
    return argument1 + 2
\end{lstlisting}

\end{document} 
  • Wouldn't it be easier to use a tcolorbox-based listing (module listings)? – TeXnician Feb 10 '17 at 18:34
  • @TeXnician...thanks, can you offer some more guidance? – Joe Feb 10 '17 at 18:36
1

Here's another solution with tcolorbox. You can format the title freely (title attribute of new environment). It works with all engines.

The only advantages over Ignazi's version:

  • uses listingname from package
  • is breakable (over pages)

tcolorbox

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\usepackage[no-math]{fontspec}

\usepackage{polyglossia}
\setdefaultlanguage{french}
\setotherlanguages{english}

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

\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\definecolor{light-gray}{gray}{0.92}

\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red

\renewcommand\lstlistingname{Code}
\lstset{
    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,% marge ajouté à gauche du tableau (à configurer en dernier pour l'alignement global du tableau)
    framesep=2mm, %distance texte bord du cadre (limite de la background color)
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable,listings}
\newtcblisting[use counter=lstlisting]{codeblock}[2][]{%
        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\lstlistingname~\thelstlisting~--~continued},%
        listing only,listing options={xleftmargin=-1mm},after upper={\centering\strut\lstlistingname~\thelstlisting:~#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}

\begin{document}

\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

func maFonction(): #cette ligne déclare une nouvelle fonction nommée maFonction
    var variable1 = 7 #nouvelle variable dont la valeur est 7
    variable2 = ['un', 'deux', 'trois'] #variable assignée à un tableau contenant trois chaines de caractères

func plusDeux(argument1):
    return argument1 + 2
\end{codeblock}
\end{document} 
1

As I don't use lualatex or xelatex I don't know why following code doesn't work with them, but with pdflatex it shows how to define similar listing boxes with tcolorbox.

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage[most]{tcolorbox}
\definecolor{light-gray}{gray}{0.92}

\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red

\lstset{
    language=Python,
    numbers=left,
    numbersep= 5mm,
    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,% marge ajouté à gauche du tableau (à configurer en dernier pour l'alignement global du tableau)
    framesep=2mm, %distance texte bord du cadre (limite de la background color)
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

\newtcblisting[auto counter, number within=chapter]{mylisting}[3][]{%
    listing only, 
    enhanced, 
    sharp corners, 
    colframe=light-gray,
    colback=light-gray,
    coltitle=black,
    fonttitle=\sffamily\Large,
    overlay={%
            \fill[white] (frame.south west) rectangle ([xshift=5mm]frame.north west);
            \draw[line width=1mm,Gray] ([xshift=5mm]frame.north west)--([xshift=5mm]frame.south west);
    },
    title={\ \includegraphics[width=5mm]{example-image-a}\ #2},
    after upper={\par\centering \textsf{Code~\thetcbcounter:~#3}},
    #1
    }

\begin{document}

\chapter{Chapter title}

\section{Section title}

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

func maFonction(): #cette ligne déclare une nouvelle fonction nommée maFonction
    var variable1 = 7 #nouvelle variable dont la valeur est 7
    variable2 = ['un', 'deux', 'trois'] #variable assignée à un tableau contenant trois chaines de caractères

func plusDeux(argument1):
    return argument1 + 2
\end{mylisting}

\end{document} 

enter image description here

  • thanks for your input, but how come there is no color? The xcolor option is activated? How can you get the code to show up in color? Thanks. – Joe Feb 10 '17 at 21:05

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.