Defining a centered shadowboxed listings environment












9














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?










share|improve this question





























    9














    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?










    share|improve this question



























      9












      9








      9


      1





      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?










      share|improve this question















      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?







      listings environments boxes adjustbox fancybox






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 27 '18 at 15:28









      Martin Scharrer

      199k45632815




      199k45632815










      asked Jul 18 '13 at 18:48









      Joshua Taylor

      282217




      282217






















          2 Answers
          2






          active

          oldest

          votes


















          5














          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}





          share|improve this answer























          • This is very nice. I haven't used tcolorbox before, so it's a nice addition to the toolbox. It seems similar to fancybox's Sbox/TheSbox, and to adjustbox that works with a bgroup/egroup pair. The answer stands on its own and doesn't really need to be updated, but do you have any reference for why tcolorbox's box can hold a listing while the other package's boxes fail?
            – Joshua Taylor
            Jul 18 '13 at 20:29










          • Actually, while I like the look of the result, I'm having both some trouble understanding it, and getting it to work. What version of tcolorbox are you using? I'm using the version from TeXLive 2012, and most is an unknown option for tcolorbox. If I remove the most option, 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










          • The tcolorbox manual explains that tcbusetemplisting sets the content written to the temporary file tcbwritetemp as a listing. To use this, the example also needs tcbuselibrary{listings}. The manual also shows that most is a library shorthand for loading all the libraries except documentation. Even so, when I remove the most package 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:06










          • Of course, reading through the manual and poking in the source, I see that you're the author, so you probably know all this, some of these comments are for anyone else who comes along and finds this answer. Is the trick just that I need a later version of tcolorbox?
            – Joshua Taylor
            Jul 18 '13 at 21:06










          • @JoshuaTaylor: Sun is rising up in Europe and I see that you were busy while I was asleep ;-) First of all, the given proposal is a complete working example. But you need a recent version of tcolorbox; 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 6:09



















          1















          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.




          Well, adjustbox does not provide a shadow box by itself but allows the usage of other box commands using the precode key:



          documentclass[twocolumn]{article}

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

          lstnewenvironment{CenteredShadowboxListing}[1]{%
          noindentadjustbox{precode=shadowbox,center,vspace=bigskipamount}bgroup
          lstset{#1}%
          }{%
          egroup%
          }

          begin{document}
          lipsum[4]

          begin{CenteredShadowboxListing}[gobble=2]
          line of code
          line of code
          end{CenteredShadowboxListing}

          lipsum[4]

          end{document}


          Here the command form of adjustbox is used instead of the environment form, which is fine as adjustbox is special command which accepts bgroup .. egroup instead of { .. }. Using the environment form can cause issues in this nested definition.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "85"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f124601%2fdefining-a-centered-shadowboxed-listings-environment%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            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}





            share|improve this answer























            • This is very nice. I haven't used tcolorbox before, so it's a nice addition to the toolbox. It seems similar to fancybox's Sbox/TheSbox, and to adjustbox that works with a bgroup/egroup pair. The answer stands on its own and doesn't really need to be updated, but do you have any reference for why tcolorbox's box can hold a listing while the other package's boxes fail?
              – Joshua Taylor
              Jul 18 '13 at 20:29










            • Actually, while I like the look of the result, I'm having both some trouble understanding it, and getting it to work. What version of tcolorbox are you using? I'm using the version from TeXLive 2012, and most is an unknown option for tcolorbox. If I remove the most option, 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










            • The tcolorbox manual explains that tcbusetemplisting sets the content written to the temporary file tcbwritetemp as a listing. To use this, the example also needs tcbuselibrary{listings}. The manual also shows that most is a library shorthand for loading all the libraries except documentation. Even so, when I remove the most package 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:06










            • Of course, reading through the manual and poking in the source, I see that you're the author, so you probably know all this, some of these comments are for anyone else who comes along and finds this answer. Is the trick just that I need a later version of tcolorbox?
              – Joshua Taylor
              Jul 18 '13 at 21:06










            • @JoshuaTaylor: Sun is rising up in Europe and I see that you were busy while I was asleep ;-) First of all, the given proposal is a complete working example. But you need a recent version of tcolorbox; 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 6:09
















            5














            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}





            share|improve this answer























            • This is very nice. I haven't used tcolorbox before, so it's a nice addition to the toolbox. It seems similar to fancybox's Sbox/TheSbox, and to adjustbox that works with a bgroup/egroup pair. The answer stands on its own and doesn't really need to be updated, but do you have any reference for why tcolorbox's box can hold a listing while the other package's boxes fail?
              – Joshua Taylor
              Jul 18 '13 at 20:29










            • Actually, while I like the look of the result, I'm having both some trouble understanding it, and getting it to work. What version of tcolorbox are you using? I'm using the version from TeXLive 2012, and most is an unknown option for tcolorbox. If I remove the most option, 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










            • The tcolorbox manual explains that tcbusetemplisting sets the content written to the temporary file tcbwritetemp as a listing. To use this, the example also needs tcbuselibrary{listings}. The manual also shows that most is a library shorthand for loading all the libraries except documentation. Even so, when I remove the most package 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:06










            • Of course, reading through the manual and poking in the source, I see that you're the author, so you probably know all this, some of these comments are for anyone else who comes along and finds this answer. Is the trick just that I need a later version of tcolorbox?
              – Joshua Taylor
              Jul 18 '13 at 21:06










            • @JoshuaTaylor: Sun is rising up in Europe and I see that you were busy while I was asleep ;-) First of all, the given proposal is a complete working example. But you need a recent version of tcolorbox; 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 6:09














            5












            5








            5






            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}





            share|improve this 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}






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 26 '13 at 8:55

























            answered Jul 18 '13 at 19:33









            Thomas F. Sturm

            19.4k13171




            19.4k13171












            • This is very nice. I haven't used tcolorbox before, so it's a nice addition to the toolbox. It seems similar to fancybox's Sbox/TheSbox, and to adjustbox that works with a bgroup/egroup pair. The answer stands on its own and doesn't really need to be updated, but do you have any reference for why tcolorbox's box can hold a listing while the other package's boxes fail?
              – Joshua Taylor
              Jul 18 '13 at 20:29










            • Actually, while I like the look of the result, I'm having both some trouble understanding it, and getting it to work. What version of tcolorbox are you using? I'm using the version from TeXLive 2012, and most is an unknown option for tcolorbox. If I remove the most option, 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










            • The tcolorbox manual explains that tcbusetemplisting sets the content written to the temporary file tcbwritetemp as a listing. To use this, the example also needs tcbuselibrary{listings}. The manual also shows that most is a library shorthand for loading all the libraries except documentation. Even so, when I remove the most package 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:06










            • Of course, reading through the manual and poking in the source, I see that you're the author, so you probably know all this, some of these comments are for anyone else who comes along and finds this answer. Is the trick just that I need a later version of tcolorbox?
              – Joshua Taylor
              Jul 18 '13 at 21:06










            • @JoshuaTaylor: Sun is rising up in Europe and I see that you were busy while I was asleep ;-) First of all, the given proposal is a complete working example. But you need a recent version of tcolorbox; 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 6:09


















            • This is very nice. I haven't used tcolorbox before, so it's a nice addition to the toolbox. It seems similar to fancybox's Sbox/TheSbox, and to adjustbox that works with a bgroup/egroup pair. The answer stands on its own and doesn't really need to be updated, but do you have any reference for why tcolorbox's box can hold a listing while the other package's boxes fail?
              – Joshua Taylor
              Jul 18 '13 at 20:29










            • Actually, while I like the look of the result, I'm having both some trouble understanding it, and getting it to work. What version of tcolorbox are you using? I'm using the version from TeXLive 2012, and most is an unknown option for tcolorbox. If I remove the most option, 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










            • The tcolorbox manual explains that tcbusetemplisting sets the content written to the temporary file tcbwritetemp as a listing. To use this, the example also needs tcbuselibrary{listings}. The manual also shows that most is a library shorthand for loading all the libraries except documentation. Even so, when I remove the most package 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:06










            • Of course, reading through the manual and poking in the source, I see that you're the author, so you probably know all this, some of these comments are for anyone else who comes along and finds this answer. Is the trick just that I need a later version of tcolorbox?
              – Joshua Taylor
              Jul 18 '13 at 21:06










            • @JoshuaTaylor: Sun is rising up in Europe and I see that you were busy while I was asleep ;-) First of all, the given proposal is a complete working example. But you need a recent version of tcolorbox; 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 6:09
















            This is very nice. I haven't used tcolorbox before, so it's a nice addition to the toolbox. It seems similar to fancybox's Sbox/TheSbox, and to adjustbox that works with a bgroup/egroup pair. The answer stands on its own and doesn't really need to be updated, but do you have any reference for why tcolorbox's box can hold a listing while the other package's boxes fail?
            – Joshua Taylor
            Jul 18 '13 at 20:29




            This is very nice. I haven't used tcolorbox before, so it's a nice addition to the toolbox. It seems similar to fancybox's Sbox/TheSbox, and to adjustbox that works with a bgroup/egroup pair. The answer stands on its own and doesn't really need to be updated, but do you have any reference for why tcolorbox's box can hold a listing while the other package's boxes fail?
            – Joshua Taylor
            Jul 18 '13 at 20:29












            Actually, while I like the look of the result, I'm having both some trouble understanding it, and getting it to work. What version of tcolorbox are you using? I'm using the version from TeXLive 2012, and most is an unknown option for tcolorbox. If I remove the most option, 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




            Actually, while I like the look of the result, I'm having both some trouble understanding it, and getting it to work. What version of tcolorbox are you using? I'm using the version from TeXLive 2012, and most is an unknown option for tcolorbox. If I remove the most option, 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












            The tcolorbox manual explains that tcbusetemplisting sets the content written to the temporary file tcbwritetemp as a listing. To use this, the example also needs tcbuselibrary{listings}. The manual also shows that most is a library shorthand for loading all the libraries except documentation. Even so, when I remove the most package 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:06




            The tcolorbox manual explains that tcbusetemplisting sets the content written to the temporary file tcbwritetemp as a listing. To use this, the example also needs tcbuselibrary{listings}. The manual also shows that most is a library shorthand for loading all the libraries except documentation. Even so, when I remove the most package 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:06












            Of course, reading through the manual and poking in the source, I see that you're the author, so you probably know all this, some of these comments are for anyone else who comes along and finds this answer. Is the trick just that I need a later version of tcolorbox?
            – Joshua Taylor
            Jul 18 '13 at 21:06




            Of course, reading through the manual and poking in the source, I see that you're the author, so you probably know all this, some of these comments are for anyone else who comes along and finds this answer. Is the trick just that I need a later version of tcolorbox?
            – Joshua Taylor
            Jul 18 '13 at 21:06












            @JoshuaTaylor: Sun is rising up in Europe and I see that you were busy while I was asleep ;-) First of all, the given proposal is a complete working example. But you need a recent version of tcolorbox; 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 6:09




            @JoshuaTaylor: Sun is rising up in Europe and I see that you were busy while I was asleep ;-) First of all, the given proposal is a complete working example. But you need a recent version of tcolorbox; 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 6:09











            1















            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.




            Well, adjustbox does not provide a shadow box by itself but allows the usage of other box commands using the precode key:



            documentclass[twocolumn]{article}

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

            lstnewenvironment{CenteredShadowboxListing}[1]{%
            noindentadjustbox{precode=shadowbox,center,vspace=bigskipamount}bgroup
            lstset{#1}%
            }{%
            egroup%
            }

            begin{document}
            lipsum[4]

            begin{CenteredShadowboxListing}[gobble=2]
            line of code
            line of code
            end{CenteredShadowboxListing}

            lipsum[4]

            end{document}


            Here the command form of adjustbox is used instead of the environment form, which is fine as adjustbox is special command which accepts bgroup .. egroup instead of { .. }. Using the environment form can cause issues in this nested definition.






            share|improve this answer




























              1















              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.




              Well, adjustbox does not provide a shadow box by itself but allows the usage of other box commands using the precode key:



              documentclass[twocolumn]{article}

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

              lstnewenvironment{CenteredShadowboxListing}[1]{%
              noindentadjustbox{precode=shadowbox,center,vspace=bigskipamount}bgroup
              lstset{#1}%
              }{%
              egroup%
              }

              begin{document}
              lipsum[4]

              begin{CenteredShadowboxListing}[gobble=2]
              line of code
              line of code
              end{CenteredShadowboxListing}

              lipsum[4]

              end{document}


              Here the command form of adjustbox is used instead of the environment form, which is fine as adjustbox is special command which accepts bgroup .. egroup instead of { .. }. Using the environment form can cause issues in this nested definition.






              share|improve this answer


























                1












                1








                1







                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.




                Well, adjustbox does not provide a shadow box by itself but allows the usage of other box commands using the precode key:



                documentclass[twocolumn]{article}

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

                lstnewenvironment{CenteredShadowboxListing}[1]{%
                noindentadjustbox{precode=shadowbox,center,vspace=bigskipamount}bgroup
                lstset{#1}%
                }{%
                egroup%
                }

                begin{document}
                lipsum[4]

                begin{CenteredShadowboxListing}[gobble=2]
                line of code
                line of code
                end{CenteredShadowboxListing}

                lipsum[4]

                end{document}


                Here the command form of adjustbox is used instead of the environment form, which is fine as adjustbox is special command which accepts bgroup .. egroup instead of { .. }. Using the environment form can cause issues in this nested definition.






                share|improve this answer















                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.




                Well, adjustbox does not provide a shadow box by itself but allows the usage of other box commands using the precode key:



                documentclass[twocolumn]{article}

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

                lstnewenvironment{CenteredShadowboxListing}[1]{%
                noindentadjustbox{precode=shadowbox,center,vspace=bigskipamount}bgroup
                lstset{#1}%
                }{%
                egroup%
                }

                begin{document}
                lipsum[4]

                begin{CenteredShadowboxListing}[gobble=2]
                line of code
                line of code
                end{CenteredShadowboxListing}

                lipsum[4]

                end{document}


                Here the command form of adjustbox is used instead of the environment form, which is fine as adjustbox is special command which accepts bgroup .. egroup instead of { .. }. Using the environment form can cause issues in this nested definition.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 29 '18 at 9:36

























                answered Dec 27 '18 at 15:27









                Martin Scharrer

                199k45632815




                199k45632815






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f124601%2fdefining-a-centered-shadowboxed-listings-environment%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to send String Array data to Server using php in android

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Is anime1.com a legal site for watching anime?