Tikz 链条之间的空间

Tikz 链条之间的空间

我正在尝试使用 tikz 和链条,但在处理多条链条时遇到了一些麻烦。我该如何定位这些链条?

例如在下面的例子中,我使用第一个yshift节点来定位第二个链,但我认为这不是最好的方法......

而且我还想知道是否有可能先进行链接而无需手动进行脏操作xshift

先感谢您。

\documentclass[a4paper, 11pt]{article}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}

\begin{document}

\begin{figure}[H]
  \centering
  \begin{tikzpicture}[
    circ/.style={
      draw,
      circle,
      fill=blue!30,
      inner sep=5pt
    },
    squa/.style={
      draw,
      inner sep=5pt,
      fill=blue!20,
    },
    start chain=1,
    start chain=2,node distance=15mm
    ] 
    % Variables
    \node[on chain=1,circ] (x1) {$x_1$};
    \node[on chain=1,circ] (x2) {$x_2$};
    \node[on chain=1,circ] (x3) {$x_3$};
    % Clauses
    \node[on chain=2,yshift=-2cm,squa] (c1) {$\lnot x_1 \lor \lnot x_2$};
    \node[on chain=2,squa] (c2) {$x_2 \lor \lnot x_3$};
    \node[on chain=2,squa] (c3) {$x_1 \lor x_2$};
    \node[on chain=2,squa] (c4) {$\lnot x_2 \lor x_3$};
    \node[on chain=2,squa] (c5) {$\lnot x_1 \lor x_3$};
    % Draw
    \draw[dashed] (x1.south) -- (c1.north);
    \draw[dashed] (x1.south) -- (c5.north);
    \draw[dashed] (x2.south) -- (c1.north);
    \draw[dashed] (x2.south) -- (c4.north);
    \draw[dashed] (x3.south) -- (c2.north);
    \draw[] (x1.south) -- (c3.north);
    \draw[] (x2.south) -- (c2.north);
    \draw[] (x2.south) -- (c3.north);
    \draw[] (x3.south) -- (c4.north);
    \draw[] (x3.south) -- (c5.north);
  \end{tikzpicture}
  \caption{Une représentation d'une CNF sous forme de graph}
  \label{fig:sat_graph}
\end{figure}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:

答案1

链可以实现节点链的简单编码。借助定位包,可以轻松控制节点之间的水平和垂直距离,例如:

\documentclass[a4paper, 11pt]{article}
\usepackage{tikz}
\usetikzlibrary{chains,positioning}
\begin{document}

\begin{figure}[htb]
  \centering
  \begin{tikzpicture}[
  node distance=20mm and 15 mm,
  start chain = going right,
    circ/.style={
      draw,
      circle,
      fill=blue!30,
      inner sep=5pt,
      on chain,
    },
    squa/.style={
      draw,
      inner sep=5pt,
      fill=blue!20,
      on chain
    },
    ]
    % Variables
\node[circ] (x1) {$x_1$};
\node[circ] (x2) {$x_2$};
\node[circ] (x3) {$x_3$};
    % Clauses
\node[squa,below left=of x1] (c1) {$\lnot x_1 \lor \lnot x_2$};
\node[squa] (c2) {$x_2 \lor \lnot x_3$};
\node[squa] (c3) {$x_1 \lor x_2$};
\node[squa] (c4) {$\lnot x_2 \lor x_3$};
\node[squa] (c5) {$\lnot x_1 \lor x_3$};
    % Draw
\draw[dashed]   (x1) -- (c1)
                (x1) -- (c5)
                (x2) -- (c1)
                (x2) -- (c4)
                (x3) -- (c2);
\draw   (x1) -- (c3)
        (x2) -- (c2)
        (x2) -- (c3)
        (x3) -- (c4)
        (x3) -- (c5);
  \end{tikzpicture}
  \caption{Une représentation d'une CNF sous forme de graph}
  \label{fig:sat_graph}
\end{figure}

\end{document}

在此处输入图片描述

如果您希望在节点处拥有相同的线的起点和终点,那么您需要在上面的代码中明确定义具有节点锚点的这些点,就像您在问题中一样:

\draw[dashed]   (x1.south) -- (c1.north) ...

在此处输入图片描述

注意:这个答案实际上总结了伊格纳西评论与链有关。我还擅自更改了链的第二个节点的位置。

附录: 从你和 Ingnasi 的回答中我了解到,你喜欢水平居中的机器人链。这很容易通过改变绘制顺序来实现:首先必须在下面绘制链,然后定位包含变量

\documentclass[a4paper, 11pt]{article}
\usepackage{tikz}
\usetikzlibrary{chains,positioning}

\begin{document}
    \begin{figure}[htb]
\centering
    \begin{tikzpicture}[
    node distance = 20mm and 15 mm,
      start chain = going right,
      circ/.style = {draw, circle, fill=blue!30, inner sep=5pt},
      squa/.style = {draw, inner sep=5pt, fill=blue!20,
                     on chain},
                        ]
    % Clauses
\node[squa] (c1) {$\lnot x_1 \lor \lnot x_2$};
\node[squa] (c2) {$x_2 \lor \lnot x_3$};
\node[squa] (c3) {$x_1 \lor x_2$};
\node[squa] (c4) {$\lnot x_2 \lor x_3$};
\node[squa] (c5) {$\lnot x_1 \lor x_3$};
    % Variables
\node[circ,above left= of c3] (x1)  {$x_1$};
\node[circ,above=of c3] (x2)        {$x_2$};
\node[circ,above right=of c3] (x3)  {$x_3$};

    % Draw
\draw[dashed]   (x1) -- (c1)
                (x1) -- (c5)
                (x2) -- (c1)
                (x2) -- (c4)
                (x3) -- (c2);
\draw   (x1) -- (c3)
        (x2) -- (c2)
        (x2) -- (c3)
        (x3) -- (c4)
        (x3) -- (c5);
    \end{tikzpicture}
\caption{Une représentation d'une CNF sous forme de graph}
    \label{fig:sat_graph}
    \end{figure}
\end{document}

改变绘制顺序并仅在底行使用链条功能可以使两行节点(完美)居中:

在此处输入图片描述

答案2

从你的OP 的回答我推断:

  • 您想要绘制两行元素,一行对应variables另一行clauses
  • 两行必须垂直居中
  • 并且你想向两行添加元素并保持相对定位

对我来说,最好的解决方案是matrix每行使用两个。这样,它们可以根据需要进行填充,并且都可以垂直居中。

由于我仍然不知道为什么chains在这种情况下使用我已经从以下代码中抑制了它们:

\documentclass[border=2mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}


\begin{tikzpicture}[
    circ/.style={
      draw,
      circle,
      fill=blue!30,
      inner sep=5pt
    },
    squa/.style={
      draw,
      inner sep=5pt,
      fill=blue!20,
    },
    ] 
    \matrix (var) [draw=red, matrix of nodes, nodes={circ, anchor=center}, column sep=1cm]
    { 
     |(x1)| $x_1$ & |(x2)| $x_2$ & |(x3)| $x_3$ \\
    };

    \matrix (clauses) [draw=red, below=1.5cm of var, 
        matrix of nodes, nodes={squa, anchor=center}, column sep=1cm]
    {
    |(c1)| $\lnot x_1 \lor \lnot x_2$ &
    |(c2)| $x_2 \lor \lnot x_3$ &
    |(c3)| $x_1 \lor x_2$ &
    |(c4)| $\lnot x_2 \lor x_3$ &
    |(c5)| $\lnot x_1 \lor x_3$ \\
    };
    % Draw
    \draw[dashed]
         (x1.south) -- (c1.north)
         (x1.south) -- (c5.north)
         (x2.south) -- (c1.north)
         (x2.south) -- (c4.north)
         (x3.south) -- (c2.north);
    \draw[]
         (x1.south) -- (c3.north)
         (x2.south) -- (c2.north)
         (x2.south) -- (c3.north)
         (x3.south) -- (c4.north)
         (x3.south) -- (c5.north);
  \end{tikzpicture}

\end{document}

在此处输入图片描述

对之前的代码和图表的一些评论。

  • 我使用了为该节点|(x1)|$x_1$分配名称的语法,而不是分配的默认解决方案。x1$x_1$var-1-1
  • 为了解这些节点如何相互放置,draw=red已在两者中加入了选项。作为副作用,内部节点继承了红色。matrixmatrix-nodes
  • 以前的方案允许轻松地从变量或子句矩阵中添加或删除元素。

答案3

虽然 Ignasi 和 Zarko 的解决方案似乎是设置两个链的相对位置的最简单方法,但它不适用于居中。这是我找到的使链居中的补充解决方案。我不知道这是否是使链水平居中的更简单方法,但我找到了一种使用链和矩阵的解决方案。在几行中,想法是将链放入一个节点中,然后将节点放入一个设置为anchor的矩阵中center。为了能够在两个单元格之间建立链接,您需要设置选项remember picturetikzpicture这是我得到的结果:

结果

代码如下:

\documentclass[a4paper, 11pt]{article}

\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}

\begin{document}

\begin{figure}[H]
  \centering
  \begin{tikzpicture}[
    remember picture,
    circ/.style={
      draw,
      circle,
      fill=blue!30,
      inner sep=5pt
    },
    squa/.style={
      draw,
      inner sep=5pt,
      fill=blue!20,
    },
    start chain=1,
    start chain=2
    ] 
    \matrix [matrix of nodes,nodes={anchor=center},row sep = 2em]
    { 
      % Variables
      \node{\tikz{
          \node[on chain=1, circ] (x1) {$x_1$};
          \node[on chain=1, circ] (x2) {$x_2$};
          \node[on chain=1, circ] (x3) {$x_3$};
        }};
      \\
      \node{\tikz{
          % Clauses
          \node[on chain=2, squa] (c1) {$\lnot x_1 \lor \lnot x_2$};
          \node[on chain=2, squa] (c2) {$x_2 \lor \lnot x_3$};
          \node[on chain=2, squa] (c3) {$x_1 \lor x_2$};
          \node[on chain=2, squa] (c4) {$\lnot x_2 \lor x_3$};
          \node[on chain=2, squa] (c5) {$\lnot x_1 \lor x_3$};
        }};
      \\
    };
    % Draw
    \draw[dashed]
         (x1.south) -- (c1.north)
         (x1.south) -- (c5.north)
         (x2.south) -- (c1.north)
         (x2.south) -- (c4.north)
         (x3.south) -- (c2.north);
    \draw[]
         (x1.south) -- (c3.north)
         (x2.south) -- (c2.north)
         (x2.south) -- (c3.north)
         (x3.south) -- (c4.north)
         (x3.south) -- (c5.north);
  \end{tikzpicture}
\end{figure}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:

相关内容