TransWikia.com

macros expanding to a TikZ node

TeX - LaTeX Asked by TeXtnik on May 2, 2021

I want to create rectangular nodes, given two coordinates bottom left and top right, with a macro. The difficult part is that I want to use the macro where a node name can be used; for example, in the place current page is the code below.

This code doesn’t work but it shows the syntax I would like to use. Is it possible to do something similar?

documentclass[]{article}
usepackage{tikz}
usetikzlibrary{fit}
newcommandmakenode[2]{
  node[fit={#1}{#2}] {}
  }

begin{document}
begin{tikzpicture}[overlay, remember picture]
coordinate (bottom left) at (10,10);
coordinate (top right) at (20,20);

node[anchor=south] at (makenode{bottom left}{top right}.center) {My node text};
node[anchor=south] at (current page.center) {My node text};


end{tikzpicture}

end{document}

One Answer

See if the following MWE is what you looking for:

documentclass{article}
usepackage{tikz}
usetikzlibrary{fit}
newcommandmakenode[2]{node[draw=red,         % to show node border, should be removed
                              inner sep=0pt,
                              fit=(#1) (#2)]}
begin{document}
begin{tikzpicture}%[overlay, remember picture]
coordinate (bl)    at (5,5);   % bottom left
coordinate (tr)    at (10,10); % top right

makenode{bl}{tr} {my node text};
end{tikzpicture}
end{document}

enter image description here

Note, this approach to define node will have problems when the text in node is longer than horizontal distance between defined coordinates and when you like to have text in two or more lines.

Addendum: Solution as suggested @Ignasi in comment below answer. Considering it, the code of images which has more similar nodes, can be more easy to draw and connect with arrows:

documentclass[border=3.141592]{article}
usepackage{tikz}
usetikzlibrary{fit}
newcommandmakenode[2]{node[draw=red,         % to show node border, should be removed
                              inner sep=0pt,
                              fit=(#1) (#2)]}
begin{document}
    begin{tikzpicture}[
MN/.style args = {#1/#2}{         % MakeNode
                         draw=red, inner sep=0pt, fit=(#1) (#2)}
                        ]
coordinate (bl)    at (5,5);   % bottom left
coordinate (tr)    at (10,10); % top right

node (mn) [MN=bl/tr] {my node text};
end{tikzpicture}
end{document}

Result is the same as before.

Answered by Zarko on May 2, 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