# Lstlisting: width of background box

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:

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.

• Welcome to TeX.SX! The white lines are just an artefact of your PDF viewer. They will appear and disappear as you zoom in and out; don't mind them. – jub0bs Aug 13 '14 at 20:47
• page means paper or text width? Now already the background color is spanning the text width. You can use linewidth key. Set linewidth=\textwidth in the option of turtle. – user11232 Aug 13 '14 at 23:22

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.

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:

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:

• Hi thank you for your answer. You clearly had a solution for the problem I posted. However in my document it won't apply the changes. Any idea where to start debugging? It is compiling without errors so maybe there is a package clash or some commands that prevent the change being applied. In my test document it is working so I will accept your answer. Thanks. – Scyla Aug 14 '14 at 21:19
• @Scyla If I understand you correct, your test document is based on my answer and this one works as expected on your system. But there is no visible change when you modify your target document, right? For debugging I would e.g. change the color setting in the widelisting clone you used - if this changes nothing, then - maybe - the wrong document is compiled (or included or something). This is my first guess since I clearly would expect some changes. If this is not the case, maybe you try to create a MWE with the problem and ask a new question with it; so we hopefully could help. – Thomas F. Sturm Aug 15 '14 at 9:12
• I found the problem. I didn't notice that in the colback variable in the wide listing environment. It was still set to light gray and because the MyLightGray is almost white I didn't notice the color change. After playing around with the color settings I finally figured it out. Thanks again you where really helpful! – Scyla Aug 15 '14 at 12:59
• Quick additional question. How can I define a label for references in the text? – Scyla Aug 15 '14 at 13:10
• @Scyla I'm glad that the problem could be resolved. Both environments of my answer accept two optional parameters. The first one is for setting listing options. The second one is for setting tcolorbox options. To define a label use label=something for the second option set, e.g. \begin{widelistingtitled}[language=turtle][label=test]{Wide Listing with Title}. – Thomas F. Sturm Aug 15 '14 at 13:59