TikZ 中的自訂節點

TikZ 中的自訂節點

我對這個基本問題感到抱歉,但我是 TikZ 的新手,並且在定義一個簡單的節點時遇到了很多麻煩...

我有以下程式碼:

\begin{tikzpicture}
 \draw (0,0) -- (1,-1) -- (2,0) -- (2,2) -- (0,2) -- (0,0);
\end{tikzpicture}

我想要做的就是擁有程式碼定義的形狀,以便能夠在這樣的情況下使用它:

\begin{tikzpicture}[node distance = 2cm, auto]
    % Place nodes
    \node [block] (init) {initialize model};
    \node [cloud, left of=init] (expert) {expert};
    \node [cloud, right of=init] (system) {system};
    \node [block, below of=init] (identify) {identify candidate models};
    \node [block, below of=identify] (evaluate) {evaluate candidate models};
    \node [block, left of=evaluate, node distance=3cm] (update) {update model};
    \node [decision, below of=evaluate] (decide) {is best candidate better?};
    \node [block, below of=decide, node distance=3cm] (stop) {stop};
    % Draw edges
    \path [line] (init) -- (identify);
    \path [line] (identify) -- (evaluate);
    \path [line] (evaluate) -- (decide);
    \path [line] (decide) -| node [near start] {yes} (update);
    \path [line] (update) |- (identify);
    \path [line] (decide) -- node {no}(stop);
    \path [line,dashed] (expert) -- (init);
    \path [line,dashed] (system) -- (init);
    \path [line,dashed] (system) |- (evaluate);
\end{tikzpicture}

……我可以編寫“自訂節點”(或我自己定義的東西),而不是“塊”,並讓它以相同的方式、相同的圖表出現,並在其中包含文字。

非常感謝

答案1

TikZ 有許多預先定義的符號。您所詢問的符號與庫signal中的符號非常相似shapes.symbols(文檔中的第 67.4 節)。cloud那裡還定義了一個符號。

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes.symbols}
\begin{document}
\begin{tikzpicture}[
node distance = 3cm, auto,
block/.style={signal, draw, signal to=south}]
\node [block] (init) {initialize model};
\node [cloud,draw, left of=init] (expert) {expert};
\end{tikzpicture}
\end{document}

在此輸入影像描述

答案2

Tikzpics非常適合這類事情。 tikz 手冊(版本 3.0.1a)的第 18.2 節對它們進行了詳細描述。

例如,代碼:

\documentclass{article}
\usepackage{tikz}

\tikzset{
  pics/mynode/.style args={#1,#2,#3}{
     code={
       \draw (0,0) -- (1,-1) -- (2,0) -- (2,2) -- (0,2) -- (0,0);
       \node[#3] (#1) at (1,1) {#2};
     }
  }
}

\begin{document}

  \begin{tikzpicture}
      \draw (0,0) pic{mynode={A, Hi, blue}};
      \draw (0,3) pic{mynode={B, Hello, red}};
      \draw (2,1.5) pic{mynode={C, Bye,}};
      \draw[thick, blue] (A)--(B)--(C)--(A);
  \end{tikzpicture}

\end{document}

產生圖表:

在此輸入影像描述

我已將您的“自訂節點”定義為 pic mynode。它需要三個參數:節點標籤、節點文字和節點樣式(必須給出所有三個參數,但可以留空)。該圖片繪製了您的自訂形狀,並且作為其中的一部分,在其中放置了一個「真實」節點,然後我們可以像在 MWE 中一樣使用節點標籤來引用該節點。

答案3

定義新的節點形狀不一定是基本問題。但在這種情況下,您可以使用庫single arrow中的形狀進行一些作弊shapes.arrows

在此輸入影像描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}

\tikzset{
  mycustomnode/.style={
    draw,
    single arrow,
    single arrow head extend=0,
    shape border uses incircle,
    shape border rotate=-90,
  }
}

\begin{document}
\begin{tikzpicture}
\node [mycustomnode] {};
\node [mycustomnode] at (2,0) {abc};
\end{tikzpicture}
\end{document}

相關內容