3

I have a rounded rectangle node, and I want to draw a horizontal line up to it. I can use things like (A.210) to (A.210 -| -2, 0), but I want to be able to specify the y-coordinate explicitly, and I'm struggling to see a way to do that (short of doing trigonometry by hand to figure out the right angle anchor).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}

\begin{document}
\begin{tikzpicture}
  \node (A) [draw, rounded rectangle, minimum height=4em] {};
  \draw (-2, -1em) to (A); % I want this line to be horizontal.
\end{tikzpicture}
\end{document}

Output:

6

Try this:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \draw[line width=0.5pt,color=red] (-2,0-1em) -- (0,0-1em);
    \draw[line width=0.5pt] (0,0) circle (2em);
    \fill[color=white] (0,0) circle (2em);
\end{tikzpicture}
\end{document}

enter image description here

You can change the line width and the color to whatever you want. Note that color in \fill must be set as your paper background color to hide the unnecessary part of the red line.

  • 1
    I need a node[rounded rectangle] (contents were cut for the MWE), but this is close enough to get me on the right lines of drawing the shape over the line. – dutchie Nov 5 '18 at 14:33
  • 2
    @dutchie, try this: \draw (-2, -1em) to (0,-1em);\node (A) [draw,fill=white, rounded rectangle, minimum height=4em] {}; – Sigur Nov 5 '18 at 14:33
3

You could also use intersections for that (in case you put the A node on top of some background, which prevents you from filling it white).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.misc,intersections}

\begin{document}
\begin{tikzpicture}
  \node (A) [draw, rounded rectangle, minimum height=4em,name path=A] {};
  \path[name path=B] (-2, -1em) coordinate (aux) -- (0,-1em);
  \draw[red,name intersections={of=A and B}] (aux) -- (intersection-1);
\end{tikzpicture}
\end{document}

enter image description here

  • This looks like the "right" way to do it, awesome! I'll have to do some more reading about the intersections library. – dutchie Nov 5 '18 at 14:35

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.