0

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:

\newif\ifshowtikz
\showtikztrue
% \showtikzfalse % <---- comment/uncomment that line

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

\renewenvironment{tikzpicture}{%
  \ifshowtikz\expandafter\oldtikzpicture
  \else
  \fi }{%
  \ifshowtikz\oldendtikzpicture%
  \else
  \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:

\newcommand{\propbox}[1]{
    \tikz{\node[
    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
else:
   # probbox code

Something like that. How can I do that?

2

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:

\documentclass{article}
\usepackage{verbatim}
\usepackage{tikz}

\makeatletter
\newcommand{\propbox}[1]{
    \bgroup % start local group
    % ensure {tikzpicture} environment is not a comment
    \let\tikzpicture\save@tikzpicture
    \let\endtikzpicture\save@endtikzpicture
    % Rather than restoring the value of \tikz shorter to use \save@tikz directly
    \save@tikz{\node[
    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
\let\save@tikzpicture\tikzpicture
\let\save@endtikzpicture\endtikzpicture
\let\save@tikz\tikz

% A no-op defintion of \tikz that correctly gobbles argument 
% whether delimited by braces or semicolon
\def\gobbletikz{
    \@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
\def\gobbletikz@#1;{}

% set tikz commands to be no-ops
\def\hidetikzpictures{
    \let\tikzpicture\comment
    \let\endtikzpicture\endcomment
    \let\tikz\gobbletikz
}

% restore tikz commands
\def\showtikzpictures{
    \let\tikzpicture\save@tikzpicture
    \let\endtikzpicture\save@endtikzpicture
    \let\tikz\save@tikz
}
\makeatother

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

\showtikzpictures
\begin{tikzpicture}
\node [draw] (0,0){shown};
\end{tikzpicture}
\propbox{shown either way}
\tikz{\node[draw] (0,0) {also shown};}
\tikz \node[draw]{also also shown};
\end{document} 
  • 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.