1

The code below produces a node with a subtly curved right-hand edge, using rounded rectangle from the shapes.misc tikz library. Here's what it looks like:

enter image description here

The height of the node is correct, but it's a lot wider than I'd like it to be. I'd like it to be only as wide as it needs to be to contain the label, something more like this:

enter image description here

Ideally I'd like to be able to change the height of the node independently of the width, so that I could make the following two nodes the same width as each other (at least approximately), while remaining different heights.

enter image description here

It seems like rounded rectangles have a minimum aspect ratio that's determined by the value of rounded rectangle arc length, so that the more subtle you make the curve the wider the box becomes. Is there a way to override this behaviour?

Here is the MWE code, which produces the two nodes in the last image above:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.misc}

\begin{document}

\begin{tikzpicture}
 \node [
        draw,
        rounded rectangle,
        rounded rectangle left arc=none,
        minimum height = 3em,
        rounded rectangle arc length = 90,
    ] at (0,1)
    {$f$};
 \node [
        draw,
        rounded rectangle,
        rounded rectangle left arc=none,
        minimum height = 1.5em,
        rounded rectangle arc length = 90,
    ] at (0,0)
    {$g$};
\end{tikzpicture}

\end{document}
2
  • The cause of (minimal) width of your node is rounded rectangle arc length = 90. See result, if you set it for example to rounded rectangle arc length = 120 or simple remove it from definition of your node style. Of course, by this the shape of node is changed. This features seems to be hardcoded in shape style.
    – Zarko
    Commented Jul 26, 2021 at 13:18
  • This could probably be changed but you'll have to take a look at the TeX code and modify it.
    – SebGlav
    Commented Jul 26, 2021 at 13:21

1 Answer 1

3

This is a workaround, but I think it could be useful. What about drawing your own shape as a pic? It's not a node but it's simple and we can made it as customizable as we need.

In the following example I made such pic with two parameters: the text that it must contain and the angle for the arc. It could be much easier if we fix the angle (or if we exclude 0 and 180, that way we don't need the \ifnum sentences).

\documentclass[border=2mm]{standalone}
\usepackage    {tikz}
\usetikzlibrary{calc}
\tikzset
{%
  pics/rounded rectangle/.style 2 args={% text, angle (0 <= angle <= 180)
    code={%
    \node (O) at (0,0) {#1};
    \ifnum #2 = 0
      \path[pic actions] (O.north east) rectangle (O.south west);
    \else\ifnum #2 = 180
      \path[pic actions] let \p1=(O.north east),
                             \p2=(O.south west),
                             \n1={0.5*(\y1-\y2)} % radius
        in (O.north west) -- ($(O.north east)-(\n1,0)$) arc (0.5*#2:-0.5*#2:\n1) -| cycle;
    \else
      \path[pic actions] let \p1=(O.north east),
                             \p2=(O.south west),
                             \n1={0.5*(\y1-\y2)/sin(0.5*#2)}, % radius
                             \n2={0.5*(\y1-\y2)/tan(0.5*#2)}  % delta x in the arc
        in (O.north west) -- ($(O.north east)+(\n2-\n1,0)$) arc (0.5*#2:-0.5*#2:\n1) -| cycle;
    \fi\fi
  }},
}

\begin{document}
\begin{tikzpicture}
\pic [
        draw,
        minimum height = 3em,
     ] at (0,1) {rounded rectangle={$f$}{90}};
\pic [
        draw,
        minimum height = 1.5em,
     ] at (0,0) {rounded rectangle={$g$}{90}};
\pic [
        draw,
        minimum height = 2em,
        minimum width  = 5em
     ] at (0,-1) {rounded rectangle={$f$}{120}};
\pic [
       draw
     ] at (0,-2) {rounded rectangle={$ffffffffff$}{50}};
\end{tikzpicture}
\end{document}

With the above code we get:

enter image description here

3
  • Thank you for this answer, it's super helpful. One thing I don't understand: what sets the value of \y1 and \y2 in your code?
    – N. Virgo
    Commented Jul 31, 2021 at 1:46
  • @Nathaniel, the sentence let \p1=(O.north east) gets the coordinates of the point (O.north east) and saves them as (\x1,\y1). Similar, let \p2=... saves the coordinates of the other vertex in (\x2,\y2). Commented Jul 31, 2021 at 5:12
  • I see, thank you. So \p1 isn't an arbitrary name but special tikz syntax. (I found it in the manual after searching for the text "\p1", but I never would have guessed it was special syntax.)
    – N. Virgo
    Commented Jul 31, 2021 at 5:15

You must log in to answer this question.

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