[Tex/LaTex] PGF/TikZ Draw the border of a shape inside it

bordertikz-pgf

I want to draw a thick border for a shape (let's say a square) inside it and not on its center, like shown in the image. I am interested in obtaining the second case.
Border drawn in the middle vs. border drawn inside

Unfortunately I cannot find any information regarding this. I would really appreciate if someone could help me with this. Thank you in advance.

The shape is drawn with the \filldraw command

\filldraw[fill=white, draw=yellow, line width=3mm] (0, 0) rectangle +(2, 2);

Best Answer

you can clip the half of it and then force it to draw it again double the line width. The scope limits the clipping effect.

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip[postaction={fill=white, draw=yellow, line width=6mm}] (0,0) rectangle +(2,2);
\end{scope}
\draw (0,0) rectangle (2,2);
\end{tikzpicture}
\end{document}

enter image description here

or using Mark Wibrow's further shortcut makes it a one-liner

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[preaction={clip,postaction={fill=white, draw=yellow, line width=6mm}}]
     (0,0) rectangle +(2,2);
\end{tikzpicture}
\end{document}