The \DrawBoundingBox macro draws bounding box around a figure with a 1pt border.
In the first figure, produced with \MyNode\MyNode, no tweaks are done to tikz's bounding box. Due to the control points of the loop, this results in a slightly wider bounding box on the left and right and a much larger one at the top. Spacing at the bottom seems correct. In this case, where the default bounding box is used, the two figures are placed next to each other as desired.
In the second figure, I use \MyNode* which interrupts the bounding box of the loop and manually adjusts the height to obtain the correct bounding box. This figure shows that the bounding box tweak was correct as it has the desired 1pt spacing on all four sides.
However, when I put two figures with the bounding box tweak next to each other, \MyNode*\MyNode*, they seem to overlap (third figure). It seems as it the second instance of use as bounding box in the \DrawBoundingBox macro has been ignored.
I would prefer to do this in two steps as this allows each figure to tweak its own bounding box before invoking \DrawBoundingBox too add additional tweaks as desired.
What changes do I need to make so that the figures with the tweaked bounding box are placed next to each other without overlapping the bounding boxes.
Code:
\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\newcommand*{\DrawBoundingBox}[1][]{%
\draw [
use as bounding box,% <-- this seems to have no effect
draw=gray,
]
([shift={(-1pt,-1pt)}]current bounding box.south west)
rectangle
([shift={(1pt,1pt)}]current bounding box.north east);
}
\tikzset{Loop Style/.style={loop above, ultra thick, red, distance=1.0cm, ->}}
\newcommand*{\BBoxTweakForLoopDistance}{0.75cm}%
\NewDocumentCommand{\MyNode}{s}{%
\begin{tikzpicture}[baseline]
\node [draw=black, circle] (A) at (0,0) {a};
\IfBooleanTF{#1}{% Interrupt bounding box for * variant
\begin{pgfinterruptboundingbox}
\path (A) edge[Loop Style] (A);
\end{pgfinterruptboundingbox}
\path [use as bounding box]%% Expand bounding box to include the loop path
(current bounding box.south west)
rectangle
([shift={(0pt,\BBoxTweakForLoopDistance)}]current bounding box.north east);
}{%
\path (A) edge[Loop Style] (A);
}%
\DrawBoundingBox
\end{tikzpicture}%
}
\begin{document}
\MyNode\MyNode
\quad
\MyNode*
\quad
\MyNode\MyNode
\end{document}



\pgfresetboundingbox. However, if the\path [use as bounding box] ...is placed within ascopethings work as desired. Seems to me that that should have been the default behavior, but thetikz/pgfauthors must have had their reasons. Do you want to post an answer? – Peter Grill May 21 '18 at 21:25pgfinterruptboundingboxenvironment is equivalent to adding theoverlayoption, i.e.\path[overlay] (A) edge[Loop Style] (A);would be shorter. ;-) – May 21 '18 at 21:35