1

I have a macro written as \newcommand which contains a complete new tikz-environment.

How could one make a macro out of this that only contains the content of the tikz-environment, so that if one wants to have this drawing multiple times, it is drawn in one single tikz-environment?

I know I could simply strip the declaration of the tikz-environment in my macro, but that would let me use the macro outside a tikz-environment too.

Examples:

I have a macro similar to the following one:

\newcommand{\myTikzMacro}{
  \begin{tikzpicture}
    \draw (0,0) rectangle (1,1);
    \draw (1,1) rectangle (2,0);
  \end{tikzpicture}

Now I could use the macro multiple times after each other like this:

\myTikzMacro \\
\myTikzMacro \\

But then multiple tikz-environments would be used. To avoid that I'd be able to make this macro:

\newcommand{\myTikzMacro2}{
    \draw (0,0) rectangle (1,1);
    \draw (1,1) rectangle (2,0);

And use it as followed:

\begin{tikzpicture}
  \myTikzMacro2
  \myTikzMacro2
\end{tikzpicture}

But this would enable me to use the macro outside a tikzpicture environment too which I'd like to avoid too.

Is there a way to declare macros only available/visible within tikzpicture environments? (maybe with pgfkeys)

atticus
  • 557
  • 1
    I'm not sure I understand: what should happen if you use the macro outside of a tikz environment? Just nothing? Then I have to ask why. Do you expect the macro to pop out at random places? – campa Oct 07 '20 at 12:44
  • In that case I'd like the macro just not defined (latex would then error, that that macro is unknown). That's the reason why I thought of declaring the macro as a pgfkeys macro, but I don't quite know how to do that – atticus Oct 07 '20 at 12:48
  • 1
    But you would get the error anyway (undefined \draw). You might be interested in How can I check if the current code is inside a tikzpicture?. – campa Oct 07 '20 at 12:53
  • Looks like a way to get around my issue. Do you know how I'd be able to throw an Error like Error: Macro ... is not defined (outsite tikzpicture)? – atticus Oct 07 '20 at 13:02
  • Just found \PackageError{mypackage}{dont do that}{extra help} from https://tex.stackexchange.com/a/183948/206293 Thanks @campa. Would you like to write an Answer so that I can mark it as solution? – atticus Oct 07 '20 at 14:01
  • 1
    \PackageError is meaningful if you are writing a package. Is that the case? – campa Oct 07 '20 at 14:09
  • Not really up to now, but if I'm the only user it suffices to indicate an Error – atticus Oct 07 '20 at 14:12

1 Answers1

1

According to How can I check if the current code is inside a tikzpicture? tikz provides the macro

\tikzifinpicture{true branch}{false branch}

for testing whether one is in a tikzpicture; the comments in its definition seem however to throw some doubts on its correctness. (I believe it might fail in nested pictures.) Assuming that it works in most simple cases you can define

\newcommand*{\myTikzPicture}
{\tikzifinpicture
    {%
     \draw (0,0) rectangle (1,1);
     \draw (1,1) rectangle (2,0);
    }%
    {%
     \GenericError{}{Unallowed command}
             {\string\myTikzPicture\space can be used only inside a tikz picture.}{}%
    }%
}

I used here \GenericError. If the command will go into a custom package, using \PackageError will be more appropriate.

campa
  • 31,130
  • Thanks. Where can I find a documentation of \GenericError so that I can look up what't the use of the fist parameter? – atticus Oct 07 '20 at 14:31
  • 1
    @atticus texdoc source2e. Probably also the LaTeX companion but I can't check now. Laziest way (that's how I did): type four random but distinguishable texts and play until you understand how they work :-) – campa Oct 07 '20 at 14:33
  • Thanks. They say the first one is for contination. Don't know what that means, but if it works like your example with an empty parameter {}... – atticus Oct 07 '20 at 14:39