[Tex/LaTex] How to set the radius of rounded corners relatively to default unit

lengthstikz-pgftikz-stylesunits

Background: When I draw a tikzpicture, I use only the default units (i.e. I do not specify any unit, as in \draw (0,0) -- (1,1);).

Issue: When no unit is given in the [rounded corners = <value>] option, TikZ seems to assume the given length is expressed in pt, i.e. not in the same unit than the default one. This phenomenon is highlighted in the example below. (This might be due to the fact that [rounded corners] = [rounded corners = 4pt]?)

enter image description here

\documentclass[margin=1cm]{standalone}
    \usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[very thick]
        \draw [help lines, black!10] (-.5,-.5) grid (7.5,3.5);

        \draw [rounded corners=1] (0,0) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=1}};

        \draw [rounded corners=1cm] (4,0) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=1cm}};
    \end{tikzpicture}
\end{document}

Question: I would like to specify the radius of rounded corners as a multiple of the default unit (without assuming 1 default unit = 1cm). How can I achieve this?

Best Answer

Well, you can always save the default unit in a macro and write rounded corners=<number>\macroname. Or you could use a wrapper style definition which automates this. For example:

\documentclass[border=10pt,multi,tikz]{standalone}
\begin{document}
\tikzset{%
  ebo unit/.store in=\ebounit,
  ebo corners/.style={rounded corners=#1\ebounit},
}
\begin{tikzpicture}[very thick, ebo unit=cm]
  \draw [help lines, black!10] (-.5,-.5) grid (7.5,3.5);
  \draw [ebo corners=1] (0,0) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=1}};
  \draw [rounded corners=1cm] (4,0) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=1cm}};
  \begin{scope}[ebo unit=mm,blue]
    \draw [ebo corners=5] (0,0) ++(.25,.25) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=5}};
    \draw [rounded corners=5mm] (4,0) ++(.25,.25) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=5mm}};
  \end{scope}
  \begin{scope}[ebo unit=ex,green]
    \draw [ebo corners=8] (0,0) ++(-.25,-.25) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=8}};
    \draw [rounded corners=8ex] (4,0) ++(-.25,-.25) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=8ex}};
  \end{scope}
\end{tikzpicture}
\end{document}

ebo corners and rounded corners