5

Consider

\documentclass[11pt]{article}
\usepackage{tikz-feynman}
\begin{document}
\begin{tikzpicture}
\graph[layered layout, horizontal=a to b] {
  q -- a,
  qb -- a,
  a -- b -- c -- g1,
  c -- g2,
  b -- g3,
};
\end{tikzpicture}
\end{document}

With LuaTeX, Version 0.95.0 (TeX Live 2016) on a mac running macOS Sierra, I get this

enter image description here

But I would like g1, g2 and g3 to be vertically aligned. How would I do that? I tried to use {[same layer] g1, g2, g3} but then it gives

enter image description here

I would like to keep the angles exactly like those of the first diagram. With just the b--g3 line longer to put g3 aligned with g1 and g2. I even tried g3 [below=of g2] but it does not work.

koleygr
  • 20,105
  • Sorry for the copy n' paste error. I have added precisions about my setup. –  Sep 10 '17 at 22:41
  • Sorry that edited... I thought it will ask you... feel free to go to your previous edition but I think this way is more accurate (just added tikz-feynman tag and changed the title to tikz-feynman) I also would suggest to add luatex tag since my texlive gives different result – koleygr Sep 10 '17 at 22:43
  • 1
    You might have to position the nodes by hand then. The graphdrawing algorithms are good to have the nodes not overlap but manual positioning is basically impossible. – Henri Menke Sep 10 '17 at 23:00
  • Actually, replacing \usepackage{tikz-feynman} by \usepackage{tikz}, \usetikzlibrary{graphdrawing}, \usetikzlibrary{graphs} and \usegdlibrary{layered}, I get the exact same behaviour. I was actually not using tikz-feynman per se: I just find it convenient an import because I know it pulls everything I usually need. –  Sep 10 '17 at 23:01
  • 1
    @koleygr As Luc says, this code doesn't actually use any of tikz-feynmans features, so the title change and tag doesn't really make sense. – Torbjørn T. Sep 11 '17 at 06:21
  • @TorbjørnT. Ok... feel free to change it again. It had sense at the time I added that. I could not reproduce it's result and also that was the only package used. I thought it had to do with this. Of course after Luc's answer the problem seems to be more general (with "graphs") and a change in title is recommended. See here https://tex.meta.stackexchange.com/questions/7391/edit-in-title-of-questions/7412#7412 what I just wrote on meta about such problems. – koleygr Sep 11 '17 at 07:06

1 Answers1

2

You can force a node to be placed at a specific position on the canvas, after the node placement calculations have been completed. This is done with the regardless at syntax, which are described on p380 of the TikZ manual (v3.0.1a).

Node q is placed at the origin (0,0). With the graph set on a 1cm grid, it is then a simple matter of determining the appropriate co-ordinate for node g3. We find that the co-ordinate is (4,-2), which aligns g3 with g1 and g2.

So we can then write: b -- g3[regardless at={(4,-2)}] to add the node at the desired position. Following is the result, which incorporates a background grid (How can I show coordinates by grid in TikZ automatically?) to illustrate the positioning of node g3 at (4,-2).

enter image description here

This is a MWE, modified from the original to show the libraries that are specifically required. The Tikz-Feynman package is not loaded since none of its macros are used. Note that use of the TikZ graph drawing libraries, requires lualatex to compile the code.

% !TeX TS-program = lualatex

\documentclass[tikz,border=5pt]{standalone}
\usepackage{luatex85}

\usetikzlibrary{
    graphs,
    graphdrawing,
}
\usegdlibrary{
    layered,   % requied for layered layout
}

% https://tex.stackexchange.com/a/39698
\makeatletter
\def\grd@save@target#1{%
    \def\grd@target{#1}}
\def\grd@save@start#1{%
    \def\grd@start{#1}}
\tikzset{
    grid with coordinates/.style={
        to path={%
            \pgfextra{%
                \edef\grd@@target{(\tikztotarget)}%
                \tikz@scan@one@point\grd@save@target\grd@@target\relax
                \edef\grd@@start{(\tikztostart)}%
                \tikz@scan@one@point\grd@save@start\grd@@start\relax
                \draw[minor help lines] (\tikztostart) grid (\tikztotarget);
                \draw[major help lines] (\tikztostart) grid (\tikztotarget);
                \grd@start
                \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm}
                \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm}
                \grd@target
                \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm}
                \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm}
                \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
                \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
                \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
                \node[anchor=north,color=lightgray] at ([yshift=-0.15cm]\x,\grd@ya) {\pgfmathprintnumber{\x}};
                \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
                \node[anchor=east,color=lightgray] at ([xshift=-0.15cm]\grd@xa,\y) {\pgfmathprintnumber{\y}};
            }
        }
    },
    minor help lines/.style={
        help lines,
        step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step},
        minor line width/.initial=0.25pt,
    },
    major help lines/.style={
        help lines,
        line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
        step=\pgfkeysvalueof{/tikz/grid with coordinates/major step},
        line cap=round,
        color=lightgray,
    },
    grid with coordinates/.cd,
    minor step/.initial=.2,
    major step/.initial=1,
    major line width/.initial=0.5pt,
}
\makeatother
\begin{document}

\begin{tikzpicture}[
font=\tiny,
color=blue
]

\draw (0,-2) to[grid with coordinates] (4,1);

\graph[
layered layout, 
horizontal=a to b,
] {
  {q,qb} -- a,
  a -- b -- c,
  c -- {g1,g2},
  b -- g3[regardless at={(4,-2)}],
};
\end{tikzpicture}
\end{document}
Ross
  • 5,656
  • 2
  • 14
  • 38