Tikz \draw 作为样式的一部分(在某些类型的每个节点内绘制某些内容)

Tikz \draw 作为样式的一部分(在某些类型的每个节点内绘制某些内容)

我目前正在学习如何使用 Tikz,并正在研究绘制自动机(特别是 Simulink 类状态模型)。目前,我正在制作一种样式,在绘制这些状态模型时,应该可以最大限度地减少我的工作量。不幸的是,我似乎无法弄清楚如何执行以下操作:

相对于使用特定样式(在本例中为 Ramp)的所有节点绘制两条线。

这是我目前拥有的:

\documentclass[preview]{standalone}

\usepackage[dutch]{babel}
\usepackage{mathtools}
\usepackage{mathdots}

\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{automata}
\usetikzlibrary{arrows}

\begin{document}
\tikzstyle{colorful} = [fill=gray!10]
\tikzstyle{block} = [draw, colorful, rectangle, minimum height=3em, minimum width=3em]
\tikzstyle{ramp} = [block, pin={below:Ramp}]
\tikzstyle{sum} = [draw, colorful, circle, node distance=1cm]
\tikzstyle{gain} = [draw, colorful, regular polygon, regular polygon sides=3, shape border rotate=30]
\tikzstyle{integrator} = [block, right=of gain, pin={below:Integrator}]%, node contents = $\frac{1}{s}$]
\tikzstyle{input} = [coordinate]
\tikzstyle{output} = [coordinate]
\tikzstyle{node} = [coordinate]
\tikzset{every pin/.style={pin distance = 1mm}}
\tikzset{every pin edge/.style={draw=none}}
\begin{tikzpicture}[auto, node distance=1cm,>=latex']
    \node [ramp] at (0,0) (input) {};
    \node [block, right=of input] (prod) {$\times$};
    \node [gain, right=of prod] (gain) {$-1$};
    \node [integrator] (system) {$\frac{1}{s}$};

    \node [output, right=of system] (output) {};

    \draw [draw,->] (gain) -- node [name=dy] {$\dot{y}$} (system);
    \draw [draw,->] (system) -- node [name=y] {$y$} (output);
    \draw [draw,->] (input) -- node {$t$} (prod);
    \draw [draw,->] (prod) -- node {} (gain);

    \node [node, below=of y] (node1) {};
    \node [node, below=of prod] (node2) {};
    \draw [->] (y) |- (node1) |- (node2) -- (prod);

    \draw (-1/3,-1/3) -- (-1/10,-1/3) -- (1/3,1/3);
%    \draw (-1/3,-1/3) -- ++(7/30,0) -- ++(13/30,2/3);
\end{tikzpicture}
\end{document}

底部的任一条线都应相对于 Ramp 样式的每个节点绘制。

答案1

下面是两个使用样式绘制节点的示例:

\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
  test1/.style={
    draw=red,
    minimum size=2cm,
    alias=thisone,
    append after command={(thisone.south west) -- (thisone.north east)}
  },
  test2/.style={
    draw=blue,
    minimum size=2cm,
    node contents={\tikz\draw circle(3mm);}
  }
}

\begin{document}
\begin{tikzpicture}
  \draw (0,0) node[test1,]{}
        (3,0) node[test2]{};
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:在您的具体示例中,您可以添加以下样式

\tikzset{
  with axes/.style={
    alias=thisone,
    append after command={
      ([shift={(2.1mm,2.1mm)}]thisone.south west) edge[red] +(2mm,0)
      ++(2mm,0) edge[red] ++(5mm,7mm)
    }
  }
}

然后以ramp这样的风格使用它:

\tikzstyle{ramp} = [block, pin={below:Ramp},with axes]

答案2

更新:(2014/12/22) 楼主希望将绘图纳入样式中。为此,pic添加了一个带有 7 个参数的样式

#1=color, #2=x1, #3=y1, #4=edge style #5=x2, #6=y2, #7=label

\tikzset{pics/.cd,
myinput/.style args={#1#2#3#4#5#6#7}{
code={  % #1=color, #2=x1, #3=y1, #4=edge style, #5=x2, #6=y2, #7=label
\draw[color=#1]  (input)  +(#2,#3) edge[#4] +(#5,#6);     % input function
\draw[]          (input)  +(-9pt,-12pt) -- +(-9pt,12pt);  % y-axis
\draw[rotate=90] (input)  +(-9pt,-12pt) --node[below=0.3cm,midway](){#7} +(-9pt,12pt);}}% x-axis
 }

在此处输入图片描述

代码

\documentclass[border=1cm]{standalone}

\usepackage[dutch]{babel}
\usepackage{mathtools}
\usepackage{mathdots}

\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{automata}
\usetikzlibrary{arrows}
\tikzset{pics/.cd,
myinput/.style args={#1#2#3#4#5#6#7}{
code={% #1=color, #2=x1, #3=y1, #4=edge style #5=x2,#6=y2, #7=label
\draw[color=#1]  (input)  +(#2,#3) edge[#4] +(#5,#6);
\draw[](input)  +(-9pt,-12pt) -- +(-9pt,12pt);
\draw[rotate=90]  (input)  +(-9pt,-12pt) --node[below=0.3cm,midway](){#7} +(-9pt,12pt);}}
 }
\begin{document}
\tikzstyle{colorful} = [fill=gray!10]
\tikzstyle{block} = [draw, colorful, rectangle, minimum height=3em, minimum width=3em]
\tikzstyle{ramp}=[block,pin={below:Ramp}]
\tikzstyle{sum} = [draw, colorful, circle, node distance=1cm]
\tikzstyle{gain} = [draw, colorful, regular polygon, regular polygon sides=3, shape border rotate=30]
\tikzstyle{integrator} = [block, right=of gain, pin={below:Integrator}]%, node contents = $\frac{1}{s}$]
\tikzstyle{input} = [coordinate]
\tikzstyle{output} = [coordinate]
\tikzstyle{node} = [coordinate]
\tikzset{every pin/.style={pin distance = 1mm}}
\tikzset{every pin edge/.style={draw=none}}

\begin{tikzpicture}[auto, node distance=1cm,>=latex']
    \node [block] at (0,0) (input) {};
    \node [block, right=of input] (prod) {$\times$};
    \node [gain, right=of prod] (gain) {$-1$};
    \node [integrator] (system) {$\frac{1}{s}$};
    \node [output, right=of system] (output) {};
    \draw [draw,->] (gain) -- node [name=dy] {$\dot{y}$} (system);
    \draw [draw,->] (system) -- node [name=y] {$y$} (output);
    \draw [draw,->] (input) -- node {$t$} (prod);
    \draw [draw,->] (prod) -- node {} (gain);

    \node [node, below=of y] (node1) {};
    \node [node, below=of prod] (node2) {};
    \draw [->] (y) |- (node1) |- (node2) -- (prod);
    % ramp function
    \pic {myinput={red}{-9pt}{-9pt}{out=45,in=225}{12pt}{12pt}{}};
    % step function
    \pic {myinput={blue}{-9pt}{5pt}{out=0,in=180}{12pt}{5pt}{}};
    % parabolic function
    \pic  {myinput={cyan}{-9pt}{-9pt}{out=0,in=-110}{12pt}{12pt}{}};
\end{tikzpicture}
\end{document}

这是一个可能的解决方案,其中提供了stepramp和函数。此处的解决方案为单位阶跃函数和抛物线函数parabolic添加了两个块。unitpara

应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?

在此处输入图片描述

代码

\documentclass[border=1cm]{standalone}

\usepackage[dutch]{babel}
\usepackage{mathtools}
\usepackage{mathdots}

\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{automata}
\usetikzlibrary{arrows}

\begin{document}
\tikzstyle{colorful} = [fill=gray!10]
\tikzstyle{block} = [draw, colorful, rectangle, minimum height=3em, minimum width=3em]
\tikzstyle{ramp}=[block,pin={below:Ramp}]
\tikzstyle{unit}=[block,pin={below:Step}]
\tikzstyle{para}=[block,pin={below:Parablic}]
\tikzstyle{sum} = [draw, colorful, circle, node distance=1cm]
\tikzstyle{gain} = [draw, colorful, regular polygon, regular polygon sides=3, shape border rotate=30]
\tikzstyle{integrator} = [block, right=of gain, pin={below:Integrator}]%, node contents = $\frac{1}{s}$]
\tikzstyle{input} = [coordinate]
\tikzstyle{output} = [coordinate]
\tikzstyle{node} = [coordinate]
\tikzset{every pin/.style={pin distance = 1mm}}
\tikzset{every pin edge/.style={draw=none}}

\begin{tikzpicture}[auto, node distance=1cm,>=latex']
    \node [ramp] at (0,0) (input) {};
    \node[unit, above =  of input] (unit){};
    \node[para, below =  of input] (para){};
    \node [block, right=of input] (prod) {$\times$};
    \node [gain, right=of prod] (gain) {$-1$};
    \node [integrator] (system) {$\frac{1}{s}$};
    \node [output, right=of system] (output) {};
    \draw [draw,->] (gain) -- node [name=dy] {$\dot{y}$} (system);
    \draw [draw,->] (system) -- node [name=y] {$y$} (output);
    \draw [draw,->] (input) -- node {$t$} (prod);
    \draw [draw,->] (prod) -- node {} (gain);

    \node [node, below=of y] (node1) {};
    \node [node, below=of prod] (node2) {};
    \draw [->] (y) |- (node1) |- (node2) -- (prod);
    % ramp function
    \draw[rotate=-45]  (input)  +(0,-12pt) -- +(0,16pt);
    \draw[]([xshift=-9pt]input)  +(0,-12pt) -- +(0,12pt);
    \draw[rotate=90]  ([xshift=-9pt]input)  +(0,-12pt) -- +(0,12pt);
    % step function
    \draw[]  (unit)  +(-9pt,5pt) -- +(12pt,5pt);
    \draw[]([xshift=-9pt]unit)  +(0,-12pt) -- +(0,12pt);
    \draw[rotate=90]  ([xshift=-9pt]unit)  +(0,-12pt) -- +(0,12pt);
    % parabolic function
    \draw[]  (para)  +(-9pt,-9pt) edge[bend right] +(12pt,12pt);
    \draw[]([xshift=-9pt]para)  +(0,-12pt) -- +(0,12pt);
    \draw[rotate=90]  ([xshift=-9pt]para)  +(0,-12pt) -- +(0,12pt);

\end{tikzpicture}
\end{document}

相关内容