6

I'm sorry for the basic question, but I'm new to TikZ and I'm having a lot of trouble just defining a simple node...

I've got the following code:

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

All I want to do is have the shape defined by the code to be able to use it in a situation like this:

\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}

... where instead of e.g. "block", I would be able to write "customnode" (or something I've defined myself) and have it appear in the same way, in the same sort of diagram, with text inside.

Many thanks

4

TikZ has many predefined symbols. The one you're asking about is very similar to the signal symbol in the shapes.symbols library (section 67.4 in the documentation). A cloud symbol is also defined there.

\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}

enter image description here

10

Tikz pics are great for this sort of thing. They are described in great detail in section 18.2 of the tikz manual (version 3.0.1a).

For example, the code:

\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}

produces the diagram:

enter image description here

I have defined your "custom node" as the pic mynode. It takes three arguments: the node label, node text and styling for the node (all three arguments must be given, but they can be left blank). The pic draws your custom shape and, as part of this, places a "real" node inside it, which can then we referred to using the node label as in the MWE.

5

Defining new node shapes isn't necessarily a basic question. But in this case you can cheat a bit by using the single arrow shape from the shapes.arrows library.

enter image description here

\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}
  • The large amount of space inside the node is a drawback. You can add inner sep=0pt to the style definition to reduce it a bit, but it's still a bit much. – Torbjørn T. Apr 26 '17 at 13:49
  • Great, thank you very much. I've marked this as the answer. Just for my own interest, does it involve much more work to actually define a node? I may have to do so for other shapes in the near future. – Ben A Apr 26 '17 at 13:54
  • @BenA I've never actually done it myself, but it involves more low-level and complicated code I think, see e.g. tex.stackexchange.com/a/33156/586 (The reference to the manual is not correct for version 3 of TikZ, I think chapter 101 is the appropriate one.) Edit: you can find other examples here on the site as well, search some, or see the related questions in the right margin of the above mentioned question. – Torbjørn T. Apr 26 '17 at 13:59
  • Ok, in that case I think I'll stick to predefined shapes if I possibly can! :) Thanks again – Ben A Apr 26 '17 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.