0

I am using the listings package. With the code below I get a result like this enter image description here

I want to draw a line next to the line numbers like below. enter image description here

Also if possible I would like to change the background color of the line numbers.

The code I'm using is like below

\documentclass[12pt, a4paper]{report}
\usepackage{listings}
\usepackage{xcolor}

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

\lstdefinestyle{mystyle}{ basicstyle=\ttfamily\footnotesize, escapeinside={(}{)}, frame=single, breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true, showspaces=false,
showstringspaces=false, showtabs=false,
tabsize=2, numbers=left, numbersep=5pt, framexleftmargin=1.5em, xleftmargin=2em, backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen}, keywordstyle=\color{magenta}, numberstyle=\color{codegray}, stringstyle=\color{codepurple} } \lstset{style=mystyle}

\begin{document} \begin{lstlisting}[language=python] for j=2 to A.length key=A[j] i=j-1 while i>0 and A[i]>key A[i+1]=A[i] i=i-1 A[i+1]=key \end{lstlisting} \end{document}

Moses Kim
  • 183

1 Answers1

1
  1. I propose to use the package tcolorbox for the frame.

  2. The instructions in \lstdefinestyle{mystyle}

  3. %frame=single, %framexleftmargin=1.5em, %xleftmargin=2em, %backgroundcolor=\color{backcolour} are no more use

  4. The background color for the number is for example \colorlet{mycodebacknum}{gray!70}

    \documentclass{standalone}
    \usepackage{listings}
    \usepackage{xcolor}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \usepackage{tcolorbox}
    \tcbuselibrary{skins,listings,breakable}
    

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

    \lstdefinestyle{mystyle}{ basicstyle=\ttfamily\footnotesize, escapeinside={(}{)}, %frame=single, breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true, showspaces=false,
    showstringspaces=false, showtabs=false,
    tabsize=2, numbers=left, numbersep=5pt, %framexleftmargin=1.5em, %xleftmargin=2em, %backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen}, keywordstyle=\color{magenta}, numberstyle=\color{codegray}, stringstyle=\color{codepurple} } \lstset{style=mystyle}

    \colorlet{mycodebacktitre}{backcolour} \colorlet{mycodebacktrait}{gray!70} \colorlet{mycodebacknum}{gray!70} \newtcolorbox{mycode}[1]{ % breakable, hbox boxed title, enhanced, colbacktitle=mycodebacktitre, fonttitle=\bfseries, coltitle=black, attach boxed title to top center={yshift=-2mm}, %
    colback=mycodebacktitre, colframe=mycodebacktrait, % title={#1}, overlay={\begin{tcbclipinterior}\fill[mycodebacknum] (frame.south west) rectangle ([xshift=1.5em]frame.north west);\end{tcbclipinterior}}% pour colorer la zone numéro} } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{document} \begin{mycode}{title} \begin{lstlisting}[language=python] for j=2 to A.length key=A[j] i=j-1 while i>0 and A[i]>key A[i+1]=A[i] i=i-1 A[i+1]=key \end{lstlisting} \end{mycode} \end{document}

enter image description here

pascal974
  • 4,652