4

This code is building from the solution posted here.

If this tcolorbox environment is used within a paragraph, the left gray line disappears...

enter image description here

How do you retain the look of the listing with the gray line to the left? Thanks.

Here is the code...

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\usepackage[no-math]{fontspec}
\usepackage{lipsum}
\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}\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}
\lipsum[1-4]
\par

\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
    Rather then frame code try with overlay – Salim Bou Feb 11 '17 at 6:30
  • 1
    Your problem is not the paragraph, but the page break. If it doesn't break the page the code is fine, but with page break tikz can't calculate the path. – TeXnician Feb 11 '17 at 7:09
  • 1
    @SalimBou Your version works fine. Please post it as an answer. – TeXnician Feb 11 '17 at 7:10
  • @SalimBou, can you please provide me with some more guidance as to how to implement your solution? Thanks! – Joe Feb 11 '17 at 18:25
  • 1
    Just replace frame code with overlay and the line appears again. On the second fragment the line also covers part of the continuation title. – Ignasi Feb 11 '17 at 20:12
2

Just for protocol the answer from the comments. Your problem is not the paragraph, but the page break. This causes the TikZ code in frame code to mess up. Your problem can be solved using overlay instead.

\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,
   overlay={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
   #1
  }

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.