3

Fairly simple problem. In the following MWE, I am trying to draw 2 nodes in a graph, with edges between each of the nodes, and one entering the first labeled node, and one leaving the final labeled node. To generate the entering and leaving edge, I create 2 empty elements in the matrix (one at each end). While it all works, if you look at the following image,

enter image description here

the first edge entering node (1), the edge connecting (1) and (2), and the final edge leaving node (2), are NOT aligned. It looks (to me, and a few others I asked) that the edge entering/leaving the graph are slightly 'sloped'.

Bug? Or am I missing something... (perhaps there is a simpler way to do what I'm trying to do).

 \documentclass{article}

 \usepackage{tikz}
   \usetikzlibrary{decorations,shapes,arrows,matrix,positioning,fit}

 \begin{document}

 \begin{figure}[h]
 \centering
 \begin{tikzpicture}[>=stealth,->,shorten >=1.5pt,transform canvas={scale=1.05},line 
 width=1.0pt]
 \matrix (a) [matrix of nodes,row sep=0.5em, column sep=7em, every node/.style= 
 {circle,draw,font=\small\sffamily,minimum width=1.25cm},
 n.node/.style={font=\bf\small\sffamily},
 empty.node/.style={draw=none,minimum width=1.5cm}]
 { |[empty.node]| & [-2em]|[n.node]|$N_1$  & |[n.node]|$N_2$ & [-2em]|[empty.node]|\\};]
 \path[>=stealth,font=\small] (a-1-2) edge  (a-1-3);
 \path[>=stealth,->,font=\small] (a-1-1) edge (a-1-2) ;
 \path[>=stealth,->,font=\small] (a-1-3) edge (a-1-4) ;
 \end{tikzpicture}
 \end{figure}

 \end{document}

2 Answers 2

3

No bug I think, you've missed this:

enter image description here

If you add anchor=center to the every node style the alignment is correct.

1
  • Documentation? We don't need no stinking documentation. ;-) Seriously, many thanks! Nov 1, 2022 at 12:33
1

As @Torbjørn T. indicated, anchor=center fixes things. For comparison, here is the same image using tikz-cd instead of matrix, which has somewhat simpler code.

enter image description here

\documentclass{article}
\usepackage{tikz-cd}

\begin{document}
 
\begin{tikzcd}[cells={nodes={draw, line width=1pt, circle, minimum size=1.25cm, anchor=center}}, 
    column sep=7em, arrows={line width=1pt, >=stealth,->,shorten >=1.5pt}]
|[draw=none]|\arrow[r] &[-2em] N_1\arrow[r] & N_2\arrow[r] &[-2em] |[draw=none]|
\end{tikzcd}
 
\end{document}
1
  • Indeed -- I thought about tikz-cd, but I have so much legacy 'investment' in the tikzpicture-matrix approach that at this point, I'm loathe to introduce a new approach. But, by the end of the academic year, I might have a long(er) look at tikz-cd. Thanks! Nov 1, 2022 at 12:34

You must log in to answer this question.

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