tikzcd doesn’t work in custom environment

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a document class test.cls, in which I have defined a custom environment myenvironment using the environ package as follows:



NeedsTeXFormatLaTeX2e
ProvidesClasstest
LoadClassarticle
RequirePackagechangepage
RequirePackageenviron
NewEnvironmyenvironment
beginadjustwidth10pt10pt
BODY
endadjustwidth



Now if I try to use tikz-cd inside myenvironment, it fails, e.g. this gives an error:



beginmyenvironment
[begintikzcd
1 ar[r] ar[d] & 2 ar[d] \
3 ar[r] & 4 \
endtikzcd]
endmyenvironment


On the other hand, if I simply write it like this—that is, I substitute what the environment would do—, everything is fine:



beginadjustwidth10pt10pt
[begintikzcd
1 ar[r] ar[d] & 2 ar[d] \
3 ar[r] & 4 \
endtikzcd]
endadjustwidth


What could be the problem?










share|improve this question

















  • 2




    Why do you have to collect the contents of myenvironment? Isn't LaTeX's newenvironment is enough here?
    – Phelype Oleinik
    58 mins ago











  • Indeed it is enough, I guess I just got too used to the environ package to notice. I’ve tried it now and it solves the problem. I still wonder why it doesn’t work with environ though.
    – Hermetically Sealed Halibut
    54 mins ago











  • I posted an answer explaining why it doesn't work :)
    – Phelype Oleinik
    35 mins ago














up vote
1
down vote

favorite












I have a document class test.cls, in which I have defined a custom environment myenvironment using the environ package as follows:



NeedsTeXFormatLaTeX2e
ProvidesClasstest
LoadClassarticle
RequirePackagechangepage
RequirePackageenviron
NewEnvironmyenvironment
beginadjustwidth10pt10pt
BODY
endadjustwidth



Now if I try to use tikz-cd inside myenvironment, it fails, e.g. this gives an error:



beginmyenvironment
[begintikzcd
1 ar[r] ar[d] & 2 ar[d] \
3 ar[r] & 4 \
endtikzcd]
endmyenvironment


On the other hand, if I simply write it like this—that is, I substitute what the environment would do—, everything is fine:



beginadjustwidth10pt10pt
[begintikzcd
1 ar[r] ar[d] & 2 ar[d] \
3 ar[r] & 4 \
endtikzcd]
endadjustwidth


What could be the problem?










share|improve this question

















  • 2




    Why do you have to collect the contents of myenvironment? Isn't LaTeX's newenvironment is enough here?
    – Phelype Oleinik
    58 mins ago











  • Indeed it is enough, I guess I just got too used to the environ package to notice. I’ve tried it now and it solves the problem. I still wonder why it doesn’t work with environ though.
    – Hermetically Sealed Halibut
    54 mins ago











  • I posted an answer explaining why it doesn't work :)
    – Phelype Oleinik
    35 mins ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a document class test.cls, in which I have defined a custom environment myenvironment using the environ package as follows:



NeedsTeXFormatLaTeX2e
ProvidesClasstest
LoadClassarticle
RequirePackagechangepage
RequirePackageenviron
NewEnvironmyenvironment
beginadjustwidth10pt10pt
BODY
endadjustwidth



Now if I try to use tikz-cd inside myenvironment, it fails, e.g. this gives an error:



beginmyenvironment
[begintikzcd
1 ar[r] ar[d] & 2 ar[d] \
3 ar[r] & 4 \
endtikzcd]
endmyenvironment


On the other hand, if I simply write it like this—that is, I substitute what the environment would do—, everything is fine:



beginadjustwidth10pt10pt
[begintikzcd
1 ar[r] ar[d] & 2 ar[d] \
3 ar[r] & 4 \
endtikzcd]
endadjustwidth


What could be the problem?










share|improve this question













I have a document class test.cls, in which I have defined a custom environment myenvironment using the environ package as follows:



NeedsTeXFormatLaTeX2e
ProvidesClasstest
LoadClassarticle
RequirePackagechangepage
RequirePackageenviron
NewEnvironmyenvironment
beginadjustwidth10pt10pt
BODY
endadjustwidth



Now if I try to use tikz-cd inside myenvironment, it fails, e.g. this gives an error:



beginmyenvironment
[begintikzcd
1 ar[r] ar[d] & 2 ar[d] \
3 ar[r] & 4 \
endtikzcd]
endmyenvironment


On the other hand, if I simply write it like this—that is, I substitute what the environment would do—, everything is fine:



beginadjustwidth10pt10pt
[begintikzcd
1 ar[r] ar[d] & 2 ar[d] \
3 ar[r] & 4 \
endtikzcd]
endadjustwidth


What could be the problem?







environments tikz-cd






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









Hermetically Sealed Halibut

1086




1086







  • 2




    Why do you have to collect the contents of myenvironment? Isn't LaTeX's newenvironment is enough here?
    – Phelype Oleinik
    58 mins ago











  • Indeed it is enough, I guess I just got too used to the environ package to notice. I’ve tried it now and it solves the problem. I still wonder why it doesn’t work with environ though.
    – Hermetically Sealed Halibut
    54 mins ago











  • I posted an answer explaining why it doesn't work :)
    – Phelype Oleinik
    35 mins ago












  • 2




    Why do you have to collect the contents of myenvironment? Isn't LaTeX's newenvironment is enough here?
    – Phelype Oleinik
    58 mins ago











  • Indeed it is enough, I guess I just got too used to the environ package to notice. I’ve tried it now and it solves the problem. I still wonder why it doesn’t work with environ though.
    – Hermetically Sealed Halibut
    54 mins ago











  • I posted an answer explaining why it doesn't work :)
    – Phelype Oleinik
    35 mins ago







2




2




Why do you have to collect the contents of myenvironment? Isn't LaTeX's newenvironment is enough here?
– Phelype Oleinik
58 mins ago





Why do you have to collect the contents of myenvironment? Isn't LaTeX's newenvironment is enough here?
– Phelype Oleinik
58 mins ago













Indeed it is enough, I guess I just got too used to the environ package to notice. I’ve tried it now and it solves the problem. I still wonder why it doesn’t work with environ though.
– Hermetically Sealed Halibut
54 mins ago





Indeed it is enough, I guess I just got too used to the environ package to notice. I’ve tried it now and it solves the problem. I still wonder why it doesn’t work with environ though.
– Hermetically Sealed Halibut
54 mins ago













I posted an answer explaining why it doesn't work :)
– Phelype Oleinik
35 mins ago




I posted an answer explaining why it doesn't work :)
– Phelype Oleinik
35 mins ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










You have to be very careful when collecting an environment with environ, as this usually brings problems. When environ collects its the environment it "freezes" the tokens of the environment. That is, everything will mean what it was when the outer environment was called. This will break every environment that somewhere redefines the category codes of any character (verbatim environments, for example), or that redefine some character.



In this particular case, the meaning of the & changes when you enter tikzcd. See:



documentclassarticle
usepackageamsmath
usepackagetikz-cd
begindocument

When textttenviron collects the environment:

ttfamilymeaning&

bigskip

What texttttikzcd expects texttt& to be:

begintikzcd
vbox to textwidthttfamilymeaning&
endtikzcd
enddocument



enter image description here




So basically, inside tikzcd, & means one thing, but when environ reads it, it freezes the alignment tab character & meaning, so it won't work.



So use environ only when you are sure that you need it and you are sure that it won't break anything.



In your case you don't need environ at all because you aren't applying any operation to the collected contents, so this:



newenvironmentmyenvironment
%
beginadjustwidth10pt10pt%
%
%
endadjustwidth



will suffice.






share|improve this answer



























    up vote
    2
    down vote













    It doesn't work because myenvironment absorbs the body of the environment as a macro argument; in these cases you have to pass ampersand replacement=& to tikzcd and use & instead of &, so



    beginmyenvironment
    [begintikzcd[ampersand replacement=&
    1 ar[r] ar[d] & 2 ar[d] \
    3 ar[r] & 4 \
    endtikzcd]
    endmyenvironment


    However, there's no need for environ here:



    newenvironmentmyenvironment
    beginadjustwidth10pt10pt
    endadjustwidth


    will do as well. And better, actually.



    The cargo-cult version would be



    newenvironmentmyenvironment
    adjustwidth10pt10pt
    endadjustwidth





    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: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f456291%2ftikzcd-doesn-t-work-in-custom-environment%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      You have to be very careful when collecting an environment with environ, as this usually brings problems. When environ collects its the environment it "freezes" the tokens of the environment. That is, everything will mean what it was when the outer environment was called. This will break every environment that somewhere redefines the category codes of any character (verbatim environments, for example), or that redefine some character.



      In this particular case, the meaning of the & changes when you enter tikzcd. See:



      documentclassarticle
      usepackageamsmath
      usepackagetikz-cd
      begindocument

      When textttenviron collects the environment:

      ttfamilymeaning&

      bigskip

      What texttttikzcd expects texttt& to be:

      begintikzcd
      vbox to textwidthttfamilymeaning&
      endtikzcd
      enddocument



      enter image description here




      So basically, inside tikzcd, & means one thing, but when environ reads it, it freezes the alignment tab character & meaning, so it won't work.



      So use environ only when you are sure that you need it and you are sure that it won't break anything.



      In your case you don't need environ at all because you aren't applying any operation to the collected contents, so this:



      newenvironmentmyenvironment
      %
      beginadjustwidth10pt10pt%
      %
      %
      endadjustwidth



      will suffice.






      share|improve this answer
























        up vote
        2
        down vote



        accepted










        You have to be very careful when collecting an environment with environ, as this usually brings problems. When environ collects its the environment it "freezes" the tokens of the environment. That is, everything will mean what it was when the outer environment was called. This will break every environment that somewhere redefines the category codes of any character (verbatim environments, for example), or that redefine some character.



        In this particular case, the meaning of the & changes when you enter tikzcd. See:



        documentclassarticle
        usepackageamsmath
        usepackagetikz-cd
        begindocument

        When textttenviron collects the environment:

        ttfamilymeaning&

        bigskip

        What texttttikzcd expects texttt& to be:

        begintikzcd
        vbox to textwidthttfamilymeaning&
        endtikzcd
        enddocument



        enter image description here




        So basically, inside tikzcd, & means one thing, but when environ reads it, it freezes the alignment tab character & meaning, so it won't work.



        So use environ only when you are sure that you need it and you are sure that it won't break anything.



        In your case you don't need environ at all because you aren't applying any operation to the collected contents, so this:



        newenvironmentmyenvironment
        %
        beginadjustwidth10pt10pt%
        %
        %
        endadjustwidth



        will suffice.






        share|improve this answer






















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You have to be very careful when collecting an environment with environ, as this usually brings problems. When environ collects its the environment it "freezes" the tokens of the environment. That is, everything will mean what it was when the outer environment was called. This will break every environment that somewhere redefines the category codes of any character (verbatim environments, for example), or that redefine some character.



          In this particular case, the meaning of the & changes when you enter tikzcd. See:



          documentclassarticle
          usepackageamsmath
          usepackagetikz-cd
          begindocument

          When textttenviron collects the environment:

          ttfamilymeaning&

          bigskip

          What texttttikzcd expects texttt& to be:

          begintikzcd
          vbox to textwidthttfamilymeaning&
          endtikzcd
          enddocument



          enter image description here




          So basically, inside tikzcd, & means one thing, but when environ reads it, it freezes the alignment tab character & meaning, so it won't work.



          So use environ only when you are sure that you need it and you are sure that it won't break anything.



          In your case you don't need environ at all because you aren't applying any operation to the collected contents, so this:



          newenvironmentmyenvironment
          %
          beginadjustwidth10pt10pt%
          %
          %
          endadjustwidth



          will suffice.






          share|improve this answer












          You have to be very careful when collecting an environment with environ, as this usually brings problems. When environ collects its the environment it "freezes" the tokens of the environment. That is, everything will mean what it was when the outer environment was called. This will break every environment that somewhere redefines the category codes of any character (verbatim environments, for example), or that redefine some character.



          In this particular case, the meaning of the & changes when you enter tikzcd. See:



          documentclassarticle
          usepackageamsmath
          usepackagetikz-cd
          begindocument

          When textttenviron collects the environment:

          ttfamilymeaning&

          bigskip

          What texttttikzcd expects texttt& to be:

          begintikzcd
          vbox to textwidthttfamilymeaning&
          endtikzcd
          enddocument



          enter image description here




          So basically, inside tikzcd, & means one thing, but when environ reads it, it freezes the alignment tab character & meaning, so it won't work.



          So use environ only when you are sure that you need it and you are sure that it won't break anything.



          In your case you don't need environ at all because you aren't applying any operation to the collected contents, so this:



          newenvironmentmyenvironment
          %
          beginadjustwidth10pt10pt%
          %
          %
          endadjustwidth



          will suffice.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 35 mins ago









          Phelype Oleinik

          18.9k54173




          18.9k54173




















              up vote
              2
              down vote













              It doesn't work because myenvironment absorbs the body of the environment as a macro argument; in these cases you have to pass ampersand replacement=& to tikzcd and use & instead of &, so



              beginmyenvironment
              [begintikzcd[ampersand replacement=&
              1 ar[r] ar[d] & 2 ar[d] \
              3 ar[r] & 4 \
              endtikzcd]
              endmyenvironment


              However, there's no need for environ here:



              newenvironmentmyenvironment
              beginadjustwidth10pt10pt
              endadjustwidth


              will do as well. And better, actually.



              The cargo-cult version would be



              newenvironmentmyenvironment
              adjustwidth10pt10pt
              endadjustwidth





              share|improve this answer
























                up vote
                2
                down vote













                It doesn't work because myenvironment absorbs the body of the environment as a macro argument; in these cases you have to pass ampersand replacement=& to tikzcd and use & instead of &, so



                beginmyenvironment
                [begintikzcd[ampersand replacement=&
                1 ar[r] ar[d] & 2 ar[d] \
                3 ar[r] & 4 \
                endtikzcd]
                endmyenvironment


                However, there's no need for environ here:



                newenvironmentmyenvironment
                beginadjustwidth10pt10pt
                endadjustwidth


                will do as well. And better, actually.



                The cargo-cult version would be



                newenvironmentmyenvironment
                adjustwidth10pt10pt
                endadjustwidth





                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  It doesn't work because myenvironment absorbs the body of the environment as a macro argument; in these cases you have to pass ampersand replacement=& to tikzcd and use & instead of &, so



                  beginmyenvironment
                  [begintikzcd[ampersand replacement=&
                  1 ar[r] ar[d] & 2 ar[d] \
                  3 ar[r] & 4 \
                  endtikzcd]
                  endmyenvironment


                  However, there's no need for environ here:



                  newenvironmentmyenvironment
                  beginadjustwidth10pt10pt
                  endadjustwidth


                  will do as well. And better, actually.



                  The cargo-cult version would be



                  newenvironmentmyenvironment
                  adjustwidth10pt10pt
                  endadjustwidth





                  share|improve this answer












                  It doesn't work because myenvironment absorbs the body of the environment as a macro argument; in these cases you have to pass ampersand replacement=& to tikzcd and use & instead of &, so



                  beginmyenvironment
                  [begintikzcd[ampersand replacement=&
                  1 ar[r] ar[d] & 2 ar[d] \
                  3 ar[r] & 4 \
                  endtikzcd]
                  endmyenvironment


                  However, there's no need for environ here:



                  newenvironmentmyenvironment
                  beginadjustwidth10pt10pt
                  endadjustwidth


                  will do as well. And better, actually.



                  The cargo-cult version would be



                  newenvironmentmyenvironment
                  adjustwidth10pt10pt
                  endadjustwidth






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 36 mins ago









                  egreg

                  691k8518383087




                  691k8518383087



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f456291%2ftikzcd-doesn-t-work-in-custom-environment%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery