7

I am interested in the line-to operation |- (or -| ) with rounded corner. Now, I would like to add a node on the rounded corner. But the node then appears not on the rounded corner -- it is rather on the `original corner'. How to force this node to be on the line?

Here is an MWE

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw (0,0)|-node{mid}(2,3);
\end{tikzpicture}
\begin{tikzpicture}
\draw [rounded corners=1cm] (0,0)|-node{mid}(2,3);
\end{tikzpicture}
\end{document}

This gives the output

enter image description here

I know it can be done with to and also with controls (though I couldn't get the desired result without a bit of tweaking)

I feel tweaking is not the right thing to do. It is like doing paint in tikz; if we are doing tweaking, why not draw on a WYSIWYG drawing software or geometry software and export?

So, my question is
Is there any way to put a node exactly on a rounded corner?
(preferably without using to or controls; but if you can do them in a rather neat way, you are more than welcome)

Oh, I almost forgot. Just being curious, is there a name for this |- or -| operation?

Thank you

  • 1
    there is a similar (unanswerd, unfortunately) question here: tex.stackexchange.com/questions/397589/… – Rmano Dec 20 '18 at 8:30
  • @Rmano, The problem you mentioned seems to show a different issue. Sure, rounded corner is messing with markings; but still markings are on the line. – Cyriac Antony Dec 20 '18 at 9:28
  • Yes, you can do it with markings... but finding the "center" position is still done by guessing. – Rmano Dec 20 '18 at 11:46
  • I update the answer... – Rmano Dec 20 '18 at 11:54
7

This depends a bit on what do you intend for "middle"; I would use intersections generally. In this example, I am using as "middle point" the intersection of the curved path and the rectangle formed by the two parts for the red dot, and with a 45 degree angle in the case of the blue one.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc, intersections}
\begin{document}
\begin{tikzpicture}
    \draw (0,0)|-node{mid}(2,3);
\end{tikzpicture}\quad
\begin{tikzpicture}
    \draw [rounded corners=1cm] (0,0)|-node{mid}(2,3);
\end{tikzpicture}\quad
\begin{tikzpicture}
    \coordinate (one) at (0,0);
    \coordinate (two) at (2,3);
    \draw [rounded corners=1cm, name path=A] (one)|-(two);
    % remove draw=red
    \path [draw=red, name path=B] (one -| two) -- (two -| one);
    \coordinate[name intersections={of=A and B, by=DOT}];
    \node [circle, red, fill] at (DOT){};
\end{tikzpicture}\quad
\begin{tikzpicture}
    \coordinate (one) at (0,0);
    \coordinate (two) at (2,3);
    \draw [rounded corners=1cm, name path=A] (one)|-(two);
    \coordinate (mid) at  (one |- two);
    % remove draw=blue
    \path [draw=blue, name path=B] (mid) -- ($(mid)+(1,-1)$);
    \coordinate[name intersections={of=A and B, by=DOT}];
    \node [circle, blue, fill] at (DOT){};
\end{tikzpicture}
\end{document}

Output of the snippet above

You cal also use a decoration, but in this case you have to guessestimate the pos parameter:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\tikzset{markpos/.style args={#1 at #2}{decoration={
  markings,
  mark=at position #2 with {\coordinate(#1);}},postaction={decorate}}}
\begin{tikzpicture}
    \draw (0,0)|-node{mid}(2,3);
\end{tikzpicture}\quad
\begin{tikzpicture}
    \draw [rounded corners=1cm] (0,0)|-node{mid}(2,3);
\end{tikzpicture}\quad
\begin{tikzpicture}
    \draw [rounded corners=1cm, markpos=mymark at 0.6] (0,0)|-node{mid}(2,3);
    \node [circle, red, fill] at (mymark){};
\end{tikzpicture}
\end{document}

Another one

  • I accepted this because it solves the problem. Thank you @Rmano. Still, i am curious as to whether there is some easier method. – Cyriac Antony Dec 20 '18 at 8:55
  • I mean, intersections seems to be a broadly useful technique. Yet, can we keep the code shorter (neatly)? – Cyriac Antony Dec 20 '18 at 8:57
  • 1
    Sure we can; creating macros and similar things as ever. I've been verbose on purpose... ;-) – Rmano Dec 20 '18 at 9:16
  • Wow, one parameter is fine I guess – Cyriac Antony Dec 20 '18 at 11:57
5

Here is a broad approach using node anchor that can certainly be improved:

\documentclass{article}
\usepackage{tikz}

    \begin{document}
    \begin{tikzpicture}
    \draw (0,0)|-node{mid}(2,3);
    \end{tikzpicture}
    \begin{tikzpicture}
    \draw [rounded corners=1cm] (0,0)|-node[below right]{mid}(2,3);
    \end{tikzpicture}
 \end{document}

enter image description here

  • Wouldn't this be tweaking? If the node was simply a dot (as in my original application), we will need to tweak the node distance. Am I missing something? – Cyriac Antony Dec 20 '18 at 6:57
  • @CyriacAntony yes in this solution the distance depend on the node content. I think it may be doable to automate the node position so it become independent of the content. – Hafid Boukhoulda Dec 20 '18 at 7:11
  • Automating node position independent of the content sounds good. Yet, we will have to change the node position if the rounding is more (or less), don't we? For instance: rounded corners=3cm – Cyriac Antony Dec 20 '18 at 7:16
  • @Cyriac yes exactly that's what I mean! I will think about it as soon as possible. I am in hurry now. – Hafid Boukhoulda Dec 20 '18 at 7:31
2

You can use xshift and yshift to place the node properly. The value of these shifts can be obtained with simple geometry calculation.

  \draw [rounded corners=\rndc] (0,0)|-node[xshift=0.293*\rndc),yshift=-0.293*\rndc)]{mid}(2,3);

The number 0.293 is 1-1/sqrt(2).

enter image description here

The complete code

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw (0,0)|-node{mid}(2,3);
\end{tikzpicture}
\begin{tikzpicture}
  \def\rndc{1cm}
  \draw [rounded corners=\rndc] (0,0)|-node[xshift=0.293*\rndc),yshift=-0.293*\rndc)]{mid}(2,3);
\end{tikzpicture}
\end{document}

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.