TransWikia.com

Tikz Node Style: half square, half ellipse

TeX - LaTeX Asked by CLRW97 on June 21, 2021

I was just wondering if there is a way to change the border style of a node so that one half is, for example, a rectangle and the other an ellipse or cloud or something. The image below visualizes this:

node (left half: rectangle, right half: ellipse

Does someone out there has an idea how to achieve this?

BACKGROUND:
I want to use the borderstyle to define the element described by a node, e.g. a rectangle node denotes an element of type X, a node with an ellipse border denotes an element of type Y, and an element that has both types X and Y should be half rectangle, half ellipse. I know how to draw rectangles, ellipses, clouds, etc. but how can I merge them into one style?

One Answer

I'll give you my two cents, just in case.

1. With shapes.misc library

    begin{tikzpicture}
            
        node[
            draw,
            rounded corners=3pt,
            minimum width=3cm,
            minimum height=2cm,
            rounded rectangle,
            rounded rectangle left arc=none,
            font=sffamilyLarge] {Hello};

    end{tikzpicture}

Fancy node 1

2. With a pic and manually inserted anchors

    begin{tikzpicture}

        tikzset%
            {
            pin/.style={Circle[]-,red},
            rectell/.pic={              
                draw (0,0.5*3) coordinate(-north) -| (-0.5*5,0) coordinate(-west) |- (0,-0.5*3) coordinate(-south) arc(-90:0:0.5*5 cm and 0.5*3 cm) coordinate(-east) arc(0:90:0.5*5 cm and 0.5*3 cm) -- cycle;
                node (-center) at (0,0) {#1}; 
                }
            }
        
        draw (0,0) pic(A){rectell={sffamilyHuge Hello}};
        
        draw[pin] (A-center.center) --++ (3,3) node[above] {A-center};
        draw[pin] (A-north) --++ (-1,1) node[above] {A-north};
        draw[pin] (A-west) --++ (-1,1) node[above] {A-west};
        draw[pin] (A-south) --++ (-1,-1) node[below] {A-south};
        draw[pin] (A-east) --++ (1,1) node[above] {A-east};
        
    end{tikzpicture}

Fancy node 2

Complete code with preamble:

documentclass{article}
usepackage{tikz}
usetikzlibrary{arrows.meta,shapes.misc}

begin{document}

    begin{tikzpicture}
            
        node[
            draw,
            rounded corners=3pt,
            minimum width=3cm,
            minimum height=2cm,
            rounded rectangle,
            rounded rectangle left arc=none,
            font=sffamilyLarge] {Hello};

    end{tikzpicture}
    
    
    bigskip
    
    
    begin{tikzpicture}

        tikzset%
            {
            pin/.style={Circle[]-,red},
            rectell/.pic={              
                draw (0,0.5*3) coordinate(-north) -| (-0.5*5,0) coordinate(-west) |- (0,-0.5*3) coordinate(-south) arc(-90:0:0.5*5 cm and 0.5*3 cm) coordinate(-east) arc(0:90:0.5*5 cm and 0.5*3 cm) -- cycle;
                node (-center) at (0,0) {#1}; 
                }
            }
        
        draw (0,0) pic(A){rectell={sffamilyHuge Hello}};
        
        draw[pin] (A-center.center) --++ (3,3) node[above] {A-center};
        draw[pin] (A-north) --++ (-1,1) node[above] {A-north};
        draw[pin] (A-west) --++ (-1,1) node[above] {A-west};
        draw[pin] (A-south) --++ (-1,-1) node[below] {A-south};
        draw[pin] (A-east) --++ (1,1) node[above] {A-east};
        
    end{tikzpicture}
end{document}

EDIT: A PARAMETRIC PIC USED AS A NODE

If you want to draw a pic node and choose width and height, you can pass those parameters as arguments to the pic whan you call it.

documentclass{article}
usepackage{tikz}
usetikzlibrary{arrows.meta}

begin{document}    
    begin{tikzpicture}

        tikzset%
            {
            pin/.style={Circle[]-,red},
            %
            pics/rectell/.style args={#1/#2/#3}{code = {                
                draw (0,0.5*#3) coordinate(-north) -| (-0.5*#2,0) coordinate(-west) |- (0,-0.5*#3) coordinate(-south) arc(-90:0:0.5*#2 cm and 0.5*#3 cm) coordinate(-east) arc(0:90:0.5*#2 cm and 0.5*#3 cm) -- cycle;
                coordinate (-center) at (0,0) node {#1}; 
                }}
            }
        
        draw (0,0) pic(A){rectell={sffamilyHuge Width 7 Height 3}/7/3};
        
        draw[pin] (A-center) --++ (3,2) node[above] {A-center};
        draw[pin] (A-north) --++ (-1,1) node[above] {A-north};
        draw[pin] (A-west) --++ (-1,1) node[above] {A-west};
        draw[pin] (A-south) --++ (-1,-1) node[below] {A-south};
        draw[pin] (A-east) --++ (1,1) node[above] {A-east};
        
        draw (0,-6) pic{rectell={sffamilyHuge W 4 H 5}/4/5};
    end{tikzpicture}
end{document}

Args for pic

Correct answer by SebGlav on June 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP