0

I'm trying to learn how to draw graphs in TikZ, but the simple examples I find on tutorials online do not help with the graph I'm trying to create.

Basically, I want to have four types of nodes: empty ones, nodes with a cross, nodes crossed out with a single line, and nodes with a point inside. On top of that, I want labels next to the nodes, to indicate their names.

I attach a small example of what I mean by these types of nodes.

enter image description here

This is what I currently have, using TikZiT for help.

\begin{tikzpicture}
    \begin{pgfonlayer}{nodelayer}
        \node [style=new style 0] (0) at (-2, 2) {};
        \node [style=new style 0] (1) at (-2, 1) {};
        \node [style=new style 0] (2) at (-2, 0) {};
        \node [style=new style 0] (3) at (-2, -1) {};
        \node [style=new style 0] (4) at (-4, 0.5) {};
        \node [style=new style 0] (5) at (0, 1.5) {};
        \node [style=new style 0] (6) at (0, 0.5) {};
        \node [style=new style 0] (7) at (0, -0.5) {};
    \end{pgfonlayer}
    \begin{pgfonlayer}{edgelayer}
        \draw (0) to (4);
        \draw (1) to (4);
        \draw (2) to (4);
        \draw (3) to (4);
        \draw (0) to (5);
        \draw (2) to (6);
        \draw (1) to (7);
        \draw (5) to (3);
    \end{pgfonlayer}
\end{tikzpicture}
4
  • 3
    A suggestion: post the code with the graph with all the nodes, say, circular, and ask people how to modify it to change the node shapes. That way it will not risk being confused with a "do it for me" question...
    – Rmano
    Jun 15, 2022 at 9:16
  • I thought my code would make it messier, bu it's a good point :) There it goes. Jun 15, 2022 at 9:32
  • Hmmm... the snippet need some library to compile. Please post a complete minimal example, loading (only) the needed libraries-
    – Rmano
    Jun 15, 2022 at 10:04
  • See why an minimal working example (MWE) is needed at tex.meta.stackexchange.com/questions/6255/…
    – Rmano
    Jun 15, 2022 at 10:40

1 Answer 1

2

You can define macros to draw the crosses in the nodes. For example,

\newcommand{\snode}[1]{\draw(#1.45)--(#1.225);}

Then if you have a circular node (node1), you can call \snode{node1} inside a tikzpicture and the macro will draw a line from angle 45 to angle 225.

You can change the size of the dot in \dnode (currently .5mm), and the size of the circles (currently 4mm).

Also, it is not necessary to use multiple \draw commands since the lines are all the same style.

enter image description here

\documentclass{article}

\usepackage{tikz}

\newcommand{\snode}[1]{\draw(#1.45)--(#1.225);}
\newcommand{\xnode}[1]{\draw(#1.45)--(#1.225)(#1.135)--(#1.315);}
\newcommand{\dnode}[1]{\fill (#1) circle[radius=.5mm];}

\begin{document}

\begin{tikzpicture}[every node/.style={draw, circle, minimum size=4mm}]
\node (0) at (-2, 2) {};
\node (1) at (-2, 1) {};
\node (2) at (-2, 0) {};
\node (3) at (-2, -1) {};
\node (4) at (-4, 0.5) {};
\node (5) at (0, 1.5) {};
\node (6) at (0, 0.5) {};
\node (7) at (0, -0.5) {};
\draw (0) to (4) (1) to (4) (2) to (4) (3) to (4) (0) to (6) (1) to (6) (2) to (5) (3) to (6) (3) to (7);
\dnode{4}\snode{1}\snode{2}\xnode{5}\xnode{6}\xnode{7}
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .