TransWikia.com

Position the beginning node of a graph with respect to another node

TeX - LaTeX Asked by likethevegetable on January 9, 2021

I would like to start a graph‘s first node (G1) with respect to another pre-existing node (N1), using the positioning library. I would like it to appear as follows:

N1
G1 -  - G2
        N2

I’ve included a MWE below.

My first attempt was to specify path[below=of N1] at the beginning of the graph, but that did not work as the G1 and the following graph were in line.

My second attempt which brings me very close to what I want, is to define a coordinate [below=of N1](Gstart), and shift the graph. The slight issue with this is that G1 is slightly offset (see the red ink I’ve marked up the picture with).

Note that I can position node N2 wrt the graph node G2 as I expected.

documentclass{article}
usepackage{tikz}
usetikzlibrary{positioning,graphs}

begin{document}


begin{tikzpicture}[inner sep = 0pt, outer sep = 0pt,]
tikzset{node distance = 1cm and 2cm}

node (N1) {N1};

%%% First attempt
path[below=of N1] graph [grow right sep=2cm]{ G1 -- P/ -- G2};  %%% graph doesnt start below if I try this

%%% My solution, gets me a bit close, but the graph is horizontally offset a bit
coordinate [below=of N1](Gstart);
path[shift={(Gstart)}] graph [grow right sep=2cm]{ G1 -- P/ -- G2};  %%% graph doesnt start below

node (N2) [below=of G2] {N2};
end{tikzpicture}

end{document}

enter image description here

2 Answers

I have not gone into the merits of your placement

The problem you face can be resolved with the solution given at page 301 of the pgf manual by using graphs with the option [no placement]

Graphs can be placed on a grid to your choice by varying the x and y coordinate -- additionally once a coordinate is set it will remain set for the next node in the graph which can be placed only by varying one coordinate x or y -- there is no need to vary both coords

For a better understanding see the example below

tikz graph [no placement]
{
    a ->
    { [x=1] % group option
        b [y=0] -> c[y=1]
    };
};

which results in

enter image description here

Applying the same strategy to you case using the code --

tikzgraph[no placement]
{
N1,
G1[y=-1]--G2[x=2,y=-1]
};

results in

enter image description here

As you see there is no alignment problem -- you can now expand on the graph as per your requirement

Complete MWE

documentclass{article}
usepackage{tikz}
usetikzlibrary{positioning,graphs}

begin{document}

tikzgraph[no placement]
{
N1,
G1[y=-1]--G2[x=2,y=-1]
};
end{document}

EDIT Second trial adapted from -- TikZ Graph with usebox commands not working

enter image description here

documentclass{article}
usepackage{tikz}
usetikzlibrary{positioning,graphs}

begin{document}



begin{tikzpicture}[inner sep = 0pt, outer sep = 0pt,]
tikzset{node distance = 1cm and 2cm}
coordinate (m);
node (n)     [right= 0pt of m] {N1};


begin{scope}[yshift=-1cm,local bounding box=A1]
      graph [grow right sep=2cm]{ "G1" -- b/ -- "G2"}; 
    



end{scope}
path (A1.west) node[below=1em, align =left]{``working with N1\ outside the scope''};

end{tikzpicture}

end{document}

Answered by js bibra on January 9, 2021

Is it still relevant whether instead of setting G1 below N1 you declare N1 above G1 ? which yields :

begin{document}


begin{tikzpicture}[inner sep = 0pt, outer sep = 0pt]
tikzset{node distance = 1cm and 2cm}
%%% First attempt
%path graph [grow right sep=2cm]{ G1 -- P/ -- G2};  %%% graph doesnt start below if I try this

%%% My solution, gets me a bit close, but the graph is horizontally offset a bit
path graph[grow right sep=1cm]{G1--P/--G2};  %%% graph doesnt start below
node(N2)[below=of G2] {N2};
node[above of =G1](N1){N1};
end{tikzpicture}

end{document}

enter image description here

Answered by mxnc baud on January 9, 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