居中 TikZ 图

居中 TikZ 图

有没有一种简单的方法可以将以下两组二分图水平对齐,即将左侧的三个节点向下移动 1.5 倍垂直节点距离?

在此处输入图片描述

\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}

答案1

利用chains库、网格上的节点、定义长度来计算神经元的距离。

最小工作示例(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}

在此处输入图片描述

答案2

在此处输入图片描述

\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}

答案3

有一个简单的方法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}

生产

在这种情况下,您不需要计算任何东西,这项工作留给了layered

这是一个最小的例子,您可以比较它的用法:

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

最小示例比较

相关内容