我想检测宏是否在 tikzpicture 内部执行,或者更具体地说是在图片节点内部执行。在我的 MWE 中,我使用了\ifpgfpicture
它,但它无法完成这项工作,因为它将节点内容视为图片的“外部”。我希望将“INSIDE”视为节点文本。
我并不想在环境中添加任何内容,比如tikzpicture
一些every picture
技巧来记住我们在环境中。是否有任何现有的状态宏/变量可以用于此目的?pgf
tikz
\documentclass{article}
\usepackage{tikz}
\newcommand{\testinpic}{%
\ifpgfpicture INSIDE \else OUTSIDE\fi%
}
\begin{document}
\testinpic
\begin{tikzpicture}
\fill[blue!10!white] (-4,-1) rectangle (4,1);
\node {\testinpic};
\end{tikzpicture}
\end{document}
答案1
你可以做
\documentclass{article}
\usepackage{tikz}
\makeatletter
\newcommand{\testinpic}{%
\ifx\pgfpictureid\@undefined OUTSIDE \else INSIDE\fi%
}
\makeatother
\begin{document}
\testinpic
\begin{tikzpicture}
\fill[blue!10!white] (-4,-1) rectangle (4,1);
\node {\testinpic};
\end{tikzpicture}
\end{document}
答案2
可能存在一些像这样的不明智的黑客行为?如果嵌套多次,就会造成混乱。
\documentclass{standalone}
\usepackage{tikz}
\makeatletter
\newif\iftikzinsidepicture
\newif\iftikzinsidenode
\def\tikz@picture[#1]{%
\pgfpicture%
\tikzinsidepicturetrue%
\let\tikz@atbegin@picture=\pgfutil@empty%
\let\tikz@atend@picture=\pgfutil@empty%
\let\tikz@transform=\relax%
\def\tikz@time{.5}%
\tikz@installcommands\scope[every picture,#1]%
\expandafter\tikz@atbegin@picture%
\tikz@lib@scope@check%
}
\let\tikz@do@fig@main@orig=\tikz@fig@main
\def\tikz@fig@main{%
\tikzinsidepicturefalse%
\tikzinsidenodetrue%
\tikz@do@fig@main@orig%
}
\makeatother
\def\test{{\footnotesize
\iftikzinsidenode
INSIDE-NODE
\iftikzinsidepicture
INSIDE-NODE-PICTURE
\else
OUTSIDE-NODE-PICTURE
\fi
\else%
\iftikzinsidepicture
INSIDE-PICTURE
OUTSIDE-NODE
\else
OUTSIDE-PICTURE
\fi
\fi}}
\begin{document}
\begin{tabular}{l}
\test \\
\begin{tikzpicture}
\fill[blue!20] (-4,-1) rectangle (4,1);
\pgftext{\test}
\node [fill=red!20] at (0,-0.5){\test};
\node [fill=white] at (0,0.5){ \tikz{\pgftext{\test}} };
\end{tikzpicture}
\end{tabular}
\end{document}