[Tex/LaTex] Lstlisting: width of background box

backgroundslistings

I'm trying to modify my listing environment in a way that the background color for the listing is expanded over the whole width of the page. So no white border left.

Is this possible?

I tried doing some research but it seems that I'm unable to find a solution.

Here is my current listing environment:

\documentclass{article}
\usepackage{color}
\usepackage{listings}

\definecolor{MyLightGray}{RGB}{200, 200,200}

\lstdefinelanguage{turtle}
{
    columns=fullflexible,
    keywordstyle=\color{red},
    morekeywords={@prefix,@base,@forSome,@forAll,@keywords},
    morecomment=[l]{\#},
    tabsize=4, 
    alsoletter={-?}, % allowed in names
    morecomment=[s][\color{blue}]{<}{>},
    basicstyle=\ttfamily\color{black}, 
    %numberstyle=\color{black},
    morestring=[b][\color{black}]\",    
    backgroundcolor=\color{MyLightGray},
}
\begin{document}

\begin{lstlisting}[language=turtle, captionpos=t]
@prefix something:              <http://example.org/something/> 

something:subject       something:predicate     something:object .
\end{lstlisting}
\end{document}

This is the output:

Current output

I don't want the code to be shown outside the text width only the background pane of the listing expanded .

Any help is appriciated

Quick addendum. I noticed on my pdf reader that there are small white lines between the lines of code. They don't show up in print but that might be due to the light gray background. Is this just an optical illusion or will they be more prominent when I choose a darker background color.

Best Answer

My answer uses the tcolorbox package for assistence with the boxing problem. Note that you should use the current version 3.12 (2014/07/29) of the package, i.e. you may have to update first.

Version 1:

The basic idea of the solution is to box the listing with a tcolorbox which is enlarged into the margins of the page. Here, I used a brutal enlargement of 5cm to both sides without fine-tuning to the real margins. But, if your actual margins are not larger than 5cm, this should work also for two-sided documents.

\documentclass{article}
\usepackage{color}
\usepackage[listings,skins,breakable,xparse]{tcolorbox}

\definecolor{MyLightGray}{RGB}{200, 200,200}

\lstdefinelanguage{turtle}
{
    columns=fullflexible,
    keywordstyle=\color{red},
    morekeywords={@prefix,@base,@forSome,@forAll,@keywords},
    morecomment=[l]{\#},
    tabsize=4,
    alsoletter={-?}, % allowed in names
    morecomment=[s][\color{blue}]{<}{>},
    basicstyle=\ttfamily\color{black},
    %numberstyle=\color{black},
    morestring=[b][\color{black}]\",
    backgroundcolor=\color{MyLightGray},
}

\NewTCBListing{widelisting}{ O{} O{} }{
  listing only,breakable,listing options={#1,aboveskip=0pt,belowskip=0pt},
  enhanced,sharp corners,size=tight,boxrule=0pt,frame hidden,
  width=\textwidth+10cm,
  enlarge left by=-5cm,left=5cm,
  enlarge right by=-5cm,right=5cm,
  colback=MyLightGray,
  #2 }


\begin{document}

\begin{lstlisting}[language=turtle, captionpos=t]
@prefix something:              <http://example.org/something/>

something:subject       something:predicate     something:object .
\end{lstlisting}


\begin{widelisting}[language=turtle]
@prefix something:              <http://example.org/something/>

something:subject       something:predicate     something:object .
\end{widelisting}

\end{document}

You will see that the new environment widelisting also has no tiny white lines when viewed with a previewer. enter image description here

Version 2:

If you want to have a caption or title for your listing, you have to add some modifications. To mimic the original lstlisting, the following could be used which blends with normal lisitings:

\documentclass{article}
\usepackage{color}
\usepackage[listings,skins,breakable,xparse]{tcolorbox}

\definecolor{MyLightGray}{RGB}{200, 200,200}

\lstdefinelanguage{turtle}
{
    columns=fullflexible,
    keywordstyle=\color{red},
    morekeywords={@prefix,@base,@forSome,@forAll,@keywords},
    morecomment=[l]{\#},
    tabsize=4,
    alsoletter={-?}, % allowed in names
    morecomment=[s][\color{blue}]{<}{>},
    basicstyle=\ttfamily\color{black},
    %numberstyle=\color{black},
    morestring=[b][\color{black}]\",
    backgroundcolor=\color{MyLightGray},
}

\NewTCBListing[use counter=lstlisting,list inside=lol,list type=lstlisting]{widelistingtitled}{ O{} O{} m }{
  listing only,breakable,listing options={#1,aboveskip=0pt,belowskip=0pt},
  enhanced,sharp corners,size=tight,boxrule=0pt,frame hidden,
  width=\textwidth+10cm,
  enlarge left by=-5cm,left=5cm,
  enlarge right by=-5cm,right=5cm,
  colback=MyLightGray,
  title=Listing~\thetcbcounter:~#3,
  list entry={\protect\numberline{\thetcbcounter}#3},
  attach boxed title to top center,coltitle=black,
  boxed title style={width=textwidth,empty},
  #2 }


\begin{document}

\lstlistoflistings

\begin{lstlisting}[language=turtle, captionpos=t, caption=Original Example]
@prefix something:              <http://example.org/something/>

something:subject       something:predicate     something:object .
\end{lstlisting}

\begin{widelistingtitled}[language=turtle]{Wide Listing with Title}
@prefix something:              <http://example.org/something/>

something:subject       something:predicate     something:object .
\end{widelistingtitled}

\end{document}

The output is:

enter image description here

Version 3:

As a variant, I use some color and additional lines here:

\documentclass{article}
\usepackage{color}
\usepackage[listings,skins,breakable,xparse]{tcolorbox}

\definecolor{MyLightGray}{RGB}{200, 200,200}

\lstdefinelanguage{turtle}
{
    columns=fullflexible,
    keywordstyle=\color{red},
    morekeywords={@prefix,@base,@forSome,@forAll,@keywords},
    morecomment=[l]{\#},
    tabsize=4,
    alsoletter={-?}, % allowed in names
    morecomment=[s][\color{blue}]{<}{>},
    basicstyle=\ttfamily\color{black},
    %numberstyle=\color{black},
    morestring=[b][\color{black}]\",
    backgroundcolor=\color{MyLightGray},
}

\NewTCBListing[use counter=lstlisting,list inside=lol,list type=lstlisting]{widelistingtitled}{ O{} O{} m }{
  listing only,breakable,listing options={#1,aboveskip=0pt,belowskip=0pt},
  enhanced,sharp corners,size=tight,top=1mm,bottom=1mm,boxrule=0.5mm,
  colframe=MyLightGray!50!blue,
  width=\textwidth+10cm,
  enlarge left by=-5cm,left=5cm,
  enlarge right by=-5cm,right=5cm,
  colback=MyLightGray,
  title=Listing~\thetcbcounter:~#3,
  list entry={\protect\numberline{\thetcbcounter}#3},
  attach boxed title to top center={yshift=-0.4mm},coltitle=black,
  boxed title style={width=textwidth,colback=blue!15!white,sharp corners=south},
  #2 }


\begin{document}

\lstlistoflistings

\begin{lstlisting}[language=turtle, captionpos=t, caption=Original Example]
@prefix something:              <http://example.org/something/>

something:subject       something:predicate     something:object .
\end{lstlisting}

\begin{widelistingtitled}[language=turtle]{Wide Listing with Title}
@prefix something:              <http://example.org/something/>

something:subject       something:predicate     something:object .
\end{widelistingtitled}

\end{document}

This gives: enter image description here

Related Question