Detecting if inside a tikzpicture











up vote
8
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]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}


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:



documentclass{article}
usepackage{tikz}

makeatletter
newcommand{IfInTikzPic}[2]{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%
}
makeatother

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

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

%textbf{IfInTikzPic}par
%IfInTikzPic{inside}{outside}
%
%begin{tikzpicture}
% IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
%end{tikzpicture}
%
%medskip% --------------------------
textbf{DrawLine}: Actual Outputpar

%DrawLine{blue}% <---- How do I get this case to work?

begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}

medskip% --------------------------
textbf{DrawLine}: Desired Outputpar

begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}

begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}









share|improve this question




















  • 1




    tex.stackexchange.com/questions/18652/… perhaps
    – Torbjørn T.
    yesterday










  • Or tex.stackexchange.com/questions/412456/…
    – marmot
    yesterday










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












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















up vote
8
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]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}


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:



documentclass{article}
usepackage{tikz}

makeatletter
newcommand{IfInTikzPic}[2]{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%
}
makeatother

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

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

%textbf{IfInTikzPic}par
%IfInTikzPic{inside}{outside}
%
%begin{tikzpicture}
% IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
%end{tikzpicture}
%
%medskip% --------------------------
textbf{DrawLine}: Actual Outputpar

%DrawLine{blue}% <---- How do I get this case to work?

begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}

medskip% --------------------------
textbf{DrawLine}: Desired Outputpar

begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}

begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}









share|improve this question




















  • 1




    tex.stackexchange.com/questions/18652/… perhaps
    – Torbjørn T.
    yesterday










  • Or tex.stackexchange.com/questions/412456/…
    – marmot
    yesterday










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












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













up vote
8
down vote

favorite









up vote
8
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]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}


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:



documentclass{article}
usepackage{tikz}

makeatletter
newcommand{IfInTikzPic}[2]{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%
}
makeatother

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

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

%textbf{IfInTikzPic}par
%IfInTikzPic{inside}{outside}
%
%begin{tikzpicture}
% IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
%end{tikzpicture}
%
%medskip% --------------------------
textbf{DrawLine}: Actual Outputpar

%DrawLine{blue}% <---- How do I get this case to work?

begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}

medskip% --------------------------
textbf{DrawLine}: Desired Outputpar

begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}

begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}









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]{%
IfInTikzPic{}{tikzpicture[remember picture]}
draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
IfInTikzPic{}{endtikzpicture}%
}


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:



documentclass{article}
usepackage{tikz}

makeatletter
newcommand{IfInTikzPic}[2]{% https://tex.stackexchange.com/a/121309/4301
ifxpgfpictureid@undefined#2else#1fi%
}
makeatother

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

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

%textbf{IfInTikzPic}par
%IfInTikzPic{inside}{outside}
%
%begin{tikzpicture}
% IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
%end{tikzpicture}
%
%medskip% --------------------------
textbf{DrawLine}: Actual Outputpar

%DrawLine{blue}% <---- How do I get this case to work?

begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}

medskip% --------------------------
textbf{DrawLine}: Desired Outputpar

begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}

begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}
end{document}






tikz-pgf grouping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









Peter Grill

162k24432738




162k24432738








  • 1




    tex.stackexchange.com/questions/18652/… perhaps
    – Torbjørn T.
    yesterday










  • Or tex.stackexchange.com/questions/412456/…
    – marmot
    yesterday










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












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














  • 1




    tex.stackexchange.com/questions/18652/… perhaps
    – Torbjørn T.
    yesterday










  • Or tex.stackexchange.com/questions/412456/…
    – marmot
    yesterday










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












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








1




1




tex.stackexchange.com/questions/18652/… perhaps
– Torbjørn T.
yesterday




tex.stackexchange.com/questions/18652/… perhaps
– Torbjørn T.
yesterday












Or tex.stackexchange.com/questions/412456/…
– marmot
yesterday




Or tex.stackexchange.com/questions/412456/…
– marmot
yesterday












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






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














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




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










2 Answers
2






active

oldest

votes

















up vote
6
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



documentclass{article}

usepackage{tikz}

makeatletter
def@tikzenvironment{tikzpicture}

newcommand{IfInTikzPic}{%
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

begin{document}

textbf{DrawLine}: Actual Outputpar

DrawLine{blue}

begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}

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

textbf{DrawLine}: Desired Outputpar

begin{tikzpicture}
DrawLine{blue}
end{tikzpicture}

begin{tikzpicture}
DrawLine{orange}
end{tikzpicture}

end{document}


You could also group the tikzpicture if needed:



newcommand{IfInTikzPic}{%
edeftikzstart{%
ifx@currenvir@tikzenvironmentelse
noexpandbegin{tikzpicture}[remember picture]%
fi
}%
edeftikzend{%
ifx@currenvir@tikzenvironmentelse
noexpandend{tikzpicture}%
fi
}%
}





share|improve this answer






























    up vote
    5
    down vote













    documentclass{article}
    usepackage{tikz}

    makeatletter
    newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
    ifxpgfpictureid@undefined
    expandafter@firstoftwo
    else
    expandafter@secondoftwo
    fi
    }
    makeatother

    newcommand*{DrawLine}[1]{%
    IfInTikzPic
    {begin{tikzpicture}[remember picture]
    draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
    end{tikzpicture}}%
    {draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
    }

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

    textbf{IfInTikzPic}par
    IfInTikzPic{inside}{outside}

    begin{tikzpicture}
    IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
    end{tikzpicture}

    medskip% --------------------------
    textbf{DrawLine}: Actual Outputpar

    DrawLine{blue}% <---- How do I get this case to work?

    begin{tikzpicture}
    DrawLine{orange}
    end{tikzpicture}

    medskip% --------------------------
    textbf{DrawLine}: Desired Outputpar

    begin{tikzpicture}
    DrawLine{blue}
    end{tikzpicture}

    begin{tikzpicture}
    DrawLine{orange}
    end{tikzpicture}
    end{document}




    Or this version which directly forces tikz rather than having an if-then-else construct



    documentclass{article}
    usepackage{tikz}

    makeatletter
    newcommand{ensuretikz}{% https://tex.stackexchange.com/a/121309/4301
    ifxpgfpictureid@undefined
    expandaftertikzify
    else
    expandafter@firstofone
    fi
    }
    makeatother

    deftikzify#1{begin{tikzpicture}#1end{tikzpicture}}

    newcommand*{DrawLine}[1]{%
    ensuretikz
    {draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
    }

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




    medskip% --------------------------
    textbf{DrawLine}: Actual Outputpar

    DrawLine{blue}% <---- How do I get this case to work?

    begin{tikzpicture}
    DrawLine{orange}
    end{tikzpicture}

    medskip% --------------------------
    textbf{DrawLine}: Desired Outputpar

    begin{tikzpicture}
    DrawLine{blue}
    end{tikzpicture}

    begin{tikzpicture}
    DrawLine{orange}
    end{tikzpicture}
    end{document}





    share|improve this answer























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






    • 2




      @PeterGrill I think the main error was IfInTikzPic{}{endtikzpicture}% 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
      yesterday










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












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










    • @PeterGrill plan b posted
      – David Carlisle
      yesterday











    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
    6
    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



    documentclass{article}

    usepackage{tikz}

    makeatletter
    def@tikzenvironment{tikzpicture}

    newcommand{IfInTikzPic}{%
    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

    begin{document}

    textbf{DrawLine}: Actual Outputpar

    DrawLine{blue}

    begin{tikzpicture}
    DrawLine{orange}
    end{tikzpicture}

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

    textbf{DrawLine}: Desired Outputpar

    begin{tikzpicture}
    DrawLine{blue}
    end{tikzpicture}

    begin{tikzpicture}
    DrawLine{orange}
    end{tikzpicture}

    end{document}


    You could also group the tikzpicture if needed:



    newcommand{IfInTikzPic}{%
    edeftikzstart{%
    ifx@currenvir@tikzenvironmentelse
    noexpandbegin{tikzpicture}[remember picture]%
    fi
    }%
    edeftikzend{%
    ifx@currenvir@tikzenvironmentelse
    noexpandend{tikzpicture}%
    fi
    }%
    }





    share|improve this answer



























      up vote
      6
      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



      documentclass{article}

      usepackage{tikz}

      makeatletter
      def@tikzenvironment{tikzpicture}

      newcommand{IfInTikzPic}{%
      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

      begin{document}

      textbf{DrawLine}: Actual Outputpar

      DrawLine{blue}

      begin{tikzpicture}
      DrawLine{orange}
      end{tikzpicture}

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

      textbf{DrawLine}: Desired Outputpar

      begin{tikzpicture}
      DrawLine{blue}
      end{tikzpicture}

      begin{tikzpicture}
      DrawLine{orange}
      end{tikzpicture}

      end{document}


      You could also group the tikzpicture if needed:



      newcommand{IfInTikzPic}{%
      edeftikzstart{%
      ifx@currenvir@tikzenvironmentelse
      noexpandbegin{tikzpicture}[remember picture]%
      fi
      }%
      edeftikzend{%
      ifx@currenvir@tikzenvironmentelse
      noexpandend{tikzpicture}%
      fi
      }%
      }





      share|improve this answer

























        up vote
        6
        down vote










        up vote
        6
        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



        documentclass{article}

        usepackage{tikz}

        makeatletter
        def@tikzenvironment{tikzpicture}

        newcommand{IfInTikzPic}{%
        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

        begin{document}

        textbf{DrawLine}: Actual Outputpar

        DrawLine{blue}

        begin{tikzpicture}
        DrawLine{orange}
        end{tikzpicture}

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

        textbf{DrawLine}: Desired Outputpar

        begin{tikzpicture}
        DrawLine{blue}
        end{tikzpicture}

        begin{tikzpicture}
        DrawLine{orange}
        end{tikzpicture}

        end{document}


        You could also group the tikzpicture if needed:



        newcommand{IfInTikzPic}{%
        edeftikzstart{%
        ifx@currenvir@tikzenvironmentelse
        noexpandbegin{tikzpicture}[remember picture]%
        fi
        }%
        edeftikzend{%
        ifx@currenvir@tikzenvironmentelse
        noexpandend{tikzpicture}%
        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



        documentclass{article}

        usepackage{tikz}

        makeatletter
        def@tikzenvironment{tikzpicture}

        newcommand{IfInTikzPic}{%
        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

        begin{document}

        textbf{DrawLine}: Actual Outputpar

        DrawLine{blue}

        begin{tikzpicture}
        DrawLine{orange}
        end{tikzpicture}

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

        textbf{DrawLine}: Desired Outputpar

        begin{tikzpicture}
        DrawLine{blue}
        end{tikzpicture}

        begin{tikzpicture}
        DrawLine{orange}
        end{tikzpicture}

        end{document}


        You could also group the tikzpicture if needed:



        newcommand{IfInTikzPic}{%
        edeftikzstart{%
        ifx@currenvir@tikzenvironmentelse
        noexpandbegin{tikzpicture}[remember picture]%
        fi
        }%
        edeftikzend{%
        ifx@currenvir@tikzenvironmentelse
        noexpandend{tikzpicture}%
        fi
        }%
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        Werner

        429k589401618




        429k589401618






















            up vote
            5
            down vote













            documentclass{article}
            usepackage{tikz}

            makeatletter
            newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            }
            makeatother

            newcommand*{DrawLine}[1]{%
            IfInTikzPic
            {begin{tikzpicture}[remember picture]
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
            end{tikzpicture}}%
            {draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
            }

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

            textbf{IfInTikzPic}par
            IfInTikzPic{inside}{outside}

            begin{tikzpicture}
            IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Actual Outputpar

            DrawLine{blue}% <---- How do I get this case to work?

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Desired Outputpar

            begin{tikzpicture}
            DrawLine{blue}
            end{tikzpicture}

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}
            end{document}




            Or this version which directly forces tikz rather than having an if-then-else construct



            documentclass{article}
            usepackage{tikz}

            makeatletter
            newcommand{ensuretikz}{% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandaftertikzify
            else
            expandafter@firstofone
            fi
            }
            makeatother

            deftikzify#1{begin{tikzpicture}#1end{tikzpicture}}

            newcommand*{DrawLine}[1]{%
            ensuretikz
            {draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
            }

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




            medskip% --------------------------
            textbf{DrawLine}: Actual Outputpar

            DrawLine{blue}% <---- How do I get this case to work?

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Desired Outputpar

            begin{tikzpicture}
            DrawLine{blue}
            end{tikzpicture}

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}
            end{document}





            share|improve this answer























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






            • 2




              @PeterGrill I think the main error was IfInTikzPic{}{endtikzpicture}% 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
              yesterday










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












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










            • @PeterGrill plan b posted
              – David Carlisle
              yesterday















            up vote
            5
            down vote













            documentclass{article}
            usepackage{tikz}

            makeatletter
            newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            }
            makeatother

            newcommand*{DrawLine}[1]{%
            IfInTikzPic
            {begin{tikzpicture}[remember picture]
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
            end{tikzpicture}}%
            {draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
            }

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

            textbf{IfInTikzPic}par
            IfInTikzPic{inside}{outside}

            begin{tikzpicture}
            IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Actual Outputpar

            DrawLine{blue}% <---- How do I get this case to work?

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Desired Outputpar

            begin{tikzpicture}
            DrawLine{blue}
            end{tikzpicture}

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}
            end{document}




            Or this version which directly forces tikz rather than having an if-then-else construct



            documentclass{article}
            usepackage{tikz}

            makeatletter
            newcommand{ensuretikz}{% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandaftertikzify
            else
            expandafter@firstofone
            fi
            }
            makeatother

            deftikzify#1{begin{tikzpicture}#1end{tikzpicture}}

            newcommand*{DrawLine}[1]{%
            ensuretikz
            {draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
            }

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




            medskip% --------------------------
            textbf{DrawLine}: Actual Outputpar

            DrawLine{blue}% <---- How do I get this case to work?

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Desired Outputpar

            begin{tikzpicture}
            DrawLine{blue}
            end{tikzpicture}

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}
            end{document}





            share|improve this answer























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






            • 2




              @PeterGrill I think the main error was IfInTikzPic{}{endtikzpicture}% 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
              yesterday










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












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










            • @PeterGrill plan b posted
              – David Carlisle
              yesterday













            up vote
            5
            down vote










            up vote
            5
            down vote









            documentclass{article}
            usepackage{tikz}

            makeatletter
            newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            }
            makeatother

            newcommand*{DrawLine}[1]{%
            IfInTikzPic
            {begin{tikzpicture}[remember picture]
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
            end{tikzpicture}}%
            {draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
            }

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

            textbf{IfInTikzPic}par
            IfInTikzPic{inside}{outside}

            begin{tikzpicture}
            IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Actual Outputpar

            DrawLine{blue}% <---- How do I get this case to work?

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Desired Outputpar

            begin{tikzpicture}
            DrawLine{blue}
            end{tikzpicture}

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}
            end{document}




            Or this version which directly forces tikz rather than having an if-then-else construct



            documentclass{article}
            usepackage{tikz}

            makeatletter
            newcommand{ensuretikz}{% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandaftertikzify
            else
            expandafter@firstofone
            fi
            }
            makeatother

            deftikzify#1{begin{tikzpicture}#1end{tikzpicture}}

            newcommand*{DrawLine}[1]{%
            ensuretikz
            {draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
            }

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




            medskip% --------------------------
            textbf{DrawLine}: Actual Outputpar

            DrawLine{blue}% <---- How do I get this case to work?

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Desired Outputpar

            begin{tikzpicture}
            DrawLine{blue}
            end{tikzpicture}

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}
            end{document}





            share|improve this answer














            documentclass{article}
            usepackage{tikz}

            makeatletter
            newcommand{IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            }
            makeatother

            newcommand*{DrawLine}[1]{%
            IfInTikzPic
            {begin{tikzpicture}[remember picture]
            draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};
            end{tikzpicture}}%
            {draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
            }

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

            textbf{IfInTikzPic}par
            IfInTikzPic{inside}{outside}

            begin{tikzpicture}
            IfInTikzPic{draw [red, ultra thick]}{draw [blue, ultra thick]} (0,0) -- (1,0);
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Actual Outputpar

            DrawLine{blue}% <---- How do I get this case to work?

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Desired Outputpar

            begin{tikzpicture}
            DrawLine{blue}
            end{tikzpicture}

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}
            end{document}




            Or this version which directly forces tikz rather than having an if-then-else construct



            documentclass{article}
            usepackage{tikz}

            makeatletter
            newcommand{ensuretikz}{% https://tex.stackexchange.com/a/121309/4301
            ifxpgfpictureid@undefined
            expandaftertikzify
            else
            expandafter@firstofone
            fi
            }
            makeatother

            deftikzify#1{begin{tikzpicture}#1end{tikzpicture}}

            newcommand*{DrawLine}[1]{%
            ensuretikz
            {draw[ultra thick, ->, #1] (0,) -- (1,1) node [right] {output of DrawLine};}%
            }

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




            medskip% --------------------------
            textbf{DrawLine}: Actual Outputpar

            DrawLine{blue}% <---- How do I get this case to work?

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}

            medskip% --------------------------
            textbf{DrawLine}: Desired Outputpar

            begin{tikzpicture}
            DrawLine{blue}
            end{tikzpicture}

            begin{tikzpicture}
            DrawLine{orange}
            end{tikzpicture}
            end{document}






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday

























            answered yesterday









            David Carlisle

            475k3811051840




            475k3811051840












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






            • 2




              @PeterGrill I think the main error was IfInTikzPic{}{endtikzpicture}% 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
              yesterday










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












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










            • @PeterGrill plan b posted
              – David Carlisle
              yesterday


















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






            • 2




              @PeterGrill I think the main error was IfInTikzPic{}{endtikzpicture}% 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
              yesterday










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












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










            • @PeterGrill plan b posted
              – David Carlisle
              yesterday
















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




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




            2




            2




            @PeterGrill I think the main error was IfInTikzPic{}{endtikzpicture}% 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
            yesterday




            @PeterGrill I think the main error was IfInTikzPic{}{endtikzpicture}% 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
            yesterday












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






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














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




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












            @PeterGrill plan b posted
            – David Carlisle
            yesterday




            @PeterGrill plan b posted
            – David Carlisle
            yesterday


















             

            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




















































































            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff