1

I am trying to draw a line between rectangles with rounded corners. However, I'd like the line to touch the corners. What happens is that the line ends as if the rectangle did not have rounded corners.

I remember I already encountered this problem, but I can not remember how I solved it (maybe I just removed the rounded corners, but this time I can not).

Does anyone have suggestion on how to remove the gap ?

N.B.: I can not draw a line to the center of the rectangle, then draw the rectangle above the line, as I will have custom endings for the line (not shown in the MWE).

N.B.2: I have to work with quite a lot of such rectangles, which are not always positioned like the MWE. I am looking for a general method, not for an ad-hoc one.

Resulting picture with a gap between the line and the corners of the rectangles.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}

\usetikzlibrary{
    positioning
}

\begin{document}

\begin{tikzpicture}
    \node [draw, rectangle, rounded corners] (A) {A};
    \node [draw, rectangle, rounded corners] (B) [below left = 0.5cm and 0.25cm of A] {B};
    \draw (A) -- (B);
\end{tikzpicture}
\end{document}
1

The IMHO cleanest solution is to do what Ignasi suggests: define a new shape. Assuming you do not want to do that, you can compute the intersections between the node boundaries and a path connecting their centers. This MWE comes with a basic and a slightly more advanced way of accomplishing this. The more advanced version has a style that gives the boundary path of a node the name of the node, so you do not have to add all these name path=... by hand. It also comes with a macro (no style because we need two separate paths and \pgfextra can backfire; if you are interested in an unsafe version I can add it) that allows you to connect the nodes without gap.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}

\usetikzlibrary{intersections,positioning}

\begin{document}

\begin{tikzpicture}
    \path node [draw, rectangle, rounded corners,name path=A] (A) {A};
    \path node[draw, rectangle, rounded corners,name path=B,below left = 0.5cm and 0.25cm of A] (B)  {B};
    \path[name path=AB] (A.center) -- (B.center);
    \draw[name intersections={of=A and AB,by=iA},name intersections={of=B and
    AB,by=iB}] (iA) -- (iB);
\end{tikzpicture}
\makeatletter
\tikzset{auto name node path/.style={name path=\tikz@fig@name}}
\makeatother
\newcommand{\Connect}[3][draw]{
\path[name path=#2#3] (#2.center) -- (#3.center);
\path[#1,name intersections={of=#2 and #2#3,by=i#2},
name intersections={of=#3 and #2#3,by=i#3}] (i#2) -- (i#3);
}

\begin{tikzpicture}[nodes={auto name node path}]
    \path node [draw, rectangle, rounded corners] (A) {A};
    \path node[draw, rectangle, rounded corners,below left = 0.5cm and 0.25cm of A] (B)  {B};
    \Connect{A}{B}
\end{tikzpicture}

\end{document}

enter image description here

1
  • I finally choosed your option. Maybe @Ignasi proposition is better, but I'm not confident enough with pgf to understand the solution.
    – Bromind
    Sep 23 '19 at 9:36
1

enter image description here

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[
box/.style = {draw, rounded corners, outer sep=0pt},
shorten <>/.style = {shorten >=#1,shorten <=#1}
                    ]
    \node [box] (A) {A};
    \node [box, below left = 0.5cm and 0.25cm of A] (B) {B};
    \draw[shorten <>=-0.5pt] (A) -- (B);
\end{tikzpicture}
\end{document}

Line is drawn from corners of rectangle shape with not rounded corners. So you need to prolong connecting line accordingly. Seems to be for 0.5pt appropriate amount.

3
  • 1
    Thanks, but it won't work for other (relative) positions of A and B. I forgot to mention in the question that I have quite a lot of such rectangles to deal with.
    – Bromind
    Sep 19 '19 at 11:28
  • 3
    Bromind: A rectangle with rounded corners and well positioned anchors is defined in tex.stackexchange.com/a/32869/1952. I'm sure there is another answer with even more anchors, but I don't find it now.
    – Ignasi
    Sep 19 '19 at 11:51
  • @Ignasi I feel you should write an answer.
    – user194703
    Sep 19 '19 at 16:58

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.