4

I am trying to define a style to draw a bend line from one shape's south to another shape's west

\documentclass[border=5mm, convert, usenames, dvipsnames]{standalone}
\usepackage{tikz, pifont, xcolor}
\usetikzlibrary{shapes, positioning, calc, arrows.meta, matrix, chains, scopes, fit}
\usepackage{lmodern}
\usepackage{underscore}

\begin{document}
\begin{tikzpicture}[auto]
    \tikzset{downbendjoin/.style={to path = {(\tikztostart.south) -- ($(\tikztostart.south)!0.5!(\tikztostart.south|-\tikztotarget.north)$) -| ([xshift=-1cm]\tikztotarget.west) -- (\tikztotarget)}, rounded corners}}
    \node [draw=black, rectangle, minimum width=4cm, minimum height=3cm] (a) {A};
    \node [draw=black, rectangle, minimum width=4cm, minimum height=3cm, below left = of a] (b) {B};
    \draw [downbendjoin] (a) to (b);
\end{tikzpicture}
\end{document}

It works correctly as I expected. enter image description here

The next thing I want to do is add a parameter to the downbendjoin, as the following

\tikzset{downbendjoin/.style n args ={1}{to path = {(\tikztostart.south) -- ($(\tikztostart.south)!#1!(\tikztostart.south|-\tikztotarget.north)$) -| ([xshift=-1cm]\tikztotarget.west) -- (\tikztotarget)}, rounded corners}}
\tikzset{downbendjoin/.default={0.5}}

The purpose is to control the bending point in the path, and I set the default position percentage as 0.5. But this don't work as expected, the generated picture is as the following, the percentage parameter don't work at all. enter image description here

Did some investigation but don't find the root cause. Can anyone point out what's wrong with my parameter settings?

  • 3
    If your key has only 1 argument then instead of /.style n args = {1}{...} you can use /.style directly which has a single argument by default. – percusse Jul 29 '18 at 9:29
  • 2
    see if @Qrrbrbirlbel answer on question tex.stackexchange.com/questions/45347/… can help you. – Zarko Jul 29 '18 at 10:07
  • 1
    The problem is the .style n args ={1}, it doesn't work as expected and isn't needed anyway. Use as @percusse suggested simply /.style={...} – Ulrike Fischer Jul 29 '18 at 13:07
  • @percusse, and all, thank you for point it out. I originally think n args = {...} is a generic form to add parameters to style. After remove it it now works. Fell that the tikz syntax is quite not uniformed. Need to learn them case by case. – Eric Sun Jul 29 '18 at 14:12
2

You are loading the calc library (and many many more;-), so you could simply use it...

\documentclass[border=3.14mm,tikz]{standalone}
\usetikzlibrary{positioning,calc}
\begin{document}
\tikzset{downbendjoin/.style={to path = {
    let
\p1=(\tikztostart.south),\p2=(\tikztotarget.north),\p3=(\tikztotarget.west) in
    (\tikztostart.south) -- ++(0,{#1*(\y2-\y1)}) -- ++ ({\x3-\x1-1cm},0)
    |-  (\tikztotarget)}, rounded corners}}
\foreach \X in {0.2,0.25,...,0.8}
{\begin{tikzpicture}[auto]
    \node [draw=black, rectangle, minimum width=4cm, minimum height=3cm] (a) {A};
    \node [draw=black, rectangle, minimum width=4cm, minimum height=3cm, below left = of a] (b) {B};
    \draw [downbendjoin=\X] (a) to (b);
\end{tikzpicture}}
\end{document}

enter image description here

  • yes i am using quite a lot library, I googled when I met problems and these libraries are accumulated and I never thought of clean it :). The let...in is a very interesting method that I never known. And the gif is amzing! Thank you. – Eric Sun Jul 29 '18 at 14:21

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.