Assuming the use of nodes combined with the minimum width
, minimum height
and minimum size
options to generate squares and/or rectangles, another possible solution would be to adjust the provided sizes by subtracting from them \pgflinewidth
(there are two drawn lines in each direction and only half of each drawn line is drawn outside the shape, giving us 2*\pgflinewidth/2 = \pgflinewidth
).
This means that we can draw a square/rectangle of any size that accounts for the size of the border such that the produced shapes have the right sizes. In the sample output below, the two shapes with the yellow border are drawn with no size adjustment. The same two shapes are drawn with a red border on top of them, but these shapes have their sizes adjusted by subtracting from them \pgflinewidth
. The opacity of the adjusted shapes is reduced to showcase the effect.
Output:

Solution:
\documentclass[tikz,border=2mm]{standalone}
\tikzset{%
regularsquare/.style={line width=2pt,draw=yellow,fill=white,minimum size=1cm},
adjustedsquare/.style={line width=2pt,draw=yellow,fill=white,minimum size=1cm-\pgflinewidth},
regularrectangle/.style={line width=4pt,draw=yellow,fill=white,minimum height=0.5cm,minimum width=1cm},
adjustedrectangle/.style={line width=4pt,draw=yellow,fill=white,minimum height=0.5cm-\pgflinewidth,minimum width=1cm-\pgflinewidth},
}
\begin{document}
\begin{tikzpicture}
\node[regularsquare,] at (0,0) {};
\node[adjustedsquare,draw=red,opacity=0.5,] at (0,0) {};
\node[regularrectangle,] at (0,-1.2cm) {};
\node[adjustedrectangle,draw=red,opacity=0.5,] at (0,-1.2cm) {};
\end{tikzpicture}
\end{document}