我写了一个 tikz 图表,它位于其自己的专用文件中。我想多次将此文件包含到我的文档中,每次突出显示其中的不同节点。我尝试定义一个宏来检查参数,但遗憾的是,路径内的宏扩展不起作用。还有其他想法吗?
这是我的代码:
{
\usetikzlibrary{arrows}
\usetikzlibrary{mindmap}
\begin{tikzpicture}[level 1/.append style={level distance=5ex,sibling angle=90}]
\small\it
\tikzstyle{show}=[draw,rectangle,thin,rounded corners]
\node[show] {}
child {node[show] {Monomorphism} }
child[missing]
child {node[show] {Polymorphism}
child { node[show] {Ad hoc}
child {node[show] {Overloading} }
child [missing]
child {node[show] {Coercion}}
child [missing]
}
child [missing]
child [missing]
child [missing]
child {node[show] {Universal}
child {node[show] {Parametric}
child {node[show] {Polymorphic Functions}}
child [missing]
child [missing]
child {node[show] {Polytypes}}
}
child[missing]
child[missing]
child {node[show] {Inclusion} child {node[show] {Subtype}}}
}
}
;
\end{tikzpicture}
}
答案1
首先,你必须给每个节点命名,语法如下
node[<options>] (<name>) {<node text}
我定义了一个宏\HighlightedNode
,它保存要突出显示的节点的名称,并定义了第二个宏\Highlight
来更改要突出显示的节点。突出显示只是在节点中心周围绘制的一个粗红色椭圆,如果您想要不同的东西,请告知。节点\TikzFig
包含您的树和附加
\draw [ultra thick,red] (\HighlightedNode.center) circle [x radius=5em,y radius=1.5em];
从而产生突出显示。
使用 Martin Scharrer 定义的宏我如何知道某个节点是否已定义?我检查 定义的节点是否\HighlightedNode
存在,然后才绘制高亮。通过定义\HighlightedNode
为图片中不是节点名称的字符串,这实际上关闭了高亮。
我也从 改为\tikzstyle
,\tikzset
后者是推荐的。不要使用\it
,这些双字母字体切换宏已被弃用,请改用\itshape
。最后,\small\itshape
我认为我会使用 ,而不是像你那样添加every node/.style={font=\small\itshape}
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{mindmap}
\newcommand\HighlightedNode{none}
\newcommand\Highlight[1]{\renewcommand\HighlightedNode{#1}}
\makeatletter % https://tex.stackexchange.com/questions/37709/how-can-i-know-if-a-node-is-already-defined
\long\def\ifnodedefined#1#2#3{%
\@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\makeatother
\newcommand\TikzFig{
\begin{tikzpicture}[level 1/.append style={level distance=5ex,sibling angle=90},
every node/.append style={font=\small\itshape}]
\tikzset{show/.style={draw,rectangle,thin,rounded corners}}
\node[show] {}
child {node[show] (mono) {Monomorphism} }
child[missing]
child {node[show] (poly) {Polymorphism}
child { node[show] (adhoc) {Ad hoc}
child {node[show] (overload) {Overloading} }
child [missing]
child {node[show] (coerce) {Coercion}}
child [missing]
}
child [missing]
child [missing]
child [missing]
child {node[show] (uni) {Universal}
child {node[show] (para) {Parametric}
child {node[show] (polyfunc) {Polymorphic Functions}}
child [missing]
child [missing]
child {node[show] (polytype) {Polytypes}}
}
child[missing]
child[missing]
child {node[show] (inclusion) {Inclusion} child {node[show](subtype) {Subtype}}}
}
}
;
\ifnodedefined{\HighlightedNode}{
\draw [ultra thick,red] (\HighlightedNode.center) circle [x radius=5em,y radius=1.5em];}{}
\end{tikzpicture}}
\begin{document}
\TikzFig
\Highlight{poly}
\vspace{2cm}
\TikzFig
\Highlight{coerce}
\vspace{2cm}
\TikzFig
\end{document}
答案2
也许是一种基于节点名称的样式,可以激活某种highlight
样式?
顺便说一句,看起来如果你正在使用missing
childs 来纠正树的间距。你可能对这个forest
包感兴趣森林它会自动定位节点,并在它们之间留出适当的间距(还有更多)。不过,我认为这个解决方案不会奏效。
代码
\begin{filecontents*}{\jobname.tikz}
\begin{tikzpicture}[
level 1/.append style={level distance=+5ex, sibling angle=90},
show/.style={draw,rectangle,thin,rounded corners}]
\small\itshape
\path[nodes=show] node (r) {}
child {node {Monomorphism} } child [missing]
child {node {Polymorphism}
child {node {Ad hoc}
child {node {Overloading}} child [missing]
child {node {Coercion}} child [missing] }
child [missing] child [missing] child [missing]
child {node {Universal}
child {node {Parametric}
child {node {Polymorphic Functions}}
child [missing] child [missing]
child {node {Polytypes}}}
child[missing] child[missing]
child {node {Inclusion} child {node {Subtype}}}}};
\end{tikzpicture}
\end{filecontents*}
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{mindmap}
\tikzset{highlight/.style={draw=red, text=blue, fill=green!25}}
\makeatletter
\usepackage{etoolbox}
\patchcmd\tikz@@fig@main{\tikzset{every \tikz@shape\space node/.try}}{%
\tikzset{every \tikz@shape\space node/.try}%
\tikzset{every node named \tikz@fig@name/.try}%
}{}{}
\newcommand*\inpTikZ[2][]{%
\begingroup
\def\tikz@temp{#1}% is #1 empty?
\ifx\tikz@temp\pgfutil@empty\else
\tikzset{every node named #1/.append style=highlight}%
\fi
\input{#2}%
\endgroup}
\makeatother
\begin{document}
\inpTikZ[r-1]{\jobname.tikz}
% r-2 is "missing"
\inpTikZ[r-3]{\jobname.tikz}
\inpTikZ[r-3-5]{\jobname.tikz}
\end{document}