如何更改用 Tikz 绘制的节点之间的间距?

如何更改用 Tikz 绘制的节点之间的间距?

我有一个标记为“正交图层”的节点网格,我想修改其间距。节点之间的间距太大,我希望它看起来更像“网格”。我该怎么做?代码如下:

在此处输入图片描述

\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\tikzset{%
  every neuron/.style={
    circle,
    draw,
    minimum size=1mm
  },
  neuron missing/.style={
    draw=none, 
    scale=5,
    text height=0.333cm,
    execute at begin node=\color{black}$\vdots$
  },
}

\begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]

\foreach \m/\l [count=\y] in {1,...,5}
  \node [every neuron/.try] (input-\m) at (0,2.5-\y) {};

\foreach \m [count=\y] in {1,...,5}
  \node [every neuron/.try ] (input2-\m) at (0.25,2.5-\y) {} ;
\foreach \m [count=\y] in {1,...,5}
  \node [every neuron/.try] (input3-\m) at (0.5,2.5-\y) {} ;
  \foreach \m [count=\y] in {1,...,5}
  \node [every neuron/.try, neuron \m/.try ] (input4-\m) at (0.75,2.5-\y) {} ;
\foreach \m [count=\y] in {1,missing,2}
  \node [every neuron/.try, neuron \m/.try ] (lexical-\m) at (2,2-\y) {};
  
\foreach \m [count=\y] in {1,2,3,4}
  \node [every neuron/.try, neuron \m/.try ] (semantic-\m) at (4,1.5-\y) {};

\foreach \m [count=\y] in {1,missing,2}
  \node [every neuron/.try, neuron \m/.try ] (context-\m) at (6,1.5-\y) {};

\foreach \l [count=\x from 0 ] in {Orthographic, Lexical, Semantic, Contextual}
  \node [align=center, above] at (\x*2,2) {\l \\ layer};

\end{tikzpicture}

\end{document}

答案1

像这样?

在此处输入图片描述

您的图像仍保留在表格中,因此可以按原样书写。通过使用makecell用于编写列标题和chainsTi的包Z 库用于在单元中定位节点(必要时):

\documentclass[border=0.125cm, varwidth]{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\tikzset{
node distance = 2pt,
  start chain = going right,
     C/.style = {circle, draw, on chain, node contents={}},
   dot/.style = {circle, fill, inner sep=2pt, node contents={}},
every picture/.append style = {baseline}
        }
\usepackage{makecell}
\renewcommand\theadfont{\small\linespread{0.84}\selectfont}


\begin{document}

    \begin{tabular}{*4{c}}
 \thead{Orthographic\\ layer}
    &   \thead{Lexical\\ layer}
        &   \thead{Semantic\\ layer}
            &   \thead{Contextual\\ layer}   \\
\tikz{\foreach \i in {1,...,4}\node [C];}
    &   &   &   \\ 
%
    &   \tikz{\node [C];}
        &   &   \\
%
\tikz{\foreach \i in {1,...,4}\node [C];}
    &   \tikz{\node [dot];}
       &    &   \tikz{\node [dot];}   \\
%
    &   \tikz{\node [C];}
        &   &   \\
%
\tikz{\foreach \i in {1,...,4}\node [C];}
    &   &   \tikz{\node [C];}  
            &   \tikz{\node [C];}   \\
%
\tikz{\foreach \i in {1,...,4}\node [C];}
    &   &   \tikz{\node [C];}
            &   \\
    \end{tabular}
\end{document}

笔记

  • node distance˙ defined in the单元格中节点(圆圈)之间的距离由\tikzset`控制
  • 垂直距离(在上面的 MWE 中使用表格的默认设置)可以通过包makegapedcells中定义的宏来更改makecells。例如:
\begin{table}[ht]
\setcellgapes{3pt}
\makegapedcells
    \begin{tabular}{*4{c}}
% table body
\end{table}
  • 列间距可以更改,\tabcolsep默认值为 6pt。可以通过以下方式更改\setlength\tabcolsept{<desired column sep>}

相关内容