I'm using listings to format blocks of code in my LaTeX, and I like it. But, except for large blocks of code, the border around the code block is bulky and breaks up the flow of how I read the document. I've tried searching around for a way to disable this code, but thus far I haven't found anything. Is it just impossible? Seems strange if this is the case.
[Tex/LaTex] Listings without a border
listings
Related Solutions
Here is a proposal using tcolorbox
. Colors, shadows, etc. can be adjusted to your liking. You may replace style=tcblatex
by any listings setting you want to have as default for your environment.
\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\newenvironment{CenteredShadowboxListing}[1][]{%
\tcbset{listing options={style=tcblatex,#1}}\tcbwritetemp}%
{\endtcbwritetemp%
\tcbox[enhanced,arc=0pt,outer arc=0pt,top=1mm,bottom=1mm,left=1mm,right=1mm,
boxrule=0.6pt,drop fuzzy shadow,before=\begin{center},after=\end{center}]%
{\tcbusetemplisting}}
\begin{document}
\lipsum[4]
\begin{CenteredShadowboxListing}
line of code
line of code
\end{CenteredShadowboxListing}
\lipsum[4]
\end{document}
UPDATE: With tcolorbox
version 2.41
of 2013/07/23
, the code to typset the example above can be written more compact with the same result:
\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}% version 2.41 or newer
\newtcblisting{CenteredShadowboxListing}[1][]{%
listing options={style=tcblatex,#1},hbox,listing only,
enhanced,arc=0pt,outer arc=0pt,top=1mm,bottom=1mm,left=1mm,right=1mm,
boxrule=0.6pt,drop fuzzy shadow,before=\begin{center},after=\end{center}}
\begin{document}
\lipsum[4]
\begin{CenteredShadowboxListing}
line of code
line of code
\end{CenteredShadowboxListing}
\lipsum[4]
\end{document}
With \lstdefinestyle
you can define as many styles as you wish; then you can use \lstnewenvironment
to define new listing environments using those new styles; in this way you can easily change the styles for your listings as required. You can still use \lstset
to set general settings (applicable to all the listings). A simple example:
\documentclass[]{article}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\lstset{ %<- general settings
columns=fixed,
basicstyle=\ttfamily,
frame=single,
keepspaces=true,
showstringspaces=false,
numbers=none}
\lstdefinestyle{myRstyle}{
backgroundcolor=\color[gray]{0.95},
}
\lstdefinestyle{mySstyle}{
backgroundcolor=\color{red!30},
}
\lstnewenvironment{myR}
{\lstset{language=R,style=myRstyle}}
{}
\lstnewenvironment{myS}
{\lstset{language=R,style=mySstyle}}
{}
\begin{document}
Here's some regular text.
\begin{lstlisting}
## speed dist
## Min. : 4.0 Min. : 2
## 1st Qu.:12.0 1st Qu.: 26
## Median :15.0 Median : 36
## Mean :15.4 Mean : 43
## 3rd Qu.:19.0 3rd Qu.: 56
## Max. :25.0 Max. :120
\end{lstlisting}
\begin{myR}
## speed dist
## Min. : 4.0 Min. : 2
## 1st Qu.:12.0 1st Qu.: 26
## Median :15.0 Median : 36
## Mean :15.4 Mean : 43
## 3rd Qu.:19.0 3rd Qu.: 56
## Max. :25.0 Max. :120
\end{myR}
\begin{myS}
## speed dist
## Min. : 4.0 Min. : 2
## 1st Qu.:12.0 1st Qu.: 26
## Median :15.0 Median : 36
## Mean :15.4 Mean : 43
## 3rd Qu.:19.0 3rd Qu.: 56
## Max. :25.0 Max. :120
\end{myS}
\end{document}
It's better to load xcolor
instead of color
; the usenames
option for xcolor
is deprecated and there's no need to use it.
Best Answer
Use the available options for the selected
lstlisting
environments; a litle example in which the style selected uses numbers to the left and a frame on the four sides of the listing; usingframe=<valid values>
andnumbers=<valid values>
one can change the behaviour for selected environments (for a list of all the keys and their<valid values>
refer to the package documentation):