0

I am trying to draw a directed graph but am having trouble getting it to work. Please help. I have tried the "->" and also "Direct" inputs but neither seem to work.

\documentclass[12pt]{article} \usepackage[margin=1in]{geometry} \usepackage{amsmath,amsthm,amssymb} \usepackage{tikz-network} \usepackage{tikz} \usepackage{array} \usepackage{tkz-graph} \usepackage{graphicx} \usetikzlibrary{graphs, graphdrawing} \begin{document} \begin{tikzpicture} \Vertex[x=-6,y=1]{x} \Vertex[x=8,y=1]{y} \Vertex[x=4,y=2]{0001} \Vertex[x=-2,y=2]{1001} \Vertex[x=-2,y=-2]{0101} \Vertex[x=4,y=-2]{1101} \Vertex[x=-2,y=-4]{0011} \Vertex[x=4,y=-4]{1011} \Vertex[x=4,y=-6]{0111} \Vertex[x=-2,y=-6]{1111} \Vertex[x=-2,y=8]{0000} \Vertex[x=4,y=8]{1000} \Vertex[x=4,y=4]{0010} \Vertex[x=-2,y=4]{1010} \Vertex[x=4,y=6]{0100} \Vertex[x=-2,y=6]{1100} \Vertex[x=-2,y=0]{0110} \Vertex[x=4,y=0]{1110} \Edge[](0001)(1001) \Edge[](0001)(0011) \Edge[color=red](0001)(0101) \Edge[](0101)(1101) \Edge[](0101)(0111) \Edge[](1011)(1111) \Edge[color=red](1011)(1001) \Edge[](1011)(0011) \Edge[color=red](1111)(0111) \Edge[](1111)(1101) \Edge[](0111)(0011) \Edge[](1101)(1001) \Edge[](0000)(1000) \Edge[color=red](0000)(0010) \Edge[](0000)(0100) \Edge[color=red](0100)(1100) \Edge[](0100)(0110) \Edge[color=red](1010)(1110) \Edge[](1010)(1000) \Edge[](1010)(0010) \Edge[](1110)(0110) \Edge[](1110)(1100) \Edge[](0110)(0010) \Edge[](1100)(1000) \Edge[](0000)(0001) \Edge[](1000)(1001) \Edge[](0010)(0011) \Edge[](1010)(1011) \Edge[](0100)(0101) \Edge[](1100)(1101) \Edge[](0110)(0111) \Edge[](1110)(1111) \Edge[color=red,->](x)(0000) \Edge[color=red,Direct](x)(1100) \Edge[color=red](x)(1010) \Edge[color=red](x)(1001) \Edge[](x)(0110) \Edge[color=red](x)(0101) \Edge[](x)(0011) \Edge[color=red](x)(1111) \Edge[](y)(1000) \Edge[color=red](y)(0100) \Edge[color=red](y)(0010) \Edge[color=red](y)(0001) \Edge[color=red](y)(1110) \Edge[](y)(1101) \Edge[color=red](y)(1011) \Edge[color=red](y)(0111) \end{tikzpicture} \end{document} 
3
  • 2
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Oct 8, 2024 at 22:19
  • For the edge \Edge[color=red,->](x)(0000) I am unable to give the edge direction by adding an arrow on the edge Commented Oct 8, 2024 at 23:18
  • 1
    So, tikz-network and tikz-graph define lots of stuff with the same names. If you comment out tikz-network, most of the problems go away. Then, the Direct and -> are undefined, so you need to remove those. If you want to add arrows, you might need to use lower level commands. Regardless, the naming conflicts from tikz-network and tikz-graph are the primary concern. Commented Oct 9, 2024 at 4:29

1 Answer 1

4

As the comments already mentioned, you are loading packages that conflict with each other.

As an alternative, here's a plain TikZ solution with a custom node matrix construct (stripped down version of previously used implementations) where you don't have to place all vertices manually.

Connecting the vertices with edges is aided by the graph library.

Code

\documentclass[tikz]{standalone} \usetikzlibrary{arrows.meta, chains, graphs} \ExplSyntaxOn \makeatletter \tl_replace_once:Nnn \tikz@do@matrix@cont { \tikz@node@finish } { \tikz@alias \tikz@node@finish } \makeatother \ExplSyntaxOff \tikzset{ node matrix/.style={ every outer matrix/.append style={/pgf/inner sep=+0pt, /pgf/outer sep=+0pt, draw=none, fill=none, shape=rectangle}, nodes/.style={node matrix/node/.append style={##1}}}, node matrix/node/.style={anchor=center, node contents={#1}, name={\tikzmatrixname_#1}}, node matrix/place 1st node/.code args={#1,#2}{\node[node matrix/node={#1}];}, node matrix/place other nodes/.style args={#1,#2}{/tikz/node matrix/place oth node/.list={#2}}, node matrix/place oth node/.code={\pgfmatrixendrow\node[node matrix/node={#1}];}} \newcommand*\tikzMatrixNodes[2][1]{% \matrix[every node matrix/.try,node matrix,#1,new set=\csname tikz@fig@name\endcsname, nodes/.expanded={set=\csname tikz@fig@name\endcsname}]{ \tikzset{node matrix/place 1st node={#2},node matrix/place other nodes={#2}}\\};} \begin{document} \begin{tikzpicture}[ >={Latex[round]}, start chain=going right, node distance=3cm, on grid, row sep={1.5cm, between origins}, vertex/.style={ shape=circle, text width=width("0000"), text depth=+0pt, align=center, draw}] \node [on chain, vertex] (x) {$x$}; \tikzMatrixNodes[on chain, nodes=vertex, name=m1] {0000, 1100, 1010, 1001, 0110, 0101, 0011, 1111} \tikzMatrixNodes[on chain, nodes=vertex, name=m2] {1000, 0100, 0010, 0001, 1110, 1101, 1011, 0111} \node [on chain, vertex] (y) {$y$}; \newcommand*\A[4]{\ifcase#1 1\else 0\fi#2#3#4} \newcommand*\B[4]{#1\ifcase#2 1\else 0\fi#3#4} \newcommand*\C[4]{#1#2\ifcase#3 1\else 0\fi#4} \newcommand*\D[4]{#1#2#3\ifcase#4 1\else 0\fi} \graph[use existing nodes, simple]{% simple: only one edge between two nodes x -- (m1), (m2) -- y, % connect x with all nodes of set m1, { % connect all nodes of set m2 with y \foreach \v/\w in {0000, 1100, 1010, 1001, 0110, 0101, 0011, 1111}{ m1_\v -- {% foreach node of m1 connect it with one of its four partners \foreach \i in {\A, \B, \C, \D} { m2_\expandafter\i\v } } } }, {[edges=red] \foreach \a/\b in {0000/0010, 1100/0100, 1010/1110, 1001/1011, 0101/0001, 1111/0111}{ % connect x with each given node of m1 and m2 and y % -> denotes a directed edge x -> m1_\a -> m2_\b -> y } } }; \end{tikzpicture} \end{document} 

Output

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.