2

So, I'm using the listings package to stylize source code (I'm currently using the solarized colour scheme).

I've set a frame to have a coloured background for the line numbers. I don't want this frame to be shown at the bottom of the code however, I want the border to show (a left and bottom border, but left frame only).

The style code is the following (along with some dummy code from the listings documentation). This is being used for multiple languages, therefore I have omitted the language selector.

\documentclass[a4paper,11pt]{article}
\usepackage[top=19mm, left=12.925mm, right=12.925mm, bottom=19mm]{geometry}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{xcolor-solarized}
\lstdefinestyle{mystyle}{
    basicstyle   = \ttfamily\color{solarized-base00},              % -- Making the font monospaced and small
    backgroundcolor=\color{solarized-base3},                       % -- Setting the background colour to solarized-base3
    breaklines=true,                                               % -- Allows automatic line breaking
    captionpos=b,                                                  % -- Caption position at bottom of listing
    commentstyle = \color{solarized-base1},                        % -- Sets comments to solarized-green
    escapeinside = {\%*}{*},                                       % -- Useful for adding LaTeX within the code
    emphstyle =\bfseries\color{solarized-red},                     % -- Emphasized text style to red
    frame=l,                                                       % -- Adds a line to the left of the listings, which is used for line numbers.
    framesep=2em,                                                  % -- Frame seperator size
    framerule=1pt,                                                 % -- Frame line size
    fillcolor=\color{solarized-base2},                             % -- Frame fill colour
    identifierstyle=\color{solarized-base00},                      % -- Style for Identifiers
    keepspaces=true,                                               % -- Keeps Spaces in text, useful for keeping identation of code. 
    keywordstyle = \color{solarized-cyan}\bfseries,                % -- Keyword style
    numbers=left,                                                  % -- Line numbers on the left 
    numbersep=0.5em,                                               % -- How far away should line numbers be from code
    numberstyle=\normalfont\footnotesize\color{solarized-violet},  % -- Line Number font style, size and colour 
    rulecolor=\color{solarized-blue},                              % -- if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (solarized-blue here))
    showspaces=false,                                              % -- Do not show spaces everywhere by adding particular underscores
    showtabs=fase,                                                 % -- Do not show tabs within strings adding particular underscores
    stringstyle=\color{solarized-blue},                            % -- String style
    tabsize=2,                                                     % -- Setting the tab size to 2 spaces.  
    xleftmargin=2em                                                % -- Margin from left of listing       
}
\begin{document}
\begin{lstlisting}
for i:=maxint to 0 do
  begin
    { do nothing }
  end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}
\end{document}

I have tried searching, and I have tried many different tweaks to my code. Basically, none of these solutions solve the problem I have. If you need more info, please let me know.

  • I think it is easier to use tcolorbox along with listings. Now I don't have time but tomorrow I will try an answer (if no one has answered till then). – user11232 Oct 30 '15 at 15:22
4

It turns out that I am not able to sleep. As promised, here is a version using tcolorbox and listings.

\documentclass[a4paper,11pt]{article}
\usepackage[top=19mm, left=12.925mm, right=12.925mm, bottom=19mm]{geometry}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{xcolor-solarized}
\lstdefinestyle{mystyle}{
    basicstyle   = \ttfamily\color{solarized-base00},              % -- Making the font monospaced and small
    %backgroundcolor=\color{solarized-base3},                       % -- Setting the background colour to solarized-base3
    breaklines=true,                                               % -- Allows automatic line breaking
    captionpos=b,                                                  % -- Caption position at bottom of listing
    commentstyle = \color{solarized-base1},                        % -- Sets comments to solarized-green
    escapeinside = {\%*}{*},                                       % -- Useful for adding LaTeX within the code
    emphstyle =\bfseries\color{solarized-red},                     % -- Emphasized text style to red
    identifierstyle=\color{solarized-base00},                      % -- Style for Identifiers
    keepspaces=true,                                               % -- Keeps Spaces in text, useful for keeping identation of code.
    keywordstyle = \color{solarized-cyan}\bfseries,                % -- Keyword style
    numbers=left,                                                  % -- Line numbers on the left
    numbersep=0.5em,                                               % -- How far away should line numbers be from code
    numberstyle=\normalfont\footnotesize\color{solarized-violet},  % -- Line Number font style, size and colour
    showspaces=false,                                              % -- Do not show spaces everywhere by adding particular underscores
    showtabs=fase,                                                 % -- Do not show tabs within strings adding particular underscores
    stringstyle=\color{solarized-blue},                            % -- String style
    tabsize=2,                                                     % -- Setting the tab size to 2 spaces.
}

\newcommand\sideline{
    \draw[line width=2em,solarized-base2]
      ([xshift=1em+1pt]frame.north west) --
      ([shift={(1em+1pt,1pt)}]frame.south west);
}

\usepackage[most]{tcolorbox}
\newtcblisting{mylisting}{%
nobeforeafter,
colback=solarized-base3,
colframe=solarized-blue,boxrule=0pt,leftrule=1pt,bottomrule=1pt,
boxsep=0pt,left=2em,top=-2mm,right=0pt,bottom=-2mm,arc=0pt,enhanced jigsaw,breakable,
listing options={style=mystyle},listing only,
overlay unbroken and first={\sideline},
overlay middle and last={\sideline}
}

\newtcbinputlisting{\myinputlisting}[2][]{%
listing file={#2},
nobeforeafter,
colback=solarized-base3,
colframe=solarized-blue,boxrule=0pt,leftrule=1pt,bottomrule=1pt,
boxsep=0pt,left=2em,top=-2mm,right=0pt,bottom=-2mm,arc=0pt,enhanced jigsaw,breakable,
listing options={style=mystyle},listing only,
overlay unbroken and first={\sideline},
overlay middle and last={\sideline},
#1}

\usepackage{filecontents}
\begin{filecontents*}{myfile.txt}
  for i:=maxint to 0 do
  begin
    { do nothing }
  end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{filecontents*}

\begin{document}
\begin{mylisting}
for i:=maxint to 0 do
  begin
    { do nothing }
  end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{mylisting}

\myinputlisting{myfile.txt}
\end{document}

enter image description here

  • Ok, thanks for this. I'd prefer to have the solution cover all forms of blocked listings, so apart from the environment, how can I add a listing from a file and make it look like a mylisting environment (or possibly become one automatically) – Mark Said Camilleri Oct 30 '15 at 22:44
  • 1
    @MarkSaidCamilleri See the edit. For more details, consult the tcolorbox manual. – user11232 Oct 30 '15 at 23:20
  • Thanks for this. It's just what I need. Though I had to change the top and bottom margins from -2mm to 0mm (but that's trivial) – Mark Said Camilleri Oct 31 '15 at 0:53

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.