[Tex/LaTex] Defining a centered shadowboxed listings environment

adjustboxboxesenvironmentsfancyboxlistings

I'm trying to define a environment for code listings wrapped in a shadowbox and centered in the line. Using this code, I can generate the kind of output that I want:

\documentclass[twocolumn]{article}
\usepackage{listings}
\usepackage{fancybox}
\usepackage{lipsum}
\begin{document}
\lipsum[4]
\begin{center}
  \shadowbox{%
    \begin{lstlisting}[gobble=6]
      line of code
      line of code
    \end{lstlisting}%
  }
\end{center}
\lipsum[4]
\end{document}

This produces output like this:

code listing in a shadowbox in a center environment

My problem arises when trying to abstract this into an environment. I've seen the some related questions:

  • How to center a listing? The answer in this question suggests using tabular environment to center the listing, which works, but in the present case, the shadowbox serves the same purpose (being just big enough to hold the listing).
  • How to center a lstlisting In this question the tabular technique doesn't work, but the answer shows how to save the contents of the listing in an Sbox (from fancybox) and set it in a parbox in a centerline.
  • adjustbox and lstnewenvironment This question actually involves creating frames around listings, and seems to be the most relevant to my question. The answer depends on adjustbox, which supports a simple frame, but not the shadowbox from fancybox.
  • Why does pdfLaTeX fail when I try to use `begin{lstlisting}` inside a user-defined environment? The answer here points out that it's tough to define environments in which verbatim-like environments will appear, and so it's best to use lstnewenvironment.

Based on these approaches, I've gotten as far as unsuccessfully trying to use lstnewenvironment. Just trying to get a centered listing, as below, fails (error message follows):

\documentclass[twocolumn]{article}

\usepackage{listings}
\usepackage{fancybox}
\usepackage{lipsum}
\usepackage{adjustbox}

\lstnewenvironment{CenteredShadowboxListing}[1][]{%
  \begin{center}%
    \lstset{#1}%
  }{%
  \end{center}%
}

\begin{document}
\lipsum[4]
\begin{CenteredShadowboxListing}[gobble=2]
  line of code
  line of code
\end{CenteredShadowboxListing}
\lipsum[4]
\end{document}

This fails with the error:

! File ended while scanning use of \lst@BOLGobble@.
<inserted text> 
                \par 
<*> mwe.tex

How can I define an environment that includes a code listing, puts a shadowbox around it, and centers the box?

Best Answer

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}

enter image description here

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}