The \path (0,0)
part only worked because the diagram in the linked Q&A doesn't protrude into the left margin. By specifying any coordinate with an x value that is not dependent on another picture, we give PGF/TikZ a reference point that it can use to place the new nodes in relation to. It can now calculate the horizontal distance to an object in another diagram and place the new node a specific instance in relation to that coordinate.
In your case, it does the same, however it can't place the node as far left as it needs because TeX will always place the diagram too far to the right because it needs to actually protrude into the margin.
It is better to use trim left
instead of a coordinate. It has the same effect but it allows PGF/TikZ to adjust the horizontal part of the bounding box as needed.
The specific value doesn't matter. Try it with trim left=1000pt
: on the first run, the diagram isn't even visible, on the second, it is perfectly centered under the other because it then knows where x = 1000pt is (it's at the start of the TeX line, that's where we put it!) and can then place the new node properly in relation to both the other diagram and its own.
\begin{tikzpicture}[remember picture, trim left=0pt]
\node[draw, align=center, anchor=north] (m) at (n.south)
{A new node in a new picture\\that should take up vertical space\\and be centered under First Node};
\end{tikzpicture}
This is actually a less extreme of the overlay
option applied to the whole diagram without anything actually supplying anything to the bounding box.
The overlay
option basically has the same effect as trim left = 0pt
, trim right = 0pt
, baseline = 0pt
and no height/depth.
Here, too, on the second run, PGF/TikZ knows where the origin (or any other coordinate in this remembered picture) is and now knows how objects in other diagrams are placed relevant to it. And this is all that remember picture
does, really.
Setting overlay
is not necessary to use remember picture
.
Usually (and in the examples in the manual, unfortunately) the remember picture
is used to connect to an object in another diagram. Only this needs overlay
on the connection.
Generalizing this, you can do trim left=0pt, trim right=\linewidth
(or \linewidth-\parindent
) and then you will have a TikZ diagram that is horizontally aligned, protrudes into the margin as needed and doesn't leave room for anything else on that TeX line.
Of course, you won't always want to have something protrude the margin but then the objects in the other diagram shouldn't be placed where they are.
To reiterate:
Both the horizontal and the vertical position of a TikZ diagram are controlled by TeX, the only thing PGF/TikZ can control is
- whether PGF/TikZ knows about how TeX has placed a diagram in a previous run (→
remember picture
) and
- what TeX sees from the TikZ picture, i.e. its height, depth and width, these are solely controlled by
baseline
, overlay
(as well as the pgfinterruptboundinbox
environment), trim left
and trim right
as well as the coordinates we reference inside a picture.
We just need to make sure that TeX has enough room to actually place the diagrams as we want.
Code
\documentclass{article}
\usepackage{tikz}
\begin{document}
~\\
A text line\\
%
\begin{tikzpicture}[remember picture]
\node[draw] (n) {First Node};
\end{tikzpicture}\\
%
\null\begin{tikzpicture}[remember picture, trim left=0pt]
\node[draw, align=center, anchor=north] (m) at (n.south)
{A new node in a new picture\\that should take up vertical space\\and be centered under First Node};
\end{tikzpicture}\\
%
A new text line\\
\noindent
\rule{3cm}{2em}\\
\rule{7cm}{3em}
\bigskip
\noindent\begin{tikzpicture}[remember picture]
\node[draw] (n) {First Node};
\end{tikzpicture}
% no \noindent!
% the space between the nodes is exactly \baselineskip
% because the upper diagram is placed *on* the baseline
% and the second diagram is placed *below* the baseline.
\smash{\color{gray}\rule{.1pt}{\baselineskip}}%
\begin{tikzpicture}[remember picture, trim left=0pt, baseline=(m.north)]
\node[draw, align=center, anchor=north] (m) at (n.south)
{A new node in a new picture\\that should take up vertical space\\and be centered under First Node};
\end{tikzpicture}
\end{document}
Output
If you don't want the bottom picture to protrude into the margin, you either have to place the bottom one first:
\documentclass{article}
\usepackage{tikz}
\usepackage[showframe, pass]{geometry}
\begin{document}
\noindent
\begin{tikzpicture}[remember picture]
\node[draw, align=center] (M)
{A new node in a new picture\\that should take up vertical space\\and be centered under First Node};
\end{tikzpicture}%
\begin{tikzpicture}[remember picture, trim left=0pt, trim right=0pt, baseline=0pt]
\node[draw, anchor=south] (N) at ([yshift=\baselineskip]M.north) {First Node};
\end{tikzpicture}
\end{document}
or deploy some tikzmark
tactics:
\documentclass{article}
\usepackage{tikz}
\usepackage[showframe, pass]{geometry}
\usetikzlibrary{tikzmark}
\tikzset{restore nodes from file}
\begin{document}
\noindent
\begin{tikzpicture}[remember picture, trim left=0pt]
\node[draw] at (M.north) {First Node};
\end{tikzpicture}
\noindent
\begin{tikzpicture}[
remember picture,
save nodes to file,
baseline=(M.north)]
\node[draw, align=center, anchor=north, save node] (M)
{A new node in a new picture\\that should take up vertical space\\and be centered under First Node};
\end{tikzpicture}
\end{document}
remember picture overlay
TeX sees an\hbox to 0pt{\vbox to 0pt}, so after the code vskip20pt... etc. It it simialr to drawing something to a LaTeX picture environment.