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

5
  • 1
    there is a similar (unanswerd, unfortunately) question here: tex.stackexchange.com/questions/397589/…
    – Rmano
    Dec 20, 2018 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. Dec 20, 2018 at 9:28
  • Yes, you can do it with markings... but finding the "center" position is still done by guessing.
    – Rmano
    Dec 20, 2018 at 11:46
  • I update the answer...
    – Rmano
    Dec 20, 2018 at 11:54
  • I now understand that controls gives a nice output if we declare a coordinate beforehand. \path (0,0) |-\coordinate(midpt) (2,3); \draw (0,0) ..controls (midpt).. node{mid} (2,3); gives satisfactory output in most cases. Jan 12, 2020 at 11:28

3 Answers 3

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

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

4
  • 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? Dec 20, 2018 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. Dec 20, 2018 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 Dec 20, 2018 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. Dec 20, 2018 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}
1
  • The bracket ) after \rndc) seems wrong, but it makes surprisingly no error. But it could be removed.
    – dexteritas
    Jan 22, 2023 at 11:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .