Detecting if inside a tikzpicture

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











up vote
4
down vote

favorite












I would like to define the DrawLine macro below so that it can be invoked from within a tikzpicture environment or from outside:



newcommand*DrawLine[1]%
IfInTikzPictikzpicture[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
IfInTikzPicendtikzpicture%



This works fine when invoked from within a tikzpicture, but not when it is invoked from outside (line is commented in the MWE). Invoking it from outside yields the error:




Missing endgroup inserted.




enter image description here



The MWE below uses the first reference below. The other two references also fail on the commented out test case.



References:



  • Detecting if inside a tikzpicture node. Even though this solution is for detecting within a node, it appears to work to detect if you are within a tikzpicture (as the commented out code in the MWE shows).


  • Is there a (simple) way to find out if a command is executed in a tikzpicture environment?.


  • How can I check if the current code is inside a certain environment?.


Code:



documentclassarticle
usepackagetikz

makeatletter
newcommandIfInTikzPic[2]% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%

makeatother

newcommand*DrawLine[1]%
IfInTikzPictikzpicture[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
IfInTikzPicendtikzpicture%


begindocument
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%

%textbfIfInTikzPicpar
%IfInTikzPicinsideoutside
%
%begintikzpicture
% IfInTikzPicdraw [red, ultra thick]draw [blue, ultra thick] (0,0) -- (1,0);
%endtikzpicture
%
%medskip% --------------------------
textbfDrawLine: Actual Outputpar

%DrawLineblue% <---- How do I get this case to work?

begintikzpicture
DrawLineorange
endtikzpicture

medskip% --------------------------
textbfDrawLine: Desired Outputpar

begintikzpicture
DrawLineblue
endtikzpicture

begintikzpicture
DrawLineorange
endtikzpicture
enddocument









share|improve this question



















  • 1




    tex.stackexchange.com/questions/18652/… perhaps
    – Torbjørn T.
    1 hour ago










  • Or tex.stackexchange.com/questions/412456/…
    – marmot
    1 hour ago










  • @TorbjørnT.: That works for all but the commented out case below (yields a different error message though: Undefined control sequence).
    – Peter Grill
    1 hour ago











  • @marmot: Yep, that also works for all but the commented out case below and produces the same error message.
    – Peter Grill
    1 hour ago














up vote
4
down vote

favorite












I would like to define the DrawLine macro below so that it can be invoked from within a tikzpicture environment or from outside:



newcommand*DrawLine[1]%
IfInTikzPictikzpicture[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
IfInTikzPicendtikzpicture%



This works fine when invoked from within a tikzpicture, but not when it is invoked from outside (line is commented in the MWE). Invoking it from outside yields the error:




Missing endgroup inserted.




enter image description here



The MWE below uses the first reference below. The other two references also fail on the commented out test case.



References:



  • Detecting if inside a tikzpicture node. Even though this solution is for detecting within a node, it appears to work to detect if you are within a tikzpicture (as the commented out code in the MWE shows).


  • Is there a (simple) way to find out if a command is executed in a tikzpicture environment?.


  • How can I check if the current code is inside a certain environment?.


Code:



documentclassarticle
usepackagetikz

makeatletter
newcommandIfInTikzPic[2]% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%

makeatother

newcommand*DrawLine[1]%
IfInTikzPictikzpicture[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
IfInTikzPicendtikzpicture%


begindocument
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%

%textbfIfInTikzPicpar
%IfInTikzPicinsideoutside
%
%begintikzpicture
% IfInTikzPicdraw [red, ultra thick]draw [blue, ultra thick] (0,0) -- (1,0);
%endtikzpicture
%
%medskip% --------------------------
textbfDrawLine: Actual Outputpar

%DrawLineblue% <---- How do I get this case to work?

begintikzpicture
DrawLineorange
endtikzpicture

medskip% --------------------------
textbfDrawLine: Desired Outputpar

begintikzpicture
DrawLineblue
endtikzpicture

begintikzpicture
DrawLineorange
endtikzpicture
enddocument









share|improve this question



















  • 1




    tex.stackexchange.com/questions/18652/… perhaps
    – Torbjørn T.
    1 hour ago










  • Or tex.stackexchange.com/questions/412456/…
    – marmot
    1 hour ago










  • @TorbjørnT.: That works for all but the commented out case below (yields a different error message though: Undefined control sequence).
    – Peter Grill
    1 hour ago











  • @marmot: Yep, that also works for all but the commented out case below and produces the same error message.
    – Peter Grill
    1 hour ago












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I would like to define the DrawLine macro below so that it can be invoked from within a tikzpicture environment or from outside:



newcommand*DrawLine[1]%
IfInTikzPictikzpicture[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
IfInTikzPicendtikzpicture%



This works fine when invoked from within a tikzpicture, but not when it is invoked from outside (line is commented in the MWE). Invoking it from outside yields the error:




Missing endgroup inserted.




enter image description here



The MWE below uses the first reference below. The other two references also fail on the commented out test case.



References:



  • Detecting if inside a tikzpicture node. Even though this solution is for detecting within a node, it appears to work to detect if you are within a tikzpicture (as the commented out code in the MWE shows).


  • Is there a (simple) way to find out if a command is executed in a tikzpicture environment?.


  • How can I check if the current code is inside a certain environment?.


Code:



documentclassarticle
usepackagetikz

makeatletter
newcommandIfInTikzPic[2]% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%

makeatother

newcommand*DrawLine[1]%
IfInTikzPictikzpicture[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
IfInTikzPicendtikzpicture%


begindocument
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%

%textbfIfInTikzPicpar
%IfInTikzPicinsideoutside
%
%begintikzpicture
% IfInTikzPicdraw [red, ultra thick]draw [blue, ultra thick] (0,0) -- (1,0);
%endtikzpicture
%
%medskip% --------------------------
textbfDrawLine: Actual Outputpar

%DrawLineblue% <---- How do I get this case to work?

begintikzpicture
DrawLineorange
endtikzpicture

medskip% --------------------------
textbfDrawLine: Desired Outputpar

begintikzpicture
DrawLineblue
endtikzpicture

begintikzpicture
DrawLineorange
endtikzpicture
enddocument









share|improve this question















I would like to define the DrawLine macro below so that it can be invoked from within a tikzpicture environment or from outside:



newcommand*DrawLine[1]%
IfInTikzPictikzpicture[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
IfInTikzPicendtikzpicture%



This works fine when invoked from within a tikzpicture, but not when it is invoked from outside (line is commented in the MWE). Invoking it from outside yields the error:




Missing endgroup inserted.




enter image description here



The MWE below uses the first reference below. The other two references also fail on the commented out test case.



References:



  • Detecting if inside a tikzpicture node. Even though this solution is for detecting within a node, it appears to work to detect if you are within a tikzpicture (as the commented out code in the MWE shows).


  • Is there a (simple) way to find out if a command is executed in a tikzpicture environment?.


  • How can I check if the current code is inside a certain environment?.


Code:



documentclassarticle
usepackagetikz

makeatletter
newcommandIfInTikzPic[2]% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%

makeatother

newcommand*DrawLine[1]%
IfInTikzPictikzpicture[remember picture]
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
IfInTikzPicendtikzpicture%


begindocument
%%% The commented out code here is to show that IfInTikzPic works as desired
%%% (in a tikzpicture, outside of a node).
%%%

%textbfIfInTikzPicpar
%IfInTikzPicinsideoutside
%
%begintikzpicture
% IfInTikzPicdraw [red, ultra thick]draw [blue, ultra thick] (0,0) -- (1,0);
%endtikzpicture
%
%medskip% --------------------------
textbfDrawLine: Actual Outputpar

%DrawLineblue% <---- How do I get this case to work?

begintikzpicture
DrawLineorange
endtikzpicture

medskip% --------------------------
textbfDrawLine: Desired Outputpar

begintikzpicture
DrawLineblue
endtikzpicture

begintikzpicture
DrawLineorange
endtikzpicture
enddocument






tikz-pgf grouping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 1 hour ago









Peter Grill

162k24432737




162k24432737







  • 1




    tex.stackexchange.com/questions/18652/… perhaps
    – Torbjørn T.
    1 hour ago










  • Or tex.stackexchange.com/questions/412456/…
    – marmot
    1 hour ago










  • @TorbjørnT.: That works for all but the commented out case below (yields a different error message though: Undefined control sequence).
    – Peter Grill
    1 hour ago











  • @marmot: Yep, that also works for all but the commented out case below and produces the same error message.
    – Peter Grill
    1 hour ago












  • 1




    tex.stackexchange.com/questions/18652/… perhaps
    – Torbjørn T.
    1 hour ago










  • Or tex.stackexchange.com/questions/412456/…
    – marmot
    1 hour ago










  • @TorbjørnT.: That works for all but the commented out case below (yields a different error message though: Undefined control sequence).
    – Peter Grill
    1 hour ago











  • @marmot: Yep, that also works for all but the commented out case below and produces the same error message.
    – Peter Grill
    1 hour ago







1




1




tex.stackexchange.com/questions/18652/… perhaps
– Torbjørn T.
1 hour ago




tex.stackexchange.com/questions/18652/… perhaps
– Torbjørn T.
1 hour ago












Or tex.stackexchange.com/questions/412456/…
– marmot
1 hour ago




Or tex.stackexchange.com/questions/412456/…
– marmot
1 hour ago












@TorbjørnT.: That works for all but the commented out case below (yields a different error message though: Undefined control sequence).
– Peter Grill
1 hour ago





@TorbjørnT.: That works for all but the commented out case below (yields a different error message though: Undefined control sequence).
– Peter Grill
1 hour ago













@marmot: Yep, that also works for all but the commented out case below and produces the same error message.
– Peter Grill
1 hour ago




@marmot: Yep, that also works for all but the commented out case below and produces the same error message.
– Peter Grill
1 hour ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote













The following example defines IfInTikzPic to check the current environment against tikzpicture and define a tikzstart...tikzend pair accordingly:



enter image description here



documentclassarticle

usepackagetikz

makeatletter
def@tikzenvironmenttikzpicture

newcommandIfInTikzPic%
edeftikzstart%
ifx@currenvir@tikzenvironmentelse
noexpandtikzpicture[remember picture]%
fi
%
edeftikzend%
ifx@currenvir@tikzenvironmentelse
noexpandendtikzpicture%
fi
%


newcommand*DrawLine[1]%
IfInTikzPic
tikzstart
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
tikzend%

makeatother

begindocument

textbfDrawLine: Actual Outputpar

DrawLineblue

begintikzpicture
DrawLineorange
endtikzpicture

medskip% --------------------------

textbfDrawLine: Desired Outputpar

begintikzpicture
DrawLineblue
endtikzpicture

begintikzpicture
DrawLineorange
endtikzpicture

enddocument


You could also group the tikzpicture if needed:



newcommandIfInTikzPic%
edeftikzstart%
ifx@currenvir@tikzenvironmentelse
noexpandbegintikzpicture[remember picture]%
fi
%
edeftikzend%
ifx@currenvir@tikzenvironmentelse
noexpandendtikzpicture%
fi
%






share|improve this answer





























    up vote
    2
    down vote













    documentclassarticle
    usepackagetikz

    makeatletter
    newcommandIfInTikzPic% https://tex.stackexchange.com/a/121309/4301
    ifxpgfpictureid@undefined
    expandafter@firstoftwo
    else
    expandafter@secondoftwo
    fi

    makeatother

    newcommand*DrawLine[1]%
    IfInTikzPic
    begintikzpicture[remember picture]
    draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
    endtikzpicture%
    draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;%


    begindocument
    %%% The commented out code here is to show that IfInTikzPic works as desired
    %%% (in a tikzpicture, outside of a node).
    %%%

    textbfIfInTikzPicpar
    IfInTikzPicinsideoutside

    begintikzpicture
    IfInTikzPicdraw [red, ultra thick]draw [blue, ultra thick] (0,0) -- (1,0);
    endtikzpicture

    medskip% --------------------------
    textbfDrawLine: Actual Outputpar

    DrawLineblue% <---- How do I get this case to work?

    begintikzpicture
    DrawLineorange
    endtikzpicture

    medskip% --------------------------
    textbfDrawLine: Desired Outputpar

    begintikzpicture
    DrawLineblue
    endtikzpicture

    begintikzpicture
    DrawLineorange
    endtikzpicture
    enddocument





    share|improve this answer




















    • I thought it might have something to dot with the dreaded expansion issues!
      – Peter Grill
      1 hour ago






    • 2




      @PeterGrill I think the main error was IfInTikzPicendtikzpicture% as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
      – David Carlisle
      1 hour ago










    • Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
      – Peter Grill
      44 mins ago











    • @PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
      – David Carlisle
      4 mins ago










    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%2f458849%2fdetecting-if-inside-a-tikzpicture%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
    3
    down vote













    The following example defines IfInTikzPic to check the current environment against tikzpicture and define a tikzstart...tikzend pair accordingly:



    enter image description here



    documentclassarticle

    usepackagetikz

    makeatletter
    def@tikzenvironmenttikzpicture

    newcommandIfInTikzPic%
    edeftikzstart%
    ifx@currenvir@tikzenvironmentelse
    noexpandtikzpicture[remember picture]%
    fi
    %
    edeftikzend%
    ifx@currenvir@tikzenvironmentelse
    noexpandendtikzpicture%
    fi
    %


    newcommand*DrawLine[1]%
    IfInTikzPic
    tikzstart
    draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
    tikzend%

    makeatother

    begindocument

    textbfDrawLine: Actual Outputpar

    DrawLineblue

    begintikzpicture
    DrawLineorange
    endtikzpicture

    medskip% --------------------------

    textbfDrawLine: Desired Outputpar

    begintikzpicture
    DrawLineblue
    endtikzpicture

    begintikzpicture
    DrawLineorange
    endtikzpicture

    enddocument


    You could also group the tikzpicture if needed:



    newcommandIfInTikzPic%
    edeftikzstart%
    ifx@currenvir@tikzenvironmentelse
    noexpandbegintikzpicture[remember picture]%
    fi
    %
    edeftikzend%
    ifx@currenvir@tikzenvironmentelse
    noexpandendtikzpicture%
    fi
    %






    share|improve this answer


























      up vote
      3
      down vote













      The following example defines IfInTikzPic to check the current environment against tikzpicture and define a tikzstart...tikzend pair accordingly:



      enter image description here



      documentclassarticle

      usepackagetikz

      makeatletter
      def@tikzenvironmenttikzpicture

      newcommandIfInTikzPic%
      edeftikzstart%
      ifx@currenvir@tikzenvironmentelse
      noexpandtikzpicture[remember picture]%
      fi
      %
      edeftikzend%
      ifx@currenvir@tikzenvironmentelse
      noexpandendtikzpicture%
      fi
      %


      newcommand*DrawLine[1]%
      IfInTikzPic
      tikzstart
      draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
      tikzend%

      makeatother

      begindocument

      textbfDrawLine: Actual Outputpar

      DrawLineblue

      begintikzpicture
      DrawLineorange
      endtikzpicture

      medskip% --------------------------

      textbfDrawLine: Desired Outputpar

      begintikzpicture
      DrawLineblue
      endtikzpicture

      begintikzpicture
      DrawLineorange
      endtikzpicture

      enddocument


      You could also group the tikzpicture if needed:



      newcommandIfInTikzPic%
      edeftikzstart%
      ifx@currenvir@tikzenvironmentelse
      noexpandbegintikzpicture[remember picture]%
      fi
      %
      edeftikzend%
      ifx@currenvir@tikzenvironmentelse
      noexpandendtikzpicture%
      fi
      %






      share|improve this answer
























        up vote
        3
        down vote










        up vote
        3
        down vote









        The following example defines IfInTikzPic to check the current environment against tikzpicture and define a tikzstart...tikzend pair accordingly:



        enter image description here



        documentclassarticle

        usepackagetikz

        makeatletter
        def@tikzenvironmenttikzpicture

        newcommandIfInTikzPic%
        edeftikzstart%
        ifx@currenvir@tikzenvironmentelse
        noexpandtikzpicture[remember picture]%
        fi
        %
        edeftikzend%
        ifx@currenvir@tikzenvironmentelse
        noexpandendtikzpicture%
        fi
        %


        newcommand*DrawLine[1]%
        IfInTikzPic
        tikzstart
        draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
        tikzend%

        makeatother

        begindocument

        textbfDrawLine: Actual Outputpar

        DrawLineblue

        begintikzpicture
        DrawLineorange
        endtikzpicture

        medskip% --------------------------

        textbfDrawLine: Desired Outputpar

        begintikzpicture
        DrawLineblue
        endtikzpicture

        begintikzpicture
        DrawLineorange
        endtikzpicture

        enddocument


        You could also group the tikzpicture if needed:



        newcommandIfInTikzPic%
        edeftikzstart%
        ifx@currenvir@tikzenvironmentelse
        noexpandbegintikzpicture[remember picture]%
        fi
        %
        edeftikzend%
        ifx@currenvir@tikzenvironmentelse
        noexpandendtikzpicture%
        fi
        %






        share|improve this answer














        The following example defines IfInTikzPic to check the current environment against tikzpicture and define a tikzstart...tikzend pair accordingly:



        enter image description here



        documentclassarticle

        usepackagetikz

        makeatletter
        def@tikzenvironmenttikzpicture

        newcommandIfInTikzPic%
        edeftikzstart%
        ifx@currenvir@tikzenvironmentelse
        noexpandtikzpicture[remember picture]%
        fi
        %
        edeftikzend%
        ifx@currenvir@tikzenvironmentelse
        noexpandendtikzpicture%
        fi
        %


        newcommand*DrawLine[1]%
        IfInTikzPic
        tikzstart
        draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
        tikzend%

        makeatother

        begindocument

        textbfDrawLine: Actual Outputpar

        DrawLineblue

        begintikzpicture
        DrawLineorange
        endtikzpicture

        medskip% --------------------------

        textbfDrawLine: Desired Outputpar

        begintikzpicture
        DrawLineblue
        endtikzpicture

        begintikzpicture
        DrawLineorange
        endtikzpicture

        enddocument


        You could also group the tikzpicture if needed:



        newcommandIfInTikzPic%
        edeftikzstart%
        ifx@currenvir@tikzenvironmentelse
        noexpandbegintikzpicture[remember picture]%
        fi
        %
        edeftikzend%
        ifx@currenvir@tikzenvironmentelse
        noexpandendtikzpicture%
        fi
        %







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 55 mins ago

























        answered 1 hour ago









        Werner

        428k589391616




        428k589391616




















            up vote
            2
            down vote













            documentclassarticle
            usepackagetikz

            makeatletter
            newcommandIfInTikzPic% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi

            makeatother

            newcommand*DrawLine[1]%
            IfInTikzPic
            begintikzpicture[remember picture]
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
            endtikzpicture%
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;%


            begindocument
            %%% The commented out code here is to show that IfInTikzPic works as desired
            %%% (in a tikzpicture, outside of a node).
            %%%

            textbfIfInTikzPicpar
            IfInTikzPicinsideoutside

            begintikzpicture
            IfInTikzPicdraw [red, ultra thick]draw [blue, ultra thick] (0,0) -- (1,0);
            endtikzpicture

            medskip% --------------------------
            textbfDrawLine: Actual Outputpar

            DrawLineblue% <---- How do I get this case to work?

            begintikzpicture
            DrawLineorange
            endtikzpicture

            medskip% --------------------------
            textbfDrawLine: Desired Outputpar

            begintikzpicture
            DrawLineblue
            endtikzpicture

            begintikzpicture
            DrawLineorange
            endtikzpicture
            enddocument





            share|improve this answer




















            • I thought it might have something to dot with the dreaded expansion issues!
              – Peter Grill
              1 hour ago






            • 2




              @PeterGrill I think the main error was IfInTikzPicendtikzpicture% as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
              – David Carlisle
              1 hour ago










            • Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
              – Peter Grill
              44 mins ago











            • @PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
              – David Carlisle
              4 mins ago














            up vote
            2
            down vote













            documentclassarticle
            usepackagetikz

            makeatletter
            newcommandIfInTikzPic% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi

            makeatother

            newcommand*DrawLine[1]%
            IfInTikzPic
            begintikzpicture[remember picture]
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
            endtikzpicture%
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;%


            begindocument
            %%% The commented out code here is to show that IfInTikzPic works as desired
            %%% (in a tikzpicture, outside of a node).
            %%%

            textbfIfInTikzPicpar
            IfInTikzPicinsideoutside

            begintikzpicture
            IfInTikzPicdraw [red, ultra thick]draw [blue, ultra thick] (0,0) -- (1,0);
            endtikzpicture

            medskip% --------------------------
            textbfDrawLine: Actual Outputpar

            DrawLineblue% <---- How do I get this case to work?

            begintikzpicture
            DrawLineorange
            endtikzpicture

            medskip% --------------------------
            textbfDrawLine: Desired Outputpar

            begintikzpicture
            DrawLineblue
            endtikzpicture

            begintikzpicture
            DrawLineorange
            endtikzpicture
            enddocument





            share|improve this answer




















            • I thought it might have something to dot with the dreaded expansion issues!
              – Peter Grill
              1 hour ago






            • 2




              @PeterGrill I think the main error was IfInTikzPicendtikzpicture% as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
              – David Carlisle
              1 hour ago










            • Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
              – Peter Grill
              44 mins ago











            • @PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
              – David Carlisle
              4 mins ago












            up vote
            2
            down vote










            up vote
            2
            down vote









            documentclassarticle
            usepackagetikz

            makeatletter
            newcommandIfInTikzPic% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi

            makeatother

            newcommand*DrawLine[1]%
            IfInTikzPic
            begintikzpicture[remember picture]
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
            endtikzpicture%
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;%


            begindocument
            %%% The commented out code here is to show that IfInTikzPic works as desired
            %%% (in a tikzpicture, outside of a node).
            %%%

            textbfIfInTikzPicpar
            IfInTikzPicinsideoutside

            begintikzpicture
            IfInTikzPicdraw [red, ultra thick]draw [blue, ultra thick] (0,0) -- (1,0);
            endtikzpicture

            medskip% --------------------------
            textbfDrawLine: Actual Outputpar

            DrawLineblue% <---- How do I get this case to work?

            begintikzpicture
            DrawLineorange
            endtikzpicture

            medskip% --------------------------
            textbfDrawLine: Desired Outputpar

            begintikzpicture
            DrawLineblue
            endtikzpicture

            begintikzpicture
            DrawLineorange
            endtikzpicture
            enddocument





            share|improve this answer












            documentclassarticle
            usepackagetikz

            makeatletter
            newcommandIfInTikzPic% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi

            makeatother

            newcommand*DrawLine[1]%
            IfInTikzPic
            begintikzpicture[remember picture]
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;
            endtikzpicture%
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] output of DrawLine;%


            begindocument
            %%% The commented out code here is to show that IfInTikzPic works as desired
            %%% (in a tikzpicture, outside of a node).
            %%%

            textbfIfInTikzPicpar
            IfInTikzPicinsideoutside

            begintikzpicture
            IfInTikzPicdraw [red, ultra thick]draw [blue, ultra thick] (0,0) -- (1,0);
            endtikzpicture

            medskip% --------------------------
            textbfDrawLine: Actual Outputpar

            DrawLineblue% <---- How do I get this case to work?

            begintikzpicture
            DrawLineorange
            endtikzpicture

            medskip% --------------------------
            textbfDrawLine: Desired Outputpar

            begintikzpicture
            DrawLineblue
            endtikzpicture

            begintikzpicture
            DrawLineorange
            endtikzpicture
            enddocument






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 1 hour ago









            David Carlisle

            475k3811041838




            475k3811041838











            • I thought it might have something to dot with the dreaded expansion issues!
              – Peter Grill
              1 hour ago






            • 2




              @PeterGrill I think the main error was IfInTikzPicendtikzpicture% as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
              – David Carlisle
              1 hour ago










            • Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
              – Peter Grill
              44 mins ago











            • @PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
              – David Carlisle
              4 mins ago
















            • I thought it might have something to dot with the dreaded expansion issues!
              – Peter Grill
              1 hour ago






            • 2




              @PeterGrill I think the main error was IfInTikzPicendtikzpicture% as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
              – David Carlisle
              1 hour ago










            • Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
              – Peter Grill
              44 mins ago











            • @PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
              – David Carlisle
              4 mins ago















            I thought it might have something to dot with the dreaded expansion issues!
            – Peter Grill
            1 hour ago




            I thought it might have something to dot with the dreaded expansion issues!
            – Peter Grill
            1 hour ago




            2




            2




            @PeterGrill I think the main error was IfInTikzPicendtikzpicture% as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
            – David Carlisle
            1 hour ago




            @PeterGrill I think the main error was IfInTikzPicendtikzpicture% as by that point you are always in a tikz picture, you need to know if you started it or if you were already in one.
            – David Carlisle
            1 hour ago












            Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
            – Peter Grill
            44 mins ago





            Opsss..., thats a bit embarrassing. I'd prefer to not duplicate the drawing code though.
            – Peter Grill
            44 mins ago













            @PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
            – David Carlisle
            4 mins ago




            @PeterGrill duplicating it isn't needed, I could post a variant without, i just started by simplifying it
            – David Carlisle
            4 mins ago

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f458849%2fdetecting-if-inside-a-tikzpicture%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