10

Can anyone help making this shape but you can change the length, width of each sections. (and I want to be able to fill, opacity and change the length, width of it).

This is just my rough drawing.

enter image description here

I attempted by combining three sections like this but it doesn't work well. The overlap region doesn't work with opacity.

enter image description here

\documentclass[]{standalone} 
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\draw[fill=red,draw=none, rounded corners, line cap=round, opacity=0.3] (0,0) rectangle (1cm,4cm);

\draw[fill=red,draw=none, rounded corners,  line cap=round, opacity=0.3] (0,0) rectangle (4cm,1cm);

\draw[fill=red,draw=none, rounded corners, line cap=round, opacity=0.3] (3cm,0) rectangle (4cm,4cm);

\end{tikzpicture}
\end{document}
6
  • Should this be a shape as in a node of that shape or do you just want an easy way to form a path in that U shap? What are its dimensions, how is it defined? Dec 14, 2022 at 21:18
  • @Qrrbrbirlbel shape as in node. I tried node but only do rectangle.
    – hana
    Dec 14, 2022 at 21:20
  • If you just want to use it like this, not as a node, you can create a transparency group: \begin{scope}[opacity=.3, transparency group, rounded corners] \fill[red] (0,0) rectangle (1,4); \fill[red] (0,0) rectangle (4,1); \fill[red] (3,0) rectangle (4,4); \end{scope}. Dec 14, 2022 at 22:37
  • 1
    @hana: Can you provide a more descriptive title rather than using "How to draw this shape?"
    – Werner
    Dec 15, 2022 at 17:12
  • @Werner do you have any suggestion for the title? I don't know how to say it.
    – hana
    Dec 16, 2022 at 10:54

3 Answers 3

22

This approach does not create a node, but may still be of some help on the path to the final solution. Make use of a transparency group:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\begin{scope}[opacity=.3, transparency group, rounded corners] 
    \fill[red] (0,0) rectangle (1,4); 
    \fill[red] (0,0) rectangle (4,1); 
    \fill[red] (3,0) rectangle (4,4); 
\end{scope}

\end{tikzpicture}
\end{document}

enter image description here


You can also use it as a pic:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\tikzset{
    u shape/.pic={
        \begin{scope}[transparency group, rounded corners] 
            \path[#1]
                (0,0) rectangle (1,4)
                (0,0) rectangle (4,1)
                (3,0) rectangle (4,4); 
        \end{scope}
    }
}

\begin{document}
\begin{tikzpicture}

\pic[opacity=0.3] at (0,0) {u shape={fill=green}};

\pic[rotate=30, opacity=0.25] at (3,2) {u shape={fill=red}};

\end{tikzpicture}
\end{document}

enter image description here


Since you want to be able to change height and width:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\tikzset{
    pics/u shape/.style={
        code={
            \tikzset{u shape/.cd, #1}
            \path[rounded corners, pic actions]
                (0,0) -- ++(0,{\pgfkeysvalueof{/tikz/u shape/height}}) 
                    -- ++(1,0)
                [sharp corners]
                    -- ++(0,{1-\pgfkeysvalueof{/tikz/u shape/height}})
                    -- ++({\pgfkeysvalueof{/tikz/u shape/width}-2},0)
                [rounded corners]
                    -- ++(0,{\pgfkeysvalueof{/tikz/u shape/height}-1})
                    -- ++(1,0)
                    -- ++(0,{-\pgfkeysvalueof{/tikz/u shape/height}})
                    -- ++({-\pgfkeysvalueof{/tikz/u shape/width}},0)
                    -- cycle; 
        }
    },
    u shape/.cd,
    width/.initial=3,
    height/.initial=2,
}

\begin{document}
\begin{tikzpicture}

    \pic[fill=red] at (0,0) {u shape};
    
    \pic[fill=green, rotate=30, opacity=0.5] at (0,-2) {u shape={width=5, height=3}};

\end{tikzpicture}
\end{document}

enter image description here

1
  • 1
    Thanks for the nice figues.
    – hana
    Dec 15, 2022 at 12:30
12

I'm not sure I've fully understood what you really need, but here's a start. However, it is not a node shape. That's a bit more work to get it right (it involves PGF), but just with plain TikZ and a few tricky internals and the fit library, we can create a rectangular node with the given text and then use that node in reference and draw that U-shape around it.

For this, the Unode = {<width>}{<path usage>} key is used where <width> specifies the width of that U and <path usage> specifies the usage of that path (draw, fill, pattern, opacity, …).

You can use the every Unode style for common styles between many of these shapes.

With the fit library, another rectangular node will be placed so you can use it as normal, with the line stopping outside the whole shape.

With a selective sharp corners the rounded corners can be switched off for part of a path. I'm also using the <width> argument to set the rounded corners' radius to be always on the safe side, but you can, of course, set it to any other value.


You can't use the outer shape as a reference to place the whole shape. If you use anchor at the node, the anchor of the main/inner node will be used to place the whole construct.

This could have also been implemented with a pic with a slightly different syntax … but I thought of that too late.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{fit}
\tikzset{
  Unode/.style 2 args={
    shape=rectangle, path only,
    append after command={
      \pgfextra{%
        \pgfmathanglebetweenpoints{\pgfpointanchor{\tikzlastnode}{west}}
                                  {\pgfpointanchor{\tikzlastnode}{east}}%
        \let\unodeFitRotate\pgfmathresult
        \pgfsettransform{\csname pgf@sh@nt@\tikzlastnode\endcsname}}
      (\tikzlastnode.north west)
        edge[every Unode,#2,rounded corners={(#1)/3}, to path={
            --++ (left:{#1}) |- ([yshift={-(#1)}]\tikzlastnode.south west)
                             -| ([xshift={#1}]\tikzlastnode.north east)
                             -- (\tikzlastnode.north east) {[sharp corners]
                             |- (\tikzlastnode.south west)} -- cycle}] ()
      node [path only, inner sep=+0pt, rotate fit=\unodeFitRotate,
        fit={([xshift={-(#1)}]\tikzlastnode.north west)
             ([shift={({#1},{-(#1)})}]\tikzlastnode.south east)}]
        (\tikzlastnode-f) {}}},
  every Unode/.style={draw=none, fill, fill opacity=.5}}
\begin{document}

\tikz\node[Unode={.3cm}{red}]{hana};

\begin{tikzpicture}[node font=\Huge]
\draw[help lines] (-1.5,-1.5) grid (6.5,2.75);
\node[Unode={.5cm}{green!50}]                                    (foo) {Foo};
\node[rotate=45, anchor=west,
      Unode={.7cm}{left color=red, right color=orange}] at (4,1) (bar) {Bar};
\draw[thick] (foo-f) -- (bar-f);
\path[font=\small, every pin edge/.style={thick, blue, <-}]
      (0,0) coordinate[pin={$(0,0)$}] (@)
      (4,1) coordinate[pin={$(4,1)$}] (@);
\end{tikzpicture}
\end{document}

Output

enter image description here enter image description here

1
  • That looks great, especially Foo and Bar. Probably I didn't understand the difference between node and path. I want to use it as background for something.
    – hana
    Dec 15, 2022 at 12:30
4

Just to add the simplest solution, You can just use \draw with line width set to 1cm (or whatever).

enter image description here

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw[red, opacity=.3, line width=1cm, rounded corners](0,3)--(0,0)--(4,0)--(4,3);
\end{tikzpicture}

\end{document}
6
  • 1
    You could add line cap=round. Dec 15, 2022 at 18:06
  • @JasperHabicht: I considered it, but that creates a semicircle cap. Do you know a simple way to get rounded corners there?
    – Sandy G
    Dec 15, 2022 at 19:28
  • Yes, this is true. Maybe using a (custom) arrow tip? arrows.meta has Rectangle[round] ... but I don't know how customizable this is. Dec 15, 2022 at 19:58
  • 1
    You could use {Square[round, open, length=1cm]}-{Square[round, open, length=1cm]} but since the arrow tip overlaps with the line, you would still need a transparency group. Dec 15, 2022 at 20:14
  • @JasperHabicht: Right. I had the same thought. But then your solution would be better. Well, your solution is probably better anyway. :-)
    – Sandy G
    Dec 16, 2022 at 0:44

You must log in to answer this question.

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