# Centering TikZ Graph

Is there a simple way to align the following two sets of the bipartite graph horizontally, i.e. move the three nodes on the left 1.5 times the vertical node distance downwards?

\begin{tikzpicture}
\graph[grow right sep=2cm,empty nodes,nodes={fill,circle,inner sep=0,minimum size=2mm}] {
subgraph K_nm [n=3,m=6]
};
\end{tikzpicture}

• you need to use matrix for that -- this is the natural placement for such graphs-- tex.stackexchange.com/a/588629/197451 Apr 5 '21 at 15:15
• - please see the answer below if it suits the requirement Apr 5 '21 at 15:41

## 3 Answers

With use of the chains library, nodes on grid, defined lenght for calculating neurons distances.

Minimal working example (MWE):

\documentclass[border=3.141692]{standalone}
\usepackage{tikz}
\usetikzlibrary{chains,
positioning}
\newlength\ND
\begin{document}
\begin{tikzpicture}[
node distance = \ND, on grid,
start chain = going below,
neuron/.style = {circle,draw, fill, minimum size=5pt, inner sep=0pt,
on chain, node contents={}},
]
\setlength\ND{8mm}    % Nuuron Distance
% left neurons
\foreach \i in {1,2,3}
\node (n1\i) [neuron];
% right neurons
\node (n21) [neuron, above right=1.5\ND and 4*\ND of n11];
\foreach \i in {2,...,6}
\node (n2\i) [neuron];
% Connect every input newrons with every output one
\foreach \i in {1,...,3}
\foreach \j in {1,...,6}
\draw (n1\i) -- (n2\j);
\end{tikzpicture}
\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows.meta,shapes,shapes.multipart, positioning, decorations.markings,arrows.meta, calc, fit}
\def\layersep{4cm}
\begin{document}
\begin{tikzpicture}[
node distance=\layersep,
neuron/.style={circle,fill=black!25,minimum size=5pt,inner sep=0pt},
input neuron/.style={neuron, fill=red!50},
output neuron/.style={neuron, fill=orange!50},
hidden neuron/.style={neuron, fill=green!50},
]
% Draw the input layer nodes
\foreach \name / \y in {1,...,3}
% This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
\node[input neuron] (I-\name) at (0,-\y) {};

% Draw the hidden layer nodes
\foreach \name / \y in {1,...,6}
\path[yshift=1cm]
node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};
%       % Connect every node in the input layer with every node in the
%       % hidden layer.
\foreach \source in {1,...,3}
\foreach \dest in {1,...,6}
\path (I-\source) edge (H-\dest);
\end{tikzpicture}
\end{document}

• Thank you for the help! Though, it seems there's no easier way with an option, right? Apr 5 '21 at 16:08

There is an easy way using layered layout:

% !TeX program = lualatex
\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{graphs.standard,graphdrawing}
\usegdlibrary{layered}

\begin{document}
\tikz \graph[empty nodes,
layered layout, % <- new
level distance=2cm, % <- new
grow=right, % <- new
nodes={fill,circle,inner sep=0,minimum size=2mm}]
{subgraph K_nm [n=3,m=6]};
\end{document}


In this case you don't need to calculate anything, that work is left to layered.

Here is minimal example where you can compare its usage:

\tikz \graph[layered layout,grow=right] {a -- {b,c}};
\tikz \graph {a -- {b,c}};


• Great, thank you! Jul 25 '21 at 15:10