2

I want to achieve the following:

enter image description here

But the connecting of nodes isn't working properly. I get the following output:

enter image description here

How do I fix this and where am I going wrong.

MWE:

\documentclass[tikz]{article}
\usepackage{tikz}
\tikzset{square/.style = {
    shape  = rectangle,
    fill   = gray!50,
    draw   = black,
    thick
}}

\tikzset{circle/.style = {
    shape  = circle,
    fill   = blue!20,
    draw   = blue,
    thick
}}

\begin{document}
\begin{tikzpicture}
  \draw[square] (-4,4)rectangle node (r1) {r1} (-3,5);
  \draw[circle] (-1,4.5) circle [radius=0.5cm] node (s1) {s1};
  \draw[->] (r1.west) -- (s1.east);

\end{tikzpicture}
\end{document} 

Please note that I would like to stick to the approach of \draw[->] (r1.west) -- (s1.east);

  • @JouleV thanks. In that case when to use (r1.west) -- (s1.east); and when to use (r1)--(s1) – subham soni Mar 3 at 14:41
4

Correct picture

\documentclass[tikz,margin=3mm]{standalone}
\tikzset{squarenode/.style = {
    shape  = rectangle,
    fill   = gray!50,
    draw   = black,
    thick,
    minimum size=1cm %%%% Take note of this!
},
    circlenode/.style = {
    shape  = circle,
    fill   = blue!20,
    draw   = blue,
    thick,
    minimum size=1cm %%%% and this!
}}

\begin{document}
\begin{tikzpicture}
  \draw (-4,4.5) node[squarenode] (r1) {r1}; % Or \node[squarenode] (r1) at (-4,4.5) {r1};
  \draw (-1,4.5) node[circlenode] (s1) {s1}; % Or \node[circlenode] (s1) at (-1,4.5) {s1};
  \draw[->] (r1) -- (s1); % or (r1.east)--(s1.west);

\end{tikzpicture}
\end{document} 

enter image description here

Some notes (important!)

  1. circle is a defined option, therefore you must not define a new circle. I changed it to circlenode.
  2. You don't have to draw the circle and the square manually. You can use the shape in the node and minimum size. For more controlling, we have minimum height and minimum width.
  3. I don't recommend using many \tikzset{}s.
  4. tikz is not an option of article. It is an option of standalone. When you load tikz option already, you don't need \usepackage{tikz}.

Difference when you use (r1)--(s1) and (r1.east)--(s1.west)

\documentclass[tikz,margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\node[draw] (a) at (0,0) {Some text};
\node[draw] (b) at (5,3) {Hello world};
\draw[thick] (0,0)--(5,3);
\draw[red] (a)--(b);
\draw[blue] (a.east)--(b.west);
\end{tikzpicture}
\end{document}

enter image description here

Look closely at the beginning point and the ending point of the red line and the blue line.

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.