如何从 tikzpicture 环境之外更改一个节点的样式

如何从 tikzpicture 环境之外更改一个节点的样式

每次调用相关章节中的图形时,我都想突出显示 tikz 流程图的不同节点。

我原本想定义一种新样式“highlight”,然后在包含 tikz 图片时传递一个参数,使用附加参数设置样式为<node name> == <parameter>hightight。但是,我是 tikz 新手,所以我不知道该怎么做。

下面的代码(tikzpicture)包含在相关章节中,使用以下内容:

\includestandalone[width=\textwidth]{tikz/content}
\caption{Outline of this thesis.}
\label{fig:contents}
\end{figure}

提前感谢您的阅读和帮助。

朱利安

\documentclass[border=2pt]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{fix-cm}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{positioning}
\begin{document}
\pagestyle{empty}

% Define block styles

\tikzset{block/.style={draw, rectangle, fill=gray!20, text centered, minimum height = 3em,text width=7em}}
\tikzset{empty/.style={draw, rectangle, fill=none, text centered, minimum height = 3em,text width=7em}}
\tikzset{line/.style={draw, -latex'}}

\begin{tikzpicture}[node distance = 1cm, auto]

    % Place nodes
    \node [block, text width=30em] (init){\Large Aeroelastic modeling from wind tunnel tests using frequency domain identification};
    \node [block, text width=17em, below=1em of init] (sysid) {\large System identification\\ in aeroelasticity};


    \node [empty, text width = 12em, below=1em of sysid, node distance= 3em] (theory) {Theory \& Methods \\ \textit{Chapter \ref{chpt:theory}}};


    \node [block, text width = 16em, below left=1em and -5em of theory] (unsteady) {\textbf{Unsteady Aerodynamics} \\ 2D Full-Span (Part 1)};
    \node [block, text width = 18em, below right=1em and -5em of theory] (aeroelastic) {\textbf{Aeroelastic} \\3D Cantilever (Part 2)};

    \node [block, below left=0.5em and -7.8em of unsteady] (setupone) {Setup};
    \node [block, right=1em of setupone] (expone) {Experiments};
    \node [empty, below=0.5em of setupone] (setoneCh) {The AATB \\ \textit{Chapter \ref{chpt:AATB}}};
    \node [empty, below=0.5em of expone] (unsteadyExp) {Forced-motion \\ \textit{Chapter \ref{chpt:forced-motion}}};

        \node [block, text width =8em, below left=0.5em and -8.8em of aeroelastic] (setuptwo) {Setup};
    \node [block, right=1.2em of setuptwo, text width = 8em] (exptwo) {Experiments};
    \node [empty,  text width =8em, below=0.5em of setuptwo] (settwoCh) { Cantilver AATB \\ \textit{Chapter \ref{chpt:cantiAATB}}};

    \node [empty,text width = 7em, below right=0.5em and -7.8em of exptwo] (NL) {Nonlinear response \\ \textit{Chapter \ref{chpt:aeroelastic}}};
    \node [empty,text width = 7em, below=0.5em of NL] (gust) {Gust response \\ \textit{Chapter \ref{chpt:cantiGust}}};
    \node [empty,text width = 7em, below=0.5em of gust] (tv) {Time-varying \\ \textit{Chapter \ref{chpt:TV}}};

    \node [below left=-0.75em and -0.8em of exptwo] (startexp) {};   

    % Draw edges
    \path [line] (init) -- (sysid);
    \path [line] (sysid) -- (theory);
    \path [line] (sysid) -| (unsteady);
    \path [line] (sysid) -| (aeroelastic);
    \draw (unsteady) |- (setupone);
    \draw (unsteady) |- (expone);
    \draw (aeroelastic) |- (setuptwo);
    \draw (aeroelastic) |- (exptwo);
    \draw (startexp) |- (NL);
    \draw (startexp) |- (gust);
    \draw (startexp) |- (tv);
\end{tikzpicture}

\end{document}

答案1

我建议你.provide style使用是否有类似 \providetikzstyle 和 \providecommand 的东西?,并在调用图片之前进行设置。

因此,在您的图片中使用以下方式定义默认样式.provide style

\tikzset{Node One Style/.provide style={block}}%
\tikzset{Node Two Style/.provide style={block}}%

然后用覆盖这些

\tikzset{Node One Style/.style={highlight block}}%

当你想改变的时候,说出 的风格Node One

笔记:

  • 请包含平均能量损失(在 MWE 中强调M)。
  • 在第一个解决方案中,一旦设置了样式,就需要再次设置才能禁用它。如果不需要这样做,您可以使用组设置样式,或者使用传入Alternate Solution要突出显示的节点的。如果您总是只更改一个节点的样式,那么替代解决方案更好。第一个解决方案提供了更大的灵活性。

enter image description here

代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

%% https://tex.stackexchange.com/questions/22640/is-there-something-like-providetikzstyle-similar-to-providecommand
\tikzset{/handlers/.provide style/.code={%
    \pgfkeysifdefined{\pgfkeyscurrentpath/.@cmd}{}%
        {\pgfkeys {\pgfkeyscurrentpath /.code=\pgfkeysalso {#1}}}%
}}

% Define block styles

\tikzset{block/.style={draw, rectangle, fill=gray!20, text centered, minimum height = 3em,text width=7em}}
\tikzset{highlight block/.style={draw, rectangle, fill=red!20, text centered, minimum height = 3em,text width=7em}}

\newcommand{\MyPicture}{%
    \tikzset{Node One Style/.provide style={block}}%
    \tikzset{Node Two Style/.provide style={block}}%
    % -----------------
    \begin{tikzpicture}[node distance = 1cm, auto, text width=10em]
        \node [Node One Style]                   (one) {Node One};
        \node [Node Two Style, below=1em of one] (two) {Node Two};

    \end{tikzpicture}%
}%

\begin{document}
\textbf{First Time}
\par\hspace*{1.0cm}\MyPicture

\textbf{Second Time}
\tikzset{Node One Style/.style={highlight block}}%
\par\hspace*{1.0cm}\MyPicture

\textbf{Third Time}
\tikzset{Node One Style/.style={block}}%
\tikzset{Node Two Style/.style={highlight block}}%
\par\hspace*{1.0cm}\MyPicture
\end{document}

代码Alternate Solution

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

%% https://tex.stackexchange.com/questions/22640/is-there-something-like-providetikzstyle-similar-to-providecommand
\tikzset{/handlers/.provide style/.code={%
    \pgfkeysifdefined{\pgfkeyscurrentpath/.@cmd}{}%
        {\pgfkeys {\pgfkeyscurrentpath /.code=\pgfkeysalso {#1}}}%
}}

% Define block styles

\tikzset{block/.style={draw, rectangle, fill=gray!20, text centered, minimum height = 3em,text width=7em}}
\tikzset{highlight block/.style={draw, rectangle, fill=red!20, text centered, minimum height = 3em,text width=7em}}

\newcommand{\MyPicture}[1]{%
    \tikzset{#1/.style={highlight block}}%
    \tikzset{Node One Style/.provide style={block}}%
    \tikzset{Node Two Style/.provide style={block}}%
    % -----------------
    \begin{tikzpicture}[node distance = 1cm, auto, text width=10em]
        \node [Node One Style]                   (one) {Node One};
        \node [Node Two Style, below=1em of one] (two) {Node Two};

    \end{tikzpicture}%
}%

\begin{document}
\textbf{First Time}
\par\hspace*{1.0cm}\MyPicture{Unused Style}

\textbf{Second Time}
\par\hspace*{1.0cm}\MyPicture{Node One Style}

\textbf{Third Time}
\par\hspace*{1.0cm}\MyPicture{Node Two Style}
\end{document}

答案2

按照以下方式替换要从外部管理的节点(对于其他节点则无所谓):而不是

\node[<node style>] (<node name>) {<node contents>};

\node[<node style>,managed=<node name>] {<node contents>};

这样我们managed不仅可以设置节点的名称,还可以添加额外的样式元素来突出显示节点。 的定义managed

\tikzset
 {managed/.unknown/.style={},% if managed/<node name> is unknown do nothing
  managed/.style={name=#1,managed/#1}% set the name of the node and apply the style managed/<node name>
 }

之后,您可以通过以下方式为节点提供额外的样式元素:

\begin{tikzpicture}[managed/<node name>/.style={<additional style for this node>}]

或更远的地方

\tikzset{managed/<node name>/.style={<additional style for this node>}}

在后一种情况下,您可以通过周围来限定突出显示的范围\tikzset,并通过括号 ( ) 限定其适用的图片{}

下面是重写的示例,其中突出显示了两个节点。

\documentclass[border=2pt]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{fix-cm}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{positioning}
\pagestyle{empty}
\tikzset{block/.style={draw, rectangle, fill=gray!20, text centered, minimum height = 3em,text width=7em}}
\tikzset{empty/.style={draw, rectangle, fill=none, text centered, minimum height = 3em,text width=7em}}
\tikzset{line/.style={draw, -latex'}}
\tikzset
 {managed/.unknown/.style={},
  managed/.style={name=#1,managed/#1}
 }
\begin{document}
\begin{tikzpicture}[node distance = 1cm, auto,managed/exptwo/.style={text=red},managed/init/.style={fill=blue!20}]
\node [block, block, text width=30em,managed=init] {\Large Aeroelastic modeling from wind tunnel tests using frequency domain identification};
\node [block, text width=17em, below=1em of init,managed=sysid] {\large System identification\\ in aeroelasticity};
\node [empty, text width = 12em, below=1em of sysid, node distance= 3em,managed=theory] {Theory \& Methods \\ \textit{Chapter \ref{chpt:theory}}};
\node [block, text width = 16em, below left=1em and -5em of theory,managed=unsteady] {\textbf{Unsteady Aerodynamics} \\ 2D Full-Span (Part 1)};
\node [block, text width = 18em, below right=1em and -5em of theory,managed=aeroelastic] {\textbf{Aeroelastic} \\3D Cantilever (Part 2)};
\node [block, below left=0.5em and -7.8em of unsteady,managed=setupone] {Setup};
\node [block, right=1em of setupone,managed=expone] {Experiments};
\node [empty, below=0.5em of setupone,managed=setoneCh] {The AATB \\ \textit{Chapter \ref{chpt:AATB}}};
\node [empty, below=0.5em of expone,managed=unsteadyExp] {Forced-motion \\ \textit{Chapter \ref{chpt:forced-motion}}};
\node [block, text width =8em, below left=0.5em and -8.8em of aeroelastic,managed=setuptwo] {Setup};
\node [block, right=1.2em of setuptwo, text width = 8em, managed=exptwo] {Experiments};
\node [empty,  text width =8em, below=0.5em of setuptwo,managed=settwoCh] { Cantilver AATB \\ \textit{Chapter \ref{chpt:cantiAATB}}};
\node [empty,text width = 7em, below right=0.5em and -7.8em of exptwo,managed=NL] {Nonlinear response \\ \textit{Chapter \ref{chpt:aeroelastic}}};
\node [empty,text width = 7em, below=0.5em of NL,managed=gust] {Gust response \\ \textit{Chapter \ref{chpt:cantiGust}}};
\node [empty,text width = 7em, below=0.5em of gust,managed=tv] {Time-varying \\ \textit{Chapter \ref{chpt:TV}}};
\node [below left=-0.75em and -0.8em of exptwo,managed=startexp] {};   
\path [line] (init) -- (sysid);
\path [line] (sysid) -- (theory);
\path [line] (sysid) -| (unsteady);
\path [line] (sysid) -| (aeroelastic);
\draw (unsteady) |- (setupone);
\draw (unsteady) |- (expone);
\draw (aeroelastic) |- (setuptwo);
\draw (aeroelastic) |- (exptwo);
\draw (startexp) |- (NL);
\draw (startexp) |- (gust);
\draw (startexp) |- (tv);
\end{tikzpicture}
\end{document}

相关内容