2

I'm creating a simple depiction of a Queue data structure, the code is very simple with just a matrix of math nodes and two extra nodes (one being enqueued and the other dequeued). When adding an edge from enqueued node to the queue representation, I get an arrow head on both ends, instead of only one. How can I solve this?

Here's an image of the problem:

problem

And here's a minimal example to reproduce the issue:

\documentclass[crop]{standalone}

\usepackage{tikz} \usetikzlibrary{matrix} \usetikzlibrary{positioning} \tikzstyle{box} = [rectangle, draw, text centered, minimum height=1cm, minimum width=1cm]

\begin{document} \begin{tikzpicture} \matrix (q) [matrix of math nodes, nodes={box, thick}] { 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \ }; \node[box, thick, left=25pt of q, rotate=15] (enq) {}; \node[box, thick, right=25pt of q, rotate=-15] (deq) {};

\draw[->, thick] (enq.east) edge [bend left] node [pos=0.5, above, sloped] { \tiny Enqueue } (q-1-1.west);

\draw[->, thick] (q-1-8.east) edge [bend left] node [pos=0.5, above, sloped] { \tiny Dequeue} (deq.west); \end{tikzpicture} \end{document}

PS: if I remove the explicit anchors from the edge definition, the extra arrow heads are removed, but the edge is placed a little too high.

  • 4
    Hi, welcome. Use to instead of edge. See for example https://tex.stackexchange.com/questions/169564/ https://tex.stackexchange.com/questions/15567/ https://tex.stackexchange.com/questions/82326/ – Torbjørn T. Nov 27 '22 at 15:15
  • Thank you, @TorbjørnT. The solution was much simpler than I thought :-) – Felipe Balbi Nov 27 '22 at 15:20

1 Answers1

1

You can define edge style, for example as every edge/.style = {draw, -stealth}, as well edge labels as every edge/.style = {draw, -stealth},˛. Considering this definitions and colors of nodes shows in image, the MWE can be:

\documentclass[crop]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,
                positioning,
                quotes}

\begin{document} \begin{tikzpicture}[ box/.style = {draw=#1, thick, fill=#1!30, minimum size=1cm, align=center}, every edge/.style = {draw, -stealth}, every edge quotes/.style = {auto, font=\tiny} ] \matrix (q) [matrix of math nodes, nodes = {box=orange, outer sep=0pt}, column sep = -\pgflinewidth, inner sep=0pt] { 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \ }; \node[box=red, left =12mm of q, rotate= 15] (enq) {}; \node[box=green, right=12mm of q, rotate=-15] (deq) {};

\draw (enq.east) edge[bend left, sloped, "Enqueue"] (q.west); \draw (q.east) edge[bend left, sloped, "Dequeue"] (deq.west); \end{tikzpicture} \end{document}

enter image description here

Zarko
  • 296,517