此代码是从解决方案发布在这里。
如果tcolorbox
在段落内使用此环境,左边的灰线就会消失……
如何保留listing
左侧灰线的外观?谢谢。
这是代码...
\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
仅供参考,请参考评论中的答案。您的问题不是段落,而是分页符。这会导致 TikZ 代码frame code
混乱。您的问题可以使用overlay
以下方法解决。
\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
}