wrap a lstlisting in a minipage in an ex in a newenvironment











up vote
3
down vote

favorite












I'm using expex for linguistic examples but in my work I sometimes want to include code snippets instead. I like wrapping code snippets in lstlistings with particular language defined syntax stuff. But to get everything to play nice (aligned at the top of the example number, no page breaks in the middle) , I put that into a minipage. By now, I end up with a large about of stuff:



exlabel{somelabel}
begin{minipage}[t]{.9linewidth}
begin{lstlisting}
some_code_in_here =
end{lstlisting}
end{minipage}
xe


That's a lot of boilerplate, so I'd like to define an environment to make it more convenient:



newenvironment{exlisting}[1]
{exlabel{#1}
begin{minipage}[t]{.9linewidth}
begin{lstlisting}
}
{end{minipage}
end{lstlisting}
xe
}


But that doesn't work, somehow, my latex ends up saying that it's missing a } (but I think that error is red herring).



Anyway, I believe I've boiled it down to a minimal problem which is that I can't actually wrap up lstlisting in an environment. This MWE illustrates the problem.



documentclass{article}
usepackage{listings}
newenvironment{mylisting}
{begin{lstlisting}}
{end{lstlisting}}
begin{document}
begin{mylisting}
somecode
end{mylisting}
end{document}


I did see the part of the listtings manual (5.16) which says that listings doesn't interact well with newenvironment and that is supposed to be the way to use listings with newenvironments. However, in that case, I end up with a different error, the dreaded *, preceded by a warning which is coming from the listings code. Another MWE:



documentclass{article}
usepackage{listings}
lstnewenvironment{mplisting}
{begin{minipage}[t]{.9linewidth}}
{end{minipage}}
begin{document}
begin{mplisting}
something = anotherthing
end{mplisting}
end{document}


If I try to compile this on my system with pdflatex it looks like this:



$ pdflatex mwe.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Debian) (preloaded format=pdflatex)
restricted write18 enabled.
entering extended mode
(./mwe.tex
LaTeX2e <2017/01/01> patch level 3
Babel <3.9r> and hyphenation patterns for 3 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/listings/listings.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/latex/listings/lstmisc.sty)
(/usr/share/texlive/texmf-dist/tex/latex/listings/listings.cfg)) (./mwe.aux)
(/usr/share/texlive/texmf-dist/tex/latex/base/omscmr.fd))
*



And just hangs at the *. So, I'm pretty lost. My first foray into environment defining hasn't really gotten me anywhere. As in-place code, my expex wrapping minipage wrapping listings looks great, but I have not made any progress on creating an environment to do some of that typing for me.










share|improve this question


























    up vote
    3
    down vote

    favorite












    I'm using expex for linguistic examples but in my work I sometimes want to include code snippets instead. I like wrapping code snippets in lstlistings with particular language defined syntax stuff. But to get everything to play nice (aligned at the top of the example number, no page breaks in the middle) , I put that into a minipage. By now, I end up with a large about of stuff:



    exlabel{somelabel}
    begin{minipage}[t]{.9linewidth}
    begin{lstlisting}
    some_code_in_here =
    end{lstlisting}
    end{minipage}
    xe


    That's a lot of boilerplate, so I'd like to define an environment to make it more convenient:



    newenvironment{exlisting}[1]
    {exlabel{#1}
    begin{minipage}[t]{.9linewidth}
    begin{lstlisting}
    }
    {end{minipage}
    end{lstlisting}
    xe
    }


    But that doesn't work, somehow, my latex ends up saying that it's missing a } (but I think that error is red herring).



    Anyway, I believe I've boiled it down to a minimal problem which is that I can't actually wrap up lstlisting in an environment. This MWE illustrates the problem.



    documentclass{article}
    usepackage{listings}
    newenvironment{mylisting}
    {begin{lstlisting}}
    {end{lstlisting}}
    begin{document}
    begin{mylisting}
    somecode
    end{mylisting}
    end{document}


    I did see the part of the listtings manual (5.16) which says that listings doesn't interact well with newenvironment and that is supposed to be the way to use listings with newenvironments. However, in that case, I end up with a different error, the dreaded *, preceded by a warning which is coming from the listings code. Another MWE:



    documentclass{article}
    usepackage{listings}
    lstnewenvironment{mplisting}
    {begin{minipage}[t]{.9linewidth}}
    {end{minipage}}
    begin{document}
    begin{mplisting}
    something = anotherthing
    end{mplisting}
    end{document}


    If I try to compile this on my system with pdflatex it looks like this:



    $ pdflatex mwe.tex
    This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Debian) (preloaded format=pdflatex)
    restricted write18 enabled.
    entering extended mode
    (./mwe.tex
    LaTeX2e <2017/01/01> patch level 3
    Babel <3.9r> and hyphenation patterns for 3 language(s) loaded.
    (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
    Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
    (/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
    (/usr/share/texlive/texmf-dist/tex/latex/listings/listings.sty
    (/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
    (/usr/share/texlive/texmf-dist/tex/latex/listings/lstmisc.sty)
    (/usr/share/texlive/texmf-dist/tex/latex/listings/listings.cfg)) (./mwe.aux)
    (/usr/share/texlive/texmf-dist/tex/latex/base/omscmr.fd))
    *



    And just hangs at the *. So, I'm pretty lost. My first foray into environment defining hasn't really gotten me anywhere. As in-place code, my expex wrapping minipage wrapping listings looks great, but I have not made any progress on creating an environment to do some of that typing for me.










    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I'm using expex for linguistic examples but in my work I sometimes want to include code snippets instead. I like wrapping code snippets in lstlistings with particular language defined syntax stuff. But to get everything to play nice (aligned at the top of the example number, no page breaks in the middle) , I put that into a minipage. By now, I end up with a large about of stuff:



      exlabel{somelabel}
      begin{minipage}[t]{.9linewidth}
      begin{lstlisting}
      some_code_in_here =
      end{lstlisting}
      end{minipage}
      xe


      That's a lot of boilerplate, so I'd like to define an environment to make it more convenient:



      newenvironment{exlisting}[1]
      {exlabel{#1}
      begin{minipage}[t]{.9linewidth}
      begin{lstlisting}
      }
      {end{minipage}
      end{lstlisting}
      xe
      }


      But that doesn't work, somehow, my latex ends up saying that it's missing a } (but I think that error is red herring).



      Anyway, I believe I've boiled it down to a minimal problem which is that I can't actually wrap up lstlisting in an environment. This MWE illustrates the problem.



      documentclass{article}
      usepackage{listings}
      newenvironment{mylisting}
      {begin{lstlisting}}
      {end{lstlisting}}
      begin{document}
      begin{mylisting}
      somecode
      end{mylisting}
      end{document}


      I did see the part of the listtings manual (5.16) which says that listings doesn't interact well with newenvironment and that is supposed to be the way to use listings with newenvironments. However, in that case, I end up with a different error, the dreaded *, preceded by a warning which is coming from the listings code. Another MWE:



      documentclass{article}
      usepackage{listings}
      lstnewenvironment{mplisting}
      {begin{minipage}[t]{.9linewidth}}
      {end{minipage}}
      begin{document}
      begin{mplisting}
      something = anotherthing
      end{mplisting}
      end{document}


      If I try to compile this on my system with pdflatex it looks like this:



      $ pdflatex mwe.tex
      This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Debian) (preloaded format=pdflatex)
      restricted write18 enabled.
      entering extended mode
      (./mwe.tex
      LaTeX2e <2017/01/01> patch level 3
      Babel <3.9r> and hyphenation patterns for 3 language(s) loaded.
      (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
      Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
      (/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
      (/usr/share/texlive/texmf-dist/tex/latex/listings/listings.sty
      (/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
      (/usr/share/texlive/texmf-dist/tex/latex/listings/lstmisc.sty)
      (/usr/share/texlive/texmf-dist/tex/latex/listings/listings.cfg)) (./mwe.aux)
      (/usr/share/texlive/texmf-dist/tex/latex/base/omscmr.fd))
      *



      And just hangs at the *. So, I'm pretty lost. My first foray into environment defining hasn't really gotten me anywhere. As in-place code, my expex wrapping minipage wrapping listings looks great, but I have not made any progress on creating an environment to do some of that typing for me.










      share|improve this question













      I'm using expex for linguistic examples but in my work I sometimes want to include code snippets instead. I like wrapping code snippets in lstlistings with particular language defined syntax stuff. But to get everything to play nice (aligned at the top of the example number, no page breaks in the middle) , I put that into a minipage. By now, I end up with a large about of stuff:



      exlabel{somelabel}
      begin{minipage}[t]{.9linewidth}
      begin{lstlisting}
      some_code_in_here =
      end{lstlisting}
      end{minipage}
      xe


      That's a lot of boilerplate, so I'd like to define an environment to make it more convenient:



      newenvironment{exlisting}[1]
      {exlabel{#1}
      begin{minipage}[t]{.9linewidth}
      begin{lstlisting}
      }
      {end{minipage}
      end{lstlisting}
      xe
      }


      But that doesn't work, somehow, my latex ends up saying that it's missing a } (but I think that error is red herring).



      Anyway, I believe I've boiled it down to a minimal problem which is that I can't actually wrap up lstlisting in an environment. This MWE illustrates the problem.



      documentclass{article}
      usepackage{listings}
      newenvironment{mylisting}
      {begin{lstlisting}}
      {end{lstlisting}}
      begin{document}
      begin{mylisting}
      somecode
      end{mylisting}
      end{document}


      I did see the part of the listtings manual (5.16) which says that listings doesn't interact well with newenvironment and that is supposed to be the way to use listings with newenvironments. However, in that case, I end up with a different error, the dreaded *, preceded by a warning which is coming from the listings code. Another MWE:



      documentclass{article}
      usepackage{listings}
      lstnewenvironment{mplisting}
      {begin{minipage}[t]{.9linewidth}}
      {end{minipage}}
      begin{document}
      begin{mplisting}
      something = anotherthing
      end{mplisting}
      end{document}


      If I try to compile this on my system with pdflatex it looks like this:



      $ pdflatex mwe.tex
      This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Debian) (preloaded format=pdflatex)
      restricted write18 enabled.
      entering extended mode
      (./mwe.tex
      LaTeX2e <2017/01/01> patch level 3
      Babel <3.9r> and hyphenation patterns for 3 language(s) loaded.
      (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
      Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
      (/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
      (/usr/share/texlive/texmf-dist/tex/latex/listings/listings.sty
      (/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
      (/usr/share/texlive/texmf-dist/tex/latex/listings/lstmisc.sty)
      (/usr/share/texlive/texmf-dist/tex/latex/listings/listings.cfg)) (./mwe.aux)
      (/usr/share/texlive/texmf-dist/tex/latex/base/omscmr.fd))
      *



      And just hangs at the *. So, I'm pretty lost. My first foray into environment defining hasn't really gotten me anywhere. As in-place code, my expex wrapping minipage wrapping listings looks great, but I have not made any progress on creating an environment to do some of that typing for me.







      listings environments minipage expex






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 31 at 6:09









      JawguyChooser

      1615




      1615






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          In the definition of lstnewenvironment you can use



          minipage[t]{.9linewidth}


          instead of begin{minipage}[t]{.9linewidth}. In the sample below I use such a definition where I have also inserted some negative vertical space to fit better with the label, the second item shows what your original coding produces.



          Sample output



          documentclass{article}

          usepackage{listings,expex}

          lstnewenvironment{mplisting}
          {minipage[t]{.9linewidth}vspace{-14pt}}
          {endminipage}

          begin{document}

          exlabel{newversion}
          begin{mplisting}
          something = anotherthing
          something = anotherthing
          end{mplisting}
          xe

          exlabel{oldversion}
          begin{minipage}[t]{.9linewidth}
          begin{lstlisting}
          something = anotherthing
          something = anotherthing
          end{lstlisting}
          end{minipage}
          xe

          end{document}


          Using this idea you can make an exlisting environment as follows:



          documentclass{article}

          usepackage{listings,expex}

          lstnewenvironment{exlisting}[1]{%
          exlabel{#1}minipage[t]{.9linewidth}vspace{-14pt}}
          {endminipagexe}

          begin{document}

          begin{exlisting}{name}
          something = anotherthing
          something = anotherthing
          end{exlisting}

          end{document}


          producing



          Second sample






          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',
            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%2f434253%2fwrap-a-lstlisting-in-a-minipage-in-an-ex-in-a-newenvironment%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            In the definition of lstnewenvironment you can use



            minipage[t]{.9linewidth}


            instead of begin{minipage}[t]{.9linewidth}. In the sample below I use such a definition where I have also inserted some negative vertical space to fit better with the label, the second item shows what your original coding produces.



            Sample output



            documentclass{article}

            usepackage{listings,expex}

            lstnewenvironment{mplisting}
            {minipage[t]{.9linewidth}vspace{-14pt}}
            {endminipage}

            begin{document}

            exlabel{newversion}
            begin{mplisting}
            something = anotherthing
            something = anotherthing
            end{mplisting}
            xe

            exlabel{oldversion}
            begin{minipage}[t]{.9linewidth}
            begin{lstlisting}
            something = anotherthing
            something = anotherthing
            end{lstlisting}
            end{minipage}
            xe

            end{document}


            Using this idea you can make an exlisting environment as follows:



            documentclass{article}

            usepackage{listings,expex}

            lstnewenvironment{exlisting}[1]{%
            exlabel{#1}minipage[t]{.9linewidth}vspace{-14pt}}
            {endminipagexe}

            begin{document}

            begin{exlisting}{name}
            something = anotherthing
            something = anotherthing
            end{exlisting}

            end{document}


            producing



            Second sample






            share|improve this answer



























              up vote
              1
              down vote













              In the definition of lstnewenvironment you can use



              minipage[t]{.9linewidth}


              instead of begin{minipage}[t]{.9linewidth}. In the sample below I use such a definition where I have also inserted some negative vertical space to fit better with the label, the second item shows what your original coding produces.



              Sample output



              documentclass{article}

              usepackage{listings,expex}

              lstnewenvironment{mplisting}
              {minipage[t]{.9linewidth}vspace{-14pt}}
              {endminipage}

              begin{document}

              exlabel{newversion}
              begin{mplisting}
              something = anotherthing
              something = anotherthing
              end{mplisting}
              xe

              exlabel{oldversion}
              begin{minipage}[t]{.9linewidth}
              begin{lstlisting}
              something = anotherthing
              something = anotherthing
              end{lstlisting}
              end{minipage}
              xe

              end{document}


              Using this idea you can make an exlisting environment as follows:



              documentclass{article}

              usepackage{listings,expex}

              lstnewenvironment{exlisting}[1]{%
              exlabel{#1}minipage[t]{.9linewidth}vspace{-14pt}}
              {endminipagexe}

              begin{document}

              begin{exlisting}{name}
              something = anotherthing
              something = anotherthing
              end{exlisting}

              end{document}


              producing



              Second sample






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                In the definition of lstnewenvironment you can use



                minipage[t]{.9linewidth}


                instead of begin{minipage}[t]{.9linewidth}. In the sample below I use such a definition where I have also inserted some negative vertical space to fit better with the label, the second item shows what your original coding produces.



                Sample output



                documentclass{article}

                usepackage{listings,expex}

                lstnewenvironment{mplisting}
                {minipage[t]{.9linewidth}vspace{-14pt}}
                {endminipage}

                begin{document}

                exlabel{newversion}
                begin{mplisting}
                something = anotherthing
                something = anotherthing
                end{mplisting}
                xe

                exlabel{oldversion}
                begin{minipage}[t]{.9linewidth}
                begin{lstlisting}
                something = anotherthing
                something = anotherthing
                end{lstlisting}
                end{minipage}
                xe

                end{document}


                Using this idea you can make an exlisting environment as follows:



                documentclass{article}

                usepackage{listings,expex}

                lstnewenvironment{exlisting}[1]{%
                exlabel{#1}minipage[t]{.9linewidth}vspace{-14pt}}
                {endminipagexe}

                begin{document}

                begin{exlisting}{name}
                something = anotherthing
                something = anotherthing
                end{exlisting}

                end{document}


                producing



                Second sample






                share|improve this answer














                In the definition of lstnewenvironment you can use



                minipage[t]{.9linewidth}


                instead of begin{minipage}[t]{.9linewidth}. In the sample below I use such a definition where I have also inserted some negative vertical space to fit better with the label, the second item shows what your original coding produces.



                Sample output



                documentclass{article}

                usepackage{listings,expex}

                lstnewenvironment{mplisting}
                {minipage[t]{.9linewidth}vspace{-14pt}}
                {endminipage}

                begin{document}

                exlabel{newversion}
                begin{mplisting}
                something = anotherthing
                something = anotherthing
                end{mplisting}
                xe

                exlabel{oldversion}
                begin{minipage}[t]{.9linewidth}
                begin{lstlisting}
                something = anotherthing
                something = anotherthing
                end{lstlisting}
                end{minipage}
                xe

                end{document}


                Using this idea you can make an exlisting environment as follows:



                documentclass{article}

                usepackage{listings,expex}

                lstnewenvironment{exlisting}[1]{%
                exlabel{#1}minipage[t]{.9linewidth}vspace{-14pt}}
                {endminipagexe}

                begin{document}

                begin{exlisting}{name}
                something = anotherthing
                something = anotherthing
                end{exlisting}

                end{document}


                producing



                Second sample







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 30 at 12:16

























                answered Nov 30 at 12:09









                Andrew Swann

                76.3k9128324




                76.3k9128324






























                    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%2f434253%2fwrap-a-lstlisting-in-a-minipage-in-an-ex-in-a-newenvironment%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

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?