I am sure this has been asked many times already in many different ways, but can't get any of what I found so far to work. So here we go:

I am using the following code (that I once found on here somewhere) to globally toggle the rendering of tikz pictures:

% \showtikzfalse % <---- comment/uncomment that line

\let\oldtikzpicture\tikzpicture \let\oldendtikzpicture\endtikzpicture

  \fi }{%
  \fi }

Now, I also use tikz pictures in certain places where I always want them to render. For these places I use the following macro:

    draw,shade,rounded corners=0.1cm, top color=teal!30,bottom color=teal!80,blur shadow={shadow blur steps=5}] (A) {#1};}

I would like to to put a conditional into that propbox macro that is sensitive to the global showtikz value, like so:

if not showtikz:
   temp = showtikz
   showtikz = True
   # probbox code
   showtikz = temp
   # probbox code

Something like that. How can I do that?


Note that you have to deal separately with the \tikz command and the {tikzpicture} environment. To get rid of \tikz commands, you can use \let\tikz\@gobble which redefines the command \tikz to just delete its argument. To get rid of {tikzpicture} environments, you can let it equal to the comment environment from the verbatim package.

To define the \propbox, we restore the definitions of both inside of a group, put in the definition of \propbox and then end the group so our temporarily restored definitions go out of scope.

Here's the full code with an example:


    \bgroup % start local group
    % ensure {tikzpicture} environment is not a comment
    % Rather than restoring the value of \tikz shorter to use \save@tikz directly
    draw,shade,rounded corners=0.1cm, top color=teal!30,bottom color=teal!80] (A) {#1};}
    \egroup % The definitions go out of scope here so if tikz is not being displayed,
    % {tikzpicture} goes back to being a comment

% Save copies of tikz commands

% A no-op defintion of \tikz that correctly gobbles argument 
% whether delimited by braces or semicolon
    \@ifnextchar\bgroup % Is the argument brace delimited?
        {\@gobble}     % If the next character is an open brace, \@gobble suffices
        {\gobbletikz@} % Otherwise, it's semicolon delimited
% gobble until the next semicolon

% set tikz commands to be no-ops

% restore tikz commands

\node [draw] (0,0){hidden};
\propbox{shown even though hidetikzpictures}
\tikz{\node[draw] (0,0) {also hidden};}
\tikz \node[draw]{also also hidden};
\vskip 12pt

\node [draw] (0,0){shown};
\propbox{shown either way}
\tikz{\node[draw] (0,0) {also shown};}
\tikz \node[draw]{also also shown};
  • 1
    save@ has no semantics, it's just supposed to indicate that it's where I'm saving things. The \save@tikz macro is where I've saved the original value of the \tikz macro. It's just a little bit shorter to use \save@tikz directly rather than saying \let\tikz\save@tikz and using \tikz, but it's equivalent. – Hood Chatham Nov 9 '17 at 13:17
  • 1
    I edited the code to add comments that hopefully clarify things. – Hood Chatham Nov 9 '17 at 13:21
  • 1
    Please note, that this won't cover cases like \tikz \draw (1,0) -- (0,1); (i.e. \tikz macro without argument braces) … – Tobi Nov 9 '17 at 13:25
  • 1
    Oh yeah, I forgot you could do that. I can add a check for that case. – Hood Chatham Nov 9 '17 at 13:34
  • 1
    @Tobi Should be fixed. – Hood Chatham Nov 9 '17 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.