定义包含颜色的变量

定义包含颜色的变量

我们可以定义一个变量并将其传递给模块

\pgfmathsetmacro\j{6}

这意味着 j=6,我如何为颜色定义一个变量,我的意思是我想将颜色传递给一个模块

\pgfmathsetmacro\colr{white}

但我遇到了Unknown function 'white'。我该如何解决?!我有以下代码

\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{appendix}
\usepackage{array,textcomp}
\usepackage[latin1]{inputenc}
\usetikzlibrary{bayesnet}
\usetikzlibrary{positioning}
\usepackage{color}
\usepackage{caption}
\pgfmathsetseed{5}
\makeatletter
\DeclareRobustCommand{\rvdots}{%
  \vbox{
    \baselineskip4\p@\lineskiplimit\z@
    \kern-\p@
    \hbox{.}\hbox{.}\hbox{.}
  }}
\makeatother
\tikzset{main/.style={circle, minimum size = .1cm, thick, draw =black!80, node distance = 3mm},
         connect/.style={-latex, thick},
         box/.style={rectangle, draw=black}
}
\begin{document}
  \begin{tikzpicture}[lds/.pic={j=#1,colr=#2,%<----- here is the variable
      \node[box,draw=\colr] (-Latent) {};
      \node[main,minimum size=.2cm, right=of -Latent] (-L1) {$x_1^{(\j)}$};
      \node[main,minimum size=.2cm] (-L2) [right=of -L1] {$x_2^{(\j)}$};
      \node[main,minimum size=.2cm] (-Lt) [right=5mm of -L2] {$x_T^{(\j)}$};
      \node[box,draw=white!100] (-Observed) [below=of -Latent] {};
      \node[main,fill=black,text=white,minimum size=.2cm] (-O1) [right=of -Observed,below=of -L1] {$y_1^{(\j)}$};
      \node[main,fill=black,text=white,minimum size=.2cm] (-O2) [right=of -O1,below=of -L2] {$y_2^{(\j)}$};
      \node[main,fill=black,text=white,minimum size=.2cm] (-Ot) [right=of -O2,below=of -Lt] {$y_T^{(\j)}$};
      \draw (-L1.east) edge [connect] (-L2);
      \node at (16,0) {$\dots$};
      \path (-L1.south) edge [connect] (-O1);
      \path (-L2.south) edge [connect] (-O2);
      \path (-Lt.south) edge [connect] (-Ot);
           },my text/.style={rounded corners=2pt, text width=10mm,     font=\sffamily, line width=.5pt, align=left},
      my arrow/.style={rounded corners=2pt, draw=green!15, line width=1.5mm, -{Triangle[]}}]
    \pgfmathsetmacro\j{6} 
    \def\colr{white} %<----here
    \pic(lds1) at (0,7){lds};
  \end{tikzpicture}
\end{document}

答案1

我不知道您在定义开头使用的语法是否pic lds合法,我从未见过这样的语法,但我的猜测是它是非法的。当您写下时,j=#1,colr=#2我的猜测是这些标记被 tikz 忽略(除非它是一种我不知道的键值语法)。

稍后,在您的“主要”tikz 代码中,您定义了\j,这就是为什么当您\j稍后在代码中使用时它可以工作,但分配j=#1与它无关的原因。出于同样的原因,\def\colr在图形的“主要”部分也应该可以工作(在我的测试中,它有效)。

不过,我认为将多个值传递给 a 的正确语法pic(例如在这个答案如下:

\tikzset{
 pics/lds/.style 2 args={code={
 % ... here #1 and #2 can be used
 % ...
 }}
}

在您的代码中,您应该使用#1而不是\j#2而不是\colr。但是,如果您不想更改代码,您可以lds按如下方式开始 pic 的定义:

\tikzset{
 pics/lds/.style 2 args={code={
  \pgfmathsetmacro\j{#1}
  \def\colr{#2}
  % The remaining code is untouched
 }}

稍后,要使用此图片,您可以写入:\pic(lds1) at (0,7) {lds={6}{white}};

完整代码

\tikzset{
main/.style={circle, minimum size = .1cm, thick, draw =black!80, node distance = 3mm},
connect/.style={-latex, thick},
box/.style={rectangle, draw=black},
pics/lds/.style 2 args={code={
      \pgfmathsetmacro\j{#1}
      \def\colr{#2}%<----- here is the variable
      \node[box,draw=\colr] (-Latent) {};
      \node[main,minimum size=.2cm, right=of -Latent] (-L1) {$x_1^{(\j)}$};
      \node[main,minimum size=.2cm] (-L2) [right=of -L1] {$x_2^{(\j)}$};
      \node[main,minimum size=.2cm] (-Lt) [right=5mm of -L2] {$x_T^{(\j)}$};
      \node[box,draw=white!100] (-Observed) [below=of -Latent] {};
      \node[main,fill=black,text=white,minimum size=.2cm] (-O1) [right=of -Observed,below=of -L1] {$y_1^{(\j)}$};
      \node[main,fill=black,text=white,minimum size=.2cm] (-O2) [right=of -O1,below=of -L2] {$y_2^{(\j)}$};
      \node[main,fill=black,text=white,minimum size=.2cm] (-Ot) [right=of -O2,below=of -Lt] {$y_T^{(\j)}$};
      \draw (-L1.east) edge [connect] (-L2);
      \node at (16,0) {$\dots$};
      \path (-L1.south) edge [connect] (-O1);
      \path (-L2.south) edge [connect] (-O2);
      \path (-Lt.south) edge [connect] (-Ot);
           }},
%my text/.style={rounded corners=2pt, text width=10mm,font=\sffamily, line width=.5pt, align=left},
%my arrow/.style={rounded corners=2pt, draw=green!15, line width=1.5mm, -{Triangle[]}}      
}

  \begin{tikzpicture}[]
    \pic(lds1) at (0,7) {lds={6}{white}};
  \end{tikzpicture}

结果:

Result

此外,我更喜欢在定义颜色时使用\colorlet而不是。这意味着用进行更改,然后,当需要颜色时,使用与任何预定义颜色相同的语法编写它,即例如 而不是。\def\def\colr{#2}\colorlet{colr}{#2}draw=colrdraw=\colr

这样,您可以根据 中收到的颜色定义“混合” #2,例如:\colorlet{myforeground}{#2!30!white}\colorlet{mybackground}{#2!30!black}

相关内容