LaTeX 2e environment that “knows” the name of surrounding environment












5














I'd like to have a LaTeX 2e-environment which when occurring inside another environment "knows" the name of the surrounding environment.
Is this possible?



I know that @currenvir provides the name of the current environment.



I seek some sort of @surroundenvirwhich provides the name of the surrounding environment.



E.g.



documentclass{article}
makeatletter
newenvironment{FOO}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
{message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
newenvironment{BAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
{message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
newenvironment{FOOBAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
{message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
makeatother
begin{document}
begin{FOO}
end{FOO}
begin{BAR}
end{BAR}
begin{FOOBAR}
end{FOOBAR}
begin{FOO}
begin{BAR}
begin{FOOBAR}
end{FOOBAR}
end{BAR}
end{FOO}
end{document}


shall deliver console output:



FOO-begin: Surrounding environment's name is document
FOO-end: Surrounding environment's name is document
BAR-begin: Surrounding environment's name is document
BAR-end: Surrounding environment's name is document
FOOBAR-begin: Surrounding environment's name is document
FOOBAR-end: Surrounding environment's name is document
FOO-begin: Surrounding environment's name is document
BAR-begin: Surrounding environment's name is FOO
FOOBAR-begin: Surrounding environment's name is BAR
FOOBAR-end: Surrounding environment's name is BAR
BAR-end: Surrounding environment's name is FOO
FOO-end: Surrounding environment's name is document









share|improve this question







New contributor




James Gambolputty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    5














    I'd like to have a LaTeX 2e-environment which when occurring inside another environment "knows" the name of the surrounding environment.
    Is this possible?



    I know that @currenvir provides the name of the current environment.



    I seek some sort of @surroundenvirwhich provides the name of the surrounding environment.



    E.g.



    documentclass{article}
    makeatletter
    newenvironment{FOO}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
    {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
    newenvironment{BAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
    {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
    newenvironment{FOOBAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
    {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
    makeatother
    begin{document}
    begin{FOO}
    end{FOO}
    begin{BAR}
    end{BAR}
    begin{FOOBAR}
    end{FOOBAR}
    begin{FOO}
    begin{BAR}
    begin{FOOBAR}
    end{FOOBAR}
    end{BAR}
    end{FOO}
    end{document}


    shall deliver console output:



    FOO-begin: Surrounding environment's name is document
    FOO-end: Surrounding environment's name is document
    BAR-begin: Surrounding environment's name is document
    BAR-end: Surrounding environment's name is document
    FOOBAR-begin: Surrounding environment's name is document
    FOOBAR-end: Surrounding environment's name is document
    FOO-begin: Surrounding environment's name is document
    BAR-begin: Surrounding environment's name is FOO
    FOOBAR-begin: Surrounding environment's name is BAR
    FOOBAR-end: Surrounding environment's name is BAR
    BAR-end: Surrounding environment's name is FOO
    FOO-end: Surrounding environment's name is document









    share|improve this question







    New contributor




    James Gambolputty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      5












      5








      5







      I'd like to have a LaTeX 2e-environment which when occurring inside another environment "knows" the name of the surrounding environment.
      Is this possible?



      I know that @currenvir provides the name of the current environment.



      I seek some sort of @surroundenvirwhich provides the name of the surrounding environment.



      E.g.



      documentclass{article}
      makeatletter
      newenvironment{FOO}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
      {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
      newenvironment{BAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
      {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
      newenvironment{FOOBAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
      {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
      makeatother
      begin{document}
      begin{FOO}
      end{FOO}
      begin{BAR}
      end{BAR}
      begin{FOOBAR}
      end{FOOBAR}
      begin{FOO}
      begin{BAR}
      begin{FOOBAR}
      end{FOOBAR}
      end{BAR}
      end{FOO}
      end{document}


      shall deliver console output:



      FOO-begin: Surrounding environment's name is document
      FOO-end: Surrounding environment's name is document
      BAR-begin: Surrounding environment's name is document
      BAR-end: Surrounding environment's name is document
      FOOBAR-begin: Surrounding environment's name is document
      FOOBAR-end: Surrounding environment's name is document
      FOO-begin: Surrounding environment's name is document
      BAR-begin: Surrounding environment's name is FOO
      FOOBAR-begin: Surrounding environment's name is BAR
      FOOBAR-end: Surrounding environment's name is BAR
      BAR-end: Surrounding environment's name is FOO
      FOO-end: Surrounding environment's name is document









      share|improve this question







      New contributor




      James Gambolputty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'd like to have a LaTeX 2e-environment which when occurring inside another environment "knows" the name of the surrounding environment.
      Is this possible?



      I know that @currenvir provides the name of the current environment.



      I seek some sort of @surroundenvirwhich provides the name of the surrounding environment.



      E.g.



      documentclass{article}
      makeatletter
      newenvironment{FOO}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
      {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
      newenvironment{BAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
      {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
      newenvironment{FOOBAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
      {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
      makeatother
      begin{document}
      begin{FOO}
      end{FOO}
      begin{BAR}
      end{BAR}
      begin{FOOBAR}
      end{FOOBAR}
      begin{FOO}
      begin{BAR}
      begin{FOOBAR}
      end{FOOBAR}
      end{BAR}
      end{FOO}
      end{document}


      shall deliver console output:



      FOO-begin: Surrounding environment's name is document
      FOO-end: Surrounding environment's name is document
      BAR-begin: Surrounding environment's name is document
      BAR-end: Surrounding environment's name is document
      FOOBAR-begin: Surrounding environment's name is document
      FOOBAR-end: Surrounding environment's name is document
      FOO-begin: Surrounding environment's name is document
      BAR-begin: Surrounding environment's name is FOO
      FOOBAR-begin: Surrounding environment's name is BAR
      FOOBAR-end: Surrounding environment's name is BAR
      BAR-end: Surrounding environment's name is FOO
      FOO-end: Surrounding environment's name is document






      macros environments nesting






      share|improve this question







      New contributor




      James Gambolputty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      James Gambolputty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      James Gambolputty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Dec 24 at 17:33









      James Gambolputty

      261




      261




      New contributor




      James Gambolputty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      James Gambolputty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      James Gambolputty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          6














          Environments are usually called using a begin-end clause. You can patch begin (using etoolbox) to store the value of @currenvir before it is updated:



          documentclass{article}

          usepackage{etoolbox}

          makeatletter
          % patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
          patchcmd{begin}{begingroup}{begingrouplet@surroundenvir@currenvir}{}{}

          newenvironment{FOO}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
          {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
          newenvironment{BAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
          {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
          newenvironment{FOOBAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
          {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
          makeatother

          begin{document}

          begin{FOO}
          end{FOO}
          begin{BAR}
          end{BAR}
          begin{FOOBAR}
          end{FOOBAR}
          begin{FOO}
          begin{BAR}
          begin{FOOBAR}
          end{FOOBAR}
          end{BAR}
          end{FOO}

          end{document}


          This yields in the .log:




          FOO-begin: Surrounding environment's name is document
          FOO-end: Surrounding environment's name is document
          BAR-begin: Surrounding environment's name is document
          BAR-end: Surrounding environment's name is document
          FOOBAR-begin: Surrounding environment's name is document
          FOOBAR-end: Surrounding environment's name is document
          FOO-begin: Surrounding environment's name is document
          BAR-begin: Surrounding environment's name is FOO
          FOOBAR-begin: Surrounding environment's name is BAR
          FOOBAR-end: Surrounding environment's name is BAR
          BAR-end: Surrounding environment's name is FOO
          FOO-end: Surrounding environment's name is document


          Note that @currenvir defaults to document, set inside the LaTeX2e kernel.






          share|improve this answer





























            3














            Example "test.tex":



            documentclass{article}
            usepackage{etoolbox}
            makeatletter
            newcommand*@surroundenvir{}
            newcommand*@surroundenvirtemp{}
            BeforeBeginEnvironment{FOO}{let@surroundenvirtemp=@currenvir}
            AtBeginEnvironment{FOO}{let@surroundenvir=@surroundenvirtemp}
            BeforeBeginEnvironment{BAR}{let@surroundenvirtemp=@currenvir}
            AtBeginEnvironment{BAR}{let@surroundenvir=@surroundenvirtemp}
            BeforeBeginEnvironment{FOOBAR}{let@surroundenvirtemp=@currenvir}
            AtBeginEnvironment{FOOBAR}{let@surroundenvir=@surroundenvirtemp}
            newenvironment{FOO}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
            {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
            newenvironment{BAR}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
            {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
            newenvironment{FOOBAR}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
            {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
            makeatother

            begin{document}

            begin{FOO}
            end{FOO}

            begin{BAR}
            end{BAR}

            begin{FOOBAR}
            end{FOOBAR}

            begin{FOO}
            begin{BAR}
            begin{FOOBAR}
            end{FOOBAR}
            end{BAR}
            end{FOO}


            end{document}


            enter image description here






            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
              });


              }
              });






              James Gambolputty is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f467211%2flatex-2e-environment-that-knows-the-name-of-surrounding-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









              6














              Environments are usually called using a begin-end clause. You can patch begin (using etoolbox) to store the value of @currenvir before it is updated:



              documentclass{article}

              usepackage{etoolbox}

              makeatletter
              % patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
              patchcmd{begin}{begingroup}{begingrouplet@surroundenvir@currenvir}{}{}

              newenvironment{FOO}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
              {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
              newenvironment{BAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
              {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
              newenvironment{FOOBAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
              {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
              makeatother

              begin{document}

              begin{FOO}
              end{FOO}
              begin{BAR}
              end{BAR}
              begin{FOOBAR}
              end{FOOBAR}
              begin{FOO}
              begin{BAR}
              begin{FOOBAR}
              end{FOOBAR}
              end{BAR}
              end{FOO}

              end{document}


              This yields in the .log:




              FOO-begin: Surrounding environment's name is document
              FOO-end: Surrounding environment's name is document
              BAR-begin: Surrounding environment's name is document
              BAR-end: Surrounding environment's name is document
              FOOBAR-begin: Surrounding environment's name is document
              FOOBAR-end: Surrounding environment's name is document
              FOO-begin: Surrounding environment's name is document
              BAR-begin: Surrounding environment's name is FOO
              FOOBAR-begin: Surrounding environment's name is BAR
              FOOBAR-end: Surrounding environment's name is BAR
              BAR-end: Surrounding environment's name is FOO
              FOO-end: Surrounding environment's name is document


              Note that @currenvir defaults to document, set inside the LaTeX2e kernel.






              share|improve this answer


























                6














                Environments are usually called using a begin-end clause. You can patch begin (using etoolbox) to store the value of @currenvir before it is updated:



                documentclass{article}

                usepackage{etoolbox}

                makeatletter
                % patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
                patchcmd{begin}{begingroup}{begingrouplet@surroundenvir@currenvir}{}{}

                newenvironment{FOO}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
                {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
                newenvironment{BAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
                {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
                newenvironment{FOOBAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
                {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
                makeatother

                begin{document}

                begin{FOO}
                end{FOO}
                begin{BAR}
                end{BAR}
                begin{FOOBAR}
                end{FOOBAR}
                begin{FOO}
                begin{BAR}
                begin{FOOBAR}
                end{FOOBAR}
                end{BAR}
                end{FOO}

                end{document}


                This yields in the .log:




                FOO-begin: Surrounding environment's name is document
                FOO-end: Surrounding environment's name is document
                BAR-begin: Surrounding environment's name is document
                BAR-end: Surrounding environment's name is document
                FOOBAR-begin: Surrounding environment's name is document
                FOOBAR-end: Surrounding environment's name is document
                FOO-begin: Surrounding environment's name is document
                BAR-begin: Surrounding environment's name is FOO
                FOOBAR-begin: Surrounding environment's name is BAR
                FOOBAR-end: Surrounding environment's name is BAR
                BAR-end: Surrounding environment's name is FOO
                FOO-end: Surrounding environment's name is document


                Note that @currenvir defaults to document, set inside the LaTeX2e kernel.






                share|improve this answer
























                  6












                  6








                  6






                  Environments are usually called using a begin-end clause. You can patch begin (using etoolbox) to store the value of @currenvir before it is updated:



                  documentclass{article}

                  usepackage{etoolbox}

                  makeatletter
                  % patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
                  patchcmd{begin}{begingroup}{begingrouplet@surroundenvir@currenvir}{}{}

                  newenvironment{FOO}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
                  {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
                  newenvironment{BAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
                  {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
                  newenvironment{FOOBAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
                  {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
                  makeatother

                  begin{document}

                  begin{FOO}
                  end{FOO}
                  begin{BAR}
                  end{BAR}
                  begin{FOOBAR}
                  end{FOOBAR}
                  begin{FOO}
                  begin{BAR}
                  begin{FOOBAR}
                  end{FOOBAR}
                  end{BAR}
                  end{FOO}

                  end{document}


                  This yields in the .log:




                  FOO-begin: Surrounding environment's name is document
                  FOO-end: Surrounding environment's name is document
                  BAR-begin: Surrounding environment's name is document
                  BAR-end: Surrounding environment's name is document
                  FOOBAR-begin: Surrounding environment's name is document
                  FOOBAR-end: Surrounding environment's name is document
                  FOO-begin: Surrounding environment's name is document
                  BAR-begin: Surrounding environment's name is FOO
                  FOOBAR-begin: Surrounding environment's name is BAR
                  FOOBAR-end: Surrounding environment's name is BAR
                  BAR-end: Surrounding environment's name is FOO
                  FOO-end: Surrounding environment's name is document


                  Note that @currenvir defaults to document, set inside the LaTeX2e kernel.






                  share|improve this answer












                  Environments are usually called using a begin-end clause. You can patch begin (using etoolbox) to store the value of @currenvir before it is updated:



                  documentclass{article}

                  usepackage{etoolbox}

                  makeatletter
                  % patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
                  patchcmd{begin}{begingroup}{begingrouplet@surroundenvir@currenvir}{}{}

                  newenvironment{FOO}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
                  {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
                  newenvironment{BAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
                  {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
                  newenvironment{FOOBAR}{message{@currenvir-begin: Surrounding environment's name is @surroundenvir}}%
                  {message{@currenvir-end: Surrounding environment's name is @surroundenvir}}
                  makeatother

                  begin{document}

                  begin{FOO}
                  end{FOO}
                  begin{BAR}
                  end{BAR}
                  begin{FOOBAR}
                  end{FOOBAR}
                  begin{FOO}
                  begin{BAR}
                  begin{FOOBAR}
                  end{FOOBAR}
                  end{BAR}
                  end{FOO}

                  end{document}


                  This yields in the .log:




                  FOO-begin: Surrounding environment's name is document
                  FOO-end: Surrounding environment's name is document
                  BAR-begin: Surrounding environment's name is document
                  BAR-end: Surrounding environment's name is document
                  FOOBAR-begin: Surrounding environment's name is document
                  FOOBAR-end: Surrounding environment's name is document
                  FOO-begin: Surrounding environment's name is document
                  BAR-begin: Surrounding environment's name is FOO
                  FOOBAR-begin: Surrounding environment's name is BAR
                  FOOBAR-end: Surrounding environment's name is BAR
                  BAR-end: Surrounding environment's name is FOO
                  FOO-end: Surrounding environment's name is document


                  Note that @currenvir defaults to document, set inside the LaTeX2e kernel.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 24 at 17:46









                  Werner

                  437k649581648




                  437k649581648























                      3














                      Example "test.tex":



                      documentclass{article}
                      usepackage{etoolbox}
                      makeatletter
                      newcommand*@surroundenvir{}
                      newcommand*@surroundenvirtemp{}
                      BeforeBeginEnvironment{FOO}{let@surroundenvirtemp=@currenvir}
                      AtBeginEnvironment{FOO}{let@surroundenvir=@surroundenvirtemp}
                      BeforeBeginEnvironment{BAR}{let@surroundenvirtemp=@currenvir}
                      AtBeginEnvironment{BAR}{let@surroundenvir=@surroundenvirtemp}
                      BeforeBeginEnvironment{FOOBAR}{let@surroundenvirtemp=@currenvir}
                      AtBeginEnvironment{FOOBAR}{let@surroundenvir=@surroundenvirtemp}
                      newenvironment{FOO}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                      {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                      newenvironment{BAR}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                      {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                      newenvironment{FOOBAR}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                      {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                      makeatother

                      begin{document}

                      begin{FOO}
                      end{FOO}

                      begin{BAR}
                      end{BAR}

                      begin{FOOBAR}
                      end{FOOBAR}

                      begin{FOO}
                      begin{BAR}
                      begin{FOOBAR}
                      end{FOOBAR}
                      end{BAR}
                      end{FOO}


                      end{document}


                      enter image description here






                      share|improve this answer




























                        3














                        Example "test.tex":



                        documentclass{article}
                        usepackage{etoolbox}
                        makeatletter
                        newcommand*@surroundenvir{}
                        newcommand*@surroundenvirtemp{}
                        BeforeBeginEnvironment{FOO}{let@surroundenvirtemp=@currenvir}
                        AtBeginEnvironment{FOO}{let@surroundenvir=@surroundenvirtemp}
                        BeforeBeginEnvironment{BAR}{let@surroundenvirtemp=@currenvir}
                        AtBeginEnvironment{BAR}{let@surroundenvir=@surroundenvirtemp}
                        BeforeBeginEnvironment{FOOBAR}{let@surroundenvirtemp=@currenvir}
                        AtBeginEnvironment{FOOBAR}{let@surroundenvir=@surroundenvirtemp}
                        newenvironment{FOO}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                        {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                        newenvironment{BAR}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                        {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                        newenvironment{FOOBAR}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                        {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                        makeatother

                        begin{document}

                        begin{FOO}
                        end{FOO}

                        begin{BAR}
                        end{BAR}

                        begin{FOOBAR}
                        end{FOOBAR}

                        begin{FOO}
                        begin{BAR}
                        begin{FOOBAR}
                        end{FOOBAR}
                        end{BAR}
                        end{FOO}


                        end{document}


                        enter image description here






                        share|improve this answer


























                          3












                          3








                          3






                          Example "test.tex":



                          documentclass{article}
                          usepackage{etoolbox}
                          makeatletter
                          newcommand*@surroundenvir{}
                          newcommand*@surroundenvirtemp{}
                          BeforeBeginEnvironment{FOO}{let@surroundenvirtemp=@currenvir}
                          AtBeginEnvironment{FOO}{let@surroundenvir=@surroundenvirtemp}
                          BeforeBeginEnvironment{BAR}{let@surroundenvirtemp=@currenvir}
                          AtBeginEnvironment{BAR}{let@surroundenvir=@surroundenvirtemp}
                          BeforeBeginEnvironment{FOOBAR}{let@surroundenvirtemp=@currenvir}
                          AtBeginEnvironment{FOOBAR}{let@surroundenvir=@surroundenvirtemp}
                          newenvironment{FOO}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                          {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                          newenvironment{BAR}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                          {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                          newenvironment{FOOBAR}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                          {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                          makeatother

                          begin{document}

                          begin{FOO}
                          end{FOO}

                          begin{BAR}
                          end{BAR}

                          begin{FOOBAR}
                          end{FOOBAR}

                          begin{FOO}
                          begin{BAR}
                          begin{FOOBAR}
                          end{FOOBAR}
                          end{BAR}
                          end{FOO}


                          end{document}


                          enter image description here






                          share|improve this answer














                          Example "test.tex":



                          documentclass{article}
                          usepackage{etoolbox}
                          makeatletter
                          newcommand*@surroundenvir{}
                          newcommand*@surroundenvirtemp{}
                          BeforeBeginEnvironment{FOO}{let@surroundenvirtemp=@currenvir}
                          AtBeginEnvironment{FOO}{let@surroundenvir=@surroundenvirtemp}
                          BeforeBeginEnvironment{BAR}{let@surroundenvirtemp=@currenvir}
                          AtBeginEnvironment{BAR}{let@surroundenvir=@surroundenvirtemp}
                          BeforeBeginEnvironment{FOOBAR}{let@surroundenvirtemp=@currenvir}
                          AtBeginEnvironment{FOOBAR}{let@surroundenvir=@surroundenvirtemp}
                          newenvironment{FOO}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                          {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                          newenvironment{BAR}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                          {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                          newenvironment{FOOBAR}{message{Begin @currenvir: Surrounding env is: @surroundenvir^^J}}%
                          {message{End @currenvir: Surrounding env is: @surroundenvir^^J}}%%
                          makeatother

                          begin{document}

                          begin{FOO}
                          end{FOO}

                          begin{BAR}
                          end{BAR}

                          begin{FOOBAR}
                          end{FOOBAR}

                          begin{FOO}
                          begin{BAR}
                          begin{FOOBAR}
                          end{FOOBAR}
                          end{BAR}
                          end{FOO}


                          end{document}


                          enter image description here







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 24 at 20:34

























                          answered Dec 24 at 18:15









                          Ulrich Diez

                          4,135615




                          4,135615






















                              James Gambolputty is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              James Gambolputty is a new contributor. Be nice, and check out our Code of Conduct.













                              James Gambolputty is a new contributor. Be nice, and check out our Code of Conduct.












                              James Gambolputty is a new contributor. Be nice, and check out our Code of Conduct.
















                              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%2f467211%2flatex-2e-environment-that-knows-the-name-of-surrounding-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

                              數位音樂下載

                              When can things happen in Etherscan, such as the picture below?

                              格利澤436b