TransWikia.com

Tikz: Is it possible to align any two nodes by aligning their subnodes?

TeX - LaTeX Asked by ZIYAN CHEN on May 8, 2021

My question is:

  • Is it possible to align any two nodes by aligning their subnodes?

  • For TikZ nodes, is it possible to add a new anchor to an existed nodes?
    (for example: add node-b.north as an new anchor of node-a)

In this example, I try to put the "Delta" of 2 nodes on a same vertical line, but I failed.

documentclass[10pt,a4paper, utf-8]{article}
usepackage{
    amsmath, 
    mathtools,
    amsfonts,
    amssymb,
    tikz
}
usetikzlibrary{
    tikzmark,
    positioning,
    matrix,
}

begin{document}
begin{tikzpicture}[remember picture]
node(a) [draw] {$A subnode[draw]{sub-a}{Delta} B wedge C$};
node(b) [draw,anchor=sub-b,below=1pt of sub-a]{$A wedge C subnode{sub-b}{Delta} B$};
end{tikzpicture}
end{document}

My idea is to drop the anchor of b from sub-b to sub-a (like matrix: you can set matrix anchor=inner node).
I use tikzmark to obtain two "Delta"s’ coordinates.
When I try to set the starting point of anchor, it doesn’t work.

An simple analogy may help you understand my idea:
if node b is a ship, then
command "anchor=..." set the release point of the anchor,
command "below=..." set the destination of the anchor.


PS:

Although matrix is a possible solution,
but it’s troublesome when the "Delta" was inside a fraction(for example, the "Delta" is inside the denominator) .
The code block shown above is just an simple example,
what I really hope to see is a more general solution(stated in the title).

The first question has been answered, but I really like to see the answer of the second question(For TikZ nodes, is it possible to add a new anchor to an existed nodes?).

And please, do not delete your original answer. They are also useful ^__^.

2 Answers

This is a subtle one! On the face of it, it seems straightforward. We cannot directly use the subnode coordinate in positioning the outer node (b) because its location on the page is not known until after node b is placed. But there is a standard way around this - we save the various necessary locations to the aux file and load them in before positioning the nodes.

If it were this simple, what we would do is save the locations of the sub-b node and the b node and then use their difference to adjust the position of the b node. Although this moves b, it also moves sub-b by the same amount so their difference remains the same.

The snag is that the coordinates of the subnode (sub-b) are already based on the previous compilation. So the above strategy doesn't work because TikZ always thinks that sub-b is located where it was on the previous compilation. But the position of the b node is based on the current compilation. So TikZ is continually playing a game of chase as node b chases the sub-b subnode around the page.

To get out of this trap, we need to also use the position of b on the previous compilation. Fortunately, tikzmark can handle the saving part. A consequence of this is that this requires three compilations to settle.

documentclass[10pt,a4paper]{article}
%url{https://tex.stackexchange.com/q/567070/86}
usepackage{
    amsmath, 
    mathtools,
    amsfonts,
    amssymb,
    tikz
}
usetikzlibrary{
    tikzmark,
    calc,
}

begin{document}
begin{tikzpicture}[remember picture]
node(a) [draw] {$A subnode{sub-a}{Delta} B wedge C$};
node(b) [draw] at ($(sub-a.south)-(pic cs:b)$) {$A wedge C subnode{sub-b}{Delta} B$};
begin{scope}[overlay]
tikzmark{a}{(b.center)}
tikzmark{b}{($(sub-b.north)-(pic cs:a)$)}
end{scope}
end{tikzpicture}
end{document}

Subnodes aligned

Correct answer by Andrew Stacey on May 8, 2021

This solution uses scopes, first to compute the relative position of (sub-b) and (b), then to shift them where they are needed. Due to [remember picture], I could not reuse node names (b) and (sub-b). Note that (b.center) and (c.center) are at the origins of their respective scopes.

documentclass{standalone}
usepackage{
    amsmath, 
    mathtools,
    amsfonts,
    amssymb,
    tikz
}
usetikzlibrary{
    tikzmark,
    positioning,
    matrix,
    calc
}

begin{document}
begin{tikzpicture}[remember picture]
node(a) [draw] {$A subnode[draw]{sub-a}{Delta} B wedge C$};
begin{scope}[opacity=0]
node(b) [draw]{$A wedge C subnode{sub-b}{Delta} B$};
coordinate(S) at ($(sub-a.south)-(sub-b.north)-(0,1pt)$);
end{scope}
begin{scope}[shift=(S)]
node(c) [draw]{$A wedge C subnode{sub-c}{Delta} B$};
end{scope}
end{tikzpicture}
end{document}

demo

Answered by John Kormylo on May 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP