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:

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
tabularenvironment to center the listing, which works, but in the present case, theshadowboxserves the same purpose (being just big enough to hold the listing). - How to center a lstlisting In this question the
tabulartechnique doesn't work, but the answer shows how to save the contents of the listing in anSbox(fromfancybox) and set it in aparboxin acenterline. - 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 uselstnewenvironment.
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?

tcolorboxbefore, so it's a nice addition to the toolbox. It seems similar tofancybox'sSbox/TheSbox, and toadjustboxthat works with abgroup/egrouppair. The answer stands on its own and doesn't really need to be updated, but do you have any reference for whytcolorbox's box can hold a listing while the other package's boxes fail? – Joshua Taylor Jul 18 '13 at 20:29tcolorboxare you using? I'm using the version from TeXLive 2012, andmostis an unknown option fortcolorbox. If I remove themostoption, then I get the error that! Undefined control sequence. <argument> \tcbusetemplisting. Finally, I don't see where in the code you provided that the listings package is getting used. Is the code you posted a complete working example? – Joshua Taylor Jul 18 '13 at 20:46\tcbusetemplistingsets the content written to the temporary filetcbwritetempas a listing. To use this, the example also needs\tcbuselibrary{listings}. The manual also shows thatmostis a library shorthand for loading all the libraries except documentation. Even so, when I remove themostpackage option and instead to\tcbuselibrary{most}, I get the error "Package pgfkeys Error: I do not know the key '/tcb/library/most' and I am going to ignore it." – Joshua Taylor Jul 18 '13 at 21:06tcolorbox? – Joshua Taylor Jul 18 '13 at 21:06tcolorbox; at least v2.31. The current one is v2.40 which I recommend, see How do I update my TeX distribution?. Updating will remove all problems. – Thomas F. Sturm Jul 19 '13 at 06:09\lstinputlisting. You could use this trick also in combination with the other boxing packages. Nevertheless,tcolorboxhas a special support for usinglistingswhich makes the code compact. – Thomas F. Sturm Jul 19 '13 at 06:16style=tcblatexby anylistingssetting you want to have as default for your environment. Note thatgobblewill not work because of using\lstinputlisting(restriction coming fromlistings). – Thomas F. Sturm Jul 19 '13 at 07:16