The best thing would probably be to create your own shape.
Here, it's also much easier to take the outer sep
s into account.
\documentclass{article}
\usepackage{tikz}
\pgfset{corners rectangle size/.initial=10pt}
\makeatletter
\pgfdeclareshape{corners rectangle}{%
\inheritsavedanchors[from=rectangle]%
\inheritanchorborder[from=rectangle]%
\inheritanchor[from=rectangle]{north}%
\inheritanchor[from=rectangle]{north west}%
\inheritanchor[from=rectangle]{north east}%
\inheritanchor[from=rectangle]{center}%
\inheritanchor[from=rectangle]{west}%
\inheritanchor[from=rectangle]{east}%
\inheritanchor[from=rectangle]{mid}%
\inheritanchor[from=rectangle]{mid west}%
\inheritanchor[from=rectangle]{mid east}%
\inheritanchor[from=rectangle]{base}%
\inheritanchor[from=rectangle]{base west}%
\inheritanchor[from=rectangle]{base east}%
\inheritanchor[from=rectangle]{south}%
\inheritanchor[from=rectangle]{south west}%
\inheritanchor[from=rectangle]{south east}%
\foregroundpath{
% store lower right in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
\pgfmathsetlength\pgf@xc{\pgfkeysvalueof{/pgf/outer xsep}}%
\pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/outer ysep}}%
\advance\pgf@xa by \pgf@xc \advance\pgf@xb by -\pgf@xc
\advance\pgf@ya by \pgf@yc \advance\pgf@yb by -\pgf@yc
\pgfmathsetlength\pgfutil@tempdima{\pgfkeysvalueof{/pgf/corners rectangle size}}%
\pgfpathmoveto{\pgfqpoint{\dimexpr\pgf@xa+\pgfutil@tempdima}{\pgf@ya}}%
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}%
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\dimexpr\pgf@ya+\pgfutil@tempdima}}%
\pgfpathmoveto{\pgfqpoint{\dimexpr\pgf@xa+\pgfutil@tempdima}{\pgf@yb}}%
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}%
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\dimexpr\pgf@yb-\pgfutil@tempdima}}%
\pgfpathmoveto{\pgfqpoint{\dimexpr\pgf@xb-\pgfutil@tempdima}{\pgf@ya}}%
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}%
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\dimexpr\pgf@ya+\pgfutil@tempdima}}%
\pgfpathmoveto{\pgfqpoint{\dimexpr\pgf@xb-\pgfutil@tempdima}{\pgf@yb}}%
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}%
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\dimexpr\pgf@yb-\pgfutil@tempdima}}%
\pgfsetarrowsstart{}%
\pgfsetarrowsend{}%
}%
}%
\makeatother
\begin{document}
\begin{tikzpicture}[minimum width=8cm, minimum height=2cm, nodes=corners rectangle]
\node[draw] {lorem ipsum};
\node[corners rectangle size=15pt, draw=red, thick] at (0, -3) (a) {lorem ipsum};
\foreach \ang in {5,...,20} \draw (a.\ang) --+(\ang:.5);
\end{tikzpicture}
\end{document}
As an easy workaround, you can make use of append after command
and the like.
With \pgfsettransform
we make sure that the same transformation are used that were applied to the node, making this independently from the path those corners are drawn on.
There's no consideration for outer sep
or the line width.
\documentclass{article}
\usepackage{tikz}
\tikzset{
draw corners/.style={
append after command={\bgroup
\pgfextra{\pgfsettransform{\csname pgf@sh@nt@\tikzlastnode\endcsname}}
[to path=|-(\tikztotarget)]
([yshift=-\pgfkeysvalueof{/tikz/draw corners size}]\tikzlastnode.north west)
edge[#1] ([xshift= \pgfkeysvalueof{/tikz/draw corners size}]\tikzlastnode.north west)
([yshift=-\pgfkeysvalueof{/tikz/draw corners size}]\tikzlastnode.north east)
edge[#1] ([xshift=-\pgfkeysvalueof{/tikz/draw corners size}]\tikzlastnode.north east)
([yshift= \pgfkeysvalueof{/tikz/draw corners size}]\tikzlastnode.south east)
edge[#1] ([xshift=-\pgfkeysvalueof{/tikz/draw corners size}]\tikzlastnode.south east)
([yshift= \pgfkeysvalueof{/tikz/draw corners size}]\tikzlastnode.south west)
edge[#1] ([xshift= \pgfkeysvalueof{/tikz/draw corners size}]\tikzlastnode.south west)
\egroup}},
draw corners size/.initial=10pt}
\begin{document}
\begin{tikzpicture}[rectangle, minimum width=8cm, minimum height=2cm]
\path[rotate=30]node[rotate=10, draw corners] at (0,0) {lorem ipsum};
\tikzset{draw corners size=15pt}
\node[scale=.5, draw corners={red, thick}] at (0,-3) {lorem ipsum};
\end{tikzpicture}
\end{document}
You could also use path picture
to avoid nesting paths:
\documentclass{article}
\usepackage{tikz}
\tikzset{
draw corners/.style={
path picture={
\draw[#1]
(path picture bounding box.north west)
+(\pgflinewidth,-\pgfkeysvalueof{/tikz/draw corners size}) |-
+(\pgfkeysvalueof{/tikz/draw corners size},-\pgflinewidth)
(path picture bounding box.south west)
+(\pgflinewidth,\pgfkeysvalueof{/tikz/draw corners size}) |-
+(\pgfkeysvalueof{/tikz/draw corners size},\pgflinewidth)
(path picture bounding box.north east)
+(-\pgflinewidth,-\pgfkeysvalueof{/tikz/draw corners size}) |-
+(-\pgfkeysvalueof{/tikz/draw corners size},-\pgflinewidth)
(path picture bounding box.south east)
+(-\pgflinewidth,\pgfkeysvalueof{/tikz/draw corners size}) |-
+(-\pgfkeysvalueof{/tikz/draw corners size},\pgflinewidth);
}
},
draw corners size/.initial=10pt
}
\begin{document}
\begin{tikzpicture}
\node[draw corners, rectangle, minimum width=8cm, minimum height=2cm]
at (0,0) {lorem ipsum};
\end{tikzpicture}
\end{document}