我确信这个问题已经以多种不同的方式被问过很多次了,但到目前为止,我发现的所有方法都无法奏效。所以我们开始吧:
我正在使用以下代码(我曾经在这里的某个地方找到过)来全局切换 tikz 图片的渲染:
\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 }
现在,我还在某些我总是希望渲染 tikz 图片的地方使用 tikz 图片。对于这些地方,我使用以下宏:
\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};}
}
我想在那个propbox
对全局值敏感的宏中添加一个条件showtikz
,如下所示:
if not showtikz:
temp = showtikz
showtikz = True
# probbox code
showtikz = temp
else:
# probbox code
类似这样。我该怎么做?
答案1
请注意,您必须分别处理\tikz
命令和{tikzpicture}
环境。要摆脱\tikz
命令,您可以使用\let\tikz\@gobble
重新定义命令\tikz
以删除其参数。要摆脱{tikzpicture}
环境,您可以让它等于verbatim
包中的注释环境。
为了定义\propbox
,我们恢复组内部的定义,放入的定义\propbox
,然后结束该组,这样我们暂时恢复的定义就超出了范围。
以下是带有示例的完整代码:
\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}