6

In TikZ, I can draw a node on the border of some other node, by specifying the angle, using the "name.angle" syntax. For example, the following code:

\begin{tikzpicture}
\node[draw,circle] (A) at (0,0) {foo bar};
\node[draw,inner sep=1mm,fill=red] (B) at (A.30) {};
\end{tikzpicture}

produces this picture, where the red node is 30° at the intersection of A's border and a line, heading towards 30°, starting at the center of A, i.e. at 2 o'clock:

the picture. a red square at 2 o'clock around a node

Is there a way I can get a similar result, but where the red square is not on the border, but some distance away from it?

1
  • 1
    \path (A) + (30:1) node {red square};
    – Symbol 1
    Oct 27 at 4:04
5

By use of the calc library:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}
        \node[draw,circle] (A) at (0,0) {foo bar};
        \node[draw,inner sep=1mm,fill=red] (B) at ($(A.30)+(30:5mm)$) {};
    \end{tikzpicture}
\end{document}

enter image description here

2
  • +1: Is the 30 needed in (A.30)? Oct 27 at 1:39
  • 1
    No, just replace 5mm by a larger amount at the same time.
    – Symbol 1
    Oct 27 at 4:04
4

You mean like with yshift and xshift:

\documentclass{article}

\usepackage{tikz}

\begin{document}
    
    \begin{tikzpicture}
        \node[draw,circle] (A) at (0,0) {foo bar};
        \node[draw,inner sep=1mm,fill=red,yshift=5pt,xshift=5pt] (B) at (A.30) {};
    \end{tikzpicture}
\end{document}

enter image description here

1
  • Thanks, but unfortunately this approach easily shifts the dot off the 30° axis. You would have to carefully compute the shifting. Oct 28 at 0:02
2

You can use the pos key to put the node at some factor times the radius away from the center. In this example the center of the square is at twice the radius (pos=2).

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
 \node[draw,circle] (A) at (0,0) {foo bar};
 \path (A.center) -- node[draw,inner sep=1mm,fill=red,pos=2] (B) {} (A.30);
\end{tikzpicture}
\end{document}

enter image description here

2

A label is already a node, then why not use labels to draw these nodes?

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\node[draw,circle, 
    label={[draw, inner sep=1mm, fill=red, shift={(30:3mm)}, anchor=center]30:}, 
    label={[draw, circle, inner sep=1mm, fill=blue, shift={(120:5mm)}, anchor=center]120:}, 
    ] (A) at (0,0) {foo bar};
\end{tikzpicture}
\end{document}

enter image description here

Update:

From OPs comment I understand he/she wants to place a new node relative to the previous one.

Two options:

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\node[draw,circle, 
    ] (A) at (0,0) {foo bar};
%This line aligns its `west` anchor with a point `(30:1cm)` far from circle center
\path (A.center) --++(30:1cm) node[right,draw] {This is a new line};
%This line aligns its `north west` anchor with a point `(0:5mm)` far from circle border
\node[below right, draw] at ([xshift={5mm}]A.-45) {This is a another line};

\end{tikzpicture}
\end{document}

enter image description here

2
  • Because I might want to start a line at that point. Oct 28 at 0:03
  • 1
    @JoJoModding I've updated the answer with what I assume you wants. Is my assumption correct?
    – Ignasi
    Oct 28 at 8:21
1
\documentclass[tikz, border=1 cm]{standalone}
\begin{document}
\begin{tikzpicture}
\node[draw,circle] (A) at (0,0) {foo bar};
\draw (A) +(30:2 cm) node[draw,inner sep=1mm,fill=red] {};
\end{tikzpicture}
\end{document}

Red square 2cm away from circle node

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.