3

This question is building from the solution given by TeXnician that can be found here. Note that this original design was devised by Harvey Sheppard.

I wanted to adjust the position of the graphic that appears next to the listing heading so that the graphic appears vertically centered with the text like this:

enter image description here

Here is TeXnician's original solution:

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

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

\newcommand{\codeimg}{\includegraphics[scale=0.05]{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}  
4

Use \raisebox{vertical shift}[extension above baseline][extension below baseline]{shifted contents}. In your example:

before upper={\hspace*{-1em}\raisebox{-.2\baselineskip}[0pt][0pt]{\includegraphics[height=\baselineskip]{example-image-a}}~#2}

enter image description here

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

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

\newcommand{\codeimg}{\includegraphics[scale=0.05]{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}\raisebox{-3pt}[0pt][0pt]{\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}  
  • 2
    Perhaps use something relative to the font size: \raisebox{-.2\baselineskip}[0pt][0pt]{\includegraphics[.]{..}}. – Werner Feb 10 '17 at 23:48
  • @Werner Good idea, done. – gernot says Reinstate Monica Feb 11 '17 at 0:04

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.