在 Tikz 中复制节点

在 Tikz 中复制节点

发生了一些奇怪的事情,或者说发生了一些我无法理解的事情。当我尝试复制一个由两个用箭头连接的节点组成的非常简单的图形时,发生了以下情况:在左侧(案例 1),图形是平行的,因为我使用了命令below = 15mm of i-0.mid,这很好,但为什么它不能与 20mm 配合使用以使其与网格重合?在右侧(案例 2),我使用的below = 15mm of i-3结果是“i”和“j”节点具有不同的间距。为什么会这样?

在此处输入图片描述

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{adjustbox}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows,calc}

\begin{document}

\begin{adjustbox}{max totalsize={.9\textwidth},center}

       \begin{tikzpicture}[>=stealth',auto,node distance=2cm,
      thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]
         
         \node at (2.5, 2)    {Cases 1};

        \node[main node] at (0, 0)   (i-0) {i};
        \node[main node] at (5, 0)  (j-0) {j};
        \draw [->]   (i-0) -- (j-0) node[pos=0.5, above] {$x_{ij}=0$};
        \node [below = 1mm of i-0]  {$\in X$};
        \node [below = 1mm of j-0]  {$\in X$};
        \node [above = 1mm of j-0]  {$(i^+,q_{ij})$};
        

        \node [main node, below = 15mm of i-0.mid] (i-1) {i};
        \node [main node, below = 15mm of j-0.mid] (j-1) {j};
        \draw [->]   (i-1) -- (j-1) node[pos=0.5, above] {$0 < x_{ij} < q_{ij}$};
        \node [below = 1mm of i-1]  {$\in X$};
        \node [below = 1mm of j-1]  {$\in X$};
        \node [above = 1mm of j-1]  {$(i^+,q_{ij}-x_{ij})$};
        
        
        \node [main node, below = 15mm of i-1.mid] (i-2) {i};
        \node [main node, below = 15mm of j-1.mid] (j-2) {j};
        \draw [->]   (i-2) -- (j-2) node[pos=0.5, above] {$x_{ij} = q_{ij}$};
        \node [below = 1mm of i-2]  {$\in X$};
        \node [below = 1mm of j-2]  {$\in \overline{X}$};
        
         \node at (9.5, 2)    {Cases 2};
        
        \node [main node, right = 65mm of i-0] (i-3) {i};
        \node [main node, right = 65mm of j-0] (j-3) {j};
        \draw [->]   (i-3) -- (j-3) node[pos=0.5, above] {$x_{ji}=0$};
        \node [below = 1mm of i-3]  {$\in X$};
        \node [below = 1mm of j-3]  {$\in \overline{X}$};


        \node [main node, below = 15mm of i-3] (i-4) {i};
        \node [main node, below = 15mm of j-3] (j-4) {j};
        \draw [<-]   (i-4) -- (j-4) node[pos=0.5, above] {$0 < x_{ji} < q_{ji}$};
        \node [below = 1mm of i-4]  {$\in X$};
        \node [below = 1mm of j-4]  {$\in X$};
        \node [above = 1mm of j-4]  {$(i^-,x_{ji})$};
         
        \node [main node, below = 15mm of i-4] (i-5) {i};
        \node [main node, below = 15mm of j-4] (j-5) {j};
        \draw [<-]   (i-5) -- (j-5) node[pos=0.5, above] {$x_{ji} = q_{ji}$};
        \node [below = 1mm of i-5]  {$\in X$};
        \node [below = 1mm of j-5]  {$\in X$};
        \node [above = 1mm of j-5]  {$(i^-,x_{ji})$};       
        
        \draw [cyan, densely dotted] (0,-6) grid (12,3); 

    \end{tikzpicture} 
\end{adjustbox}


\end{document}

答案1

我会按照以下方式绘制你的图像:

  • 不使用adjustbox,如果图像根据需要更大,我宁愿减小node distance字体大小
  • text height使用和定义圆的大小text depth,以便所有圆都具有相同的大小
  • positioning使用库中定义的位置语法
  • 对于边缘标记使用quotes
  • 对于节点标签使用标签(上方、下方)
\documentclass[border=5pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, 
                calc, 
                positioning,
                quotes
                }

\begin{document}
       \begin{tikzpicture}[>=Stealth,auto,
node distance = 11mm and 33mm, 
     N/.style = {circle, draw, thick,
                 text height=1em, text depth=0.4ex,
                 font=\sffamily\Large\bfseries},
every label/.append style = {inner sep=2pt, font=\small},
every edge quotes/.style  = {inner sep=2pt, font=\small}
                        ]
% left column, 0
\node (i-0) [N,
             label=below:$\in X$]                   {i};
\node (j-0) [N, right=of i-0,
             label=below:$\in X$,
             label={$(i^+,q_{ij})$}]                {j};
    \draw [->]   (i-0) to ["{$x_{ij}=0$}"] (j-0);
% 1
\node (i-1) [N, below = of i-0,
             label=below:$\in X$]           {i};
\node (j-1) [N, below = of j-0,
             label=below:$\in X$,
             label={$(i^+,q_{ij}-x_{ij})$}] {j};
    \draw [->]   (i-1) to ["$0 < x_{ij} < q_{ij}$"] (j-1);
% 2
\node (i-2) [N, below = of i-1,
             label=below:$\in X$]           {i};
\node (j-2) [N, below = of j-1,
             label=below:$\in \overline{X}$]     {j};
    \draw [->]   (i-2) to ["{$x_{ij}=q_{ji}$}"] (j-2);

% right column, 3
\node (i-3) [N, right=7mm of j-0,
             label=below:$\in X$]           {i};
\node (j-3) [N, right=of i-3,
             label=below:$\in \overline{X}$,
             label={$(i^+,q_{ij})$}]        {j};
    \draw [->]   (i-3) to ["{$x_{ji}=0$}"] (j-3);
% 4
\node (i-4) [N, below = of i-3,
             label=below:$\in X$]           {i};
\node (j-4) [N, below = of j-3,
             label={$(i^+,x_{ji})$},
             label=below:$\in X$]           {j};
    \draw [->]   (i-4) to ["$0 < x_{ji} < q_{ji}$"] (j-4);
% 5
\node (i-5) [N, below = of i-4,
             label=below:$\in X$]           {i};
\node (j-5) [N, below = of j-4,
             label={$(i^+,x_{ji})$},
             label=below:$\in X$]           {j};
    \draw [->]   (i-5) to ["{$x_{ji}=q_{ji}$}"] (j-5);
%%%%    titles
\node [above=1em] at ($(i-0.north)!0.5!(j-0.north)$)    {Cases 1};
\node [above=1em] at ($(i-3.north)!0.5!(j-3.north)$)    {Cases 2};

    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容