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.
Best Answer
My answer uses the
tcolorbox
package for assistence with the boxing problem. Note that you should use the current version3.12 (2014/07/29)
of the package, i.e. you may have to update first.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.You will see that the new environment
widelisting
also has no tiny white lines when viewed with a previewer.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:The output is:
As a variant, I use some color and additional lines here:
This gives: