0

I have the following piece of tikz code:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[>=latex]
\draw (2,2) circle [radius=2pt];
\draw (3,3) circle [radius=2pt];
\path[inner sep=0pt] (2,2) ++(45:2pt) node (A) {};
\path[inner sep=0pt] (3,3) ++(225:2pt) node (B) {};
\draw[-] (A) -- (B);
\end{tikzpicture}
\end{document}

where I want to connect two connect the circles with a line. Points A and B should be on the circles, but unfortunately they are slighty off. How can I fix the existing code?

2

A node has usually a contents. In this case, it is empty, but lines between nodes are more complicate, there is inner sep, outer sep etc., but the center point could be used:

\draw (A.center) -- (B.center);

There is an easier way. Simpler points can be defined by coordinate instead of node:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[>=latex]
  \draw (2,2) circle [radius=2pt];
  \draw (3,3) circle [radius=2pt];
  \path (2,2) ++(45:2pt) coordinate (A);
  \path (3,3) ++(225:2pt) coordinate (B);
  \draw[-] (A) -- (B);
\end{tikzpicture}
\end{document}

Result

Or, merged to one \draw statement:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \def\radius{2pt}
  \draw[radius=\radius]
    (2, 2) circle[] ++(45:\radius) coordinate (A)
    (3, 3) circle[] ++(225:\radius) coordinate (B)
    (A) -- (B)
  ;
\end{tikzpicture}
\end{document}
1

with defining your circle as node simple solve your problem:

enter image description here

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
krog/.append style={% krog = circle ;-)
    circle, draw, minimum size=4pt, inner sep=0pt, outer sep=0pt}
                        ]
\node (A) [krog] at (2,2) {};
\node (B) [krog] at (3,3) {};
%
\draw (A) -- (B);
\end{tikzpicture}
\end{document}
  • Thank you very much. This is also a very good answer, but unfortunately I can only accept one. – Zenon Apr 4 '18 at 12:23

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.