如何在 Tikz 中定义不同类型的节点

如何在 Tikz 中定义不同类型的节点

我已经为节点定义了一个整体样式,但从示例中可以看出,我还需要不同的样式。如果每个节点有两个声明,一个蓝色实线,一个紫色虚线,那就太好了。我确信这是可能的,但我还没能找到如何实现。

\documentclass[border=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}

\begin{document}


\begin{tikzpicture}[node distance=1.5cm,
    every node/.style={fill=white, font=\sffamily}, align=center]

\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
 4      &   & 8     &   & 6     &   & 0     & (-)  \\
        & 3  &      & -1 &      &   &       & 4\\
 5      &   & 6     &   & 3     &   & 0     &  (-)  \\
        & 4 &       &   &       &  &    &   1  \\
};

\draw (m-1-1.north west) rectangle (m-4-8.south east);



% Lineas horizontales
\foreach \i in {2,4} {
  \draw (m-\i-1.south west) -- (m-\i-8.south east);
}

% Lineas verticales
\foreach \j in {2,4,6,8} {
  \draw (m-1-\j.north east) -- (m-4-\j.south east);
}

\node[draw=blue,circle,fit=(m-1-5)(m-2-6),inner sep = -19pt] {3};

\node[draw=blue,circle,fit=(m-3-3)(m-4-4),inner sep = -19pt] {5};
\node[draw=blue,circle,fit=(m-3-5)(m-4-6),inner sep = -19pt] {7};



  \path[draw,purple, dashed,very thick] (0,0)++(-1.8,0.9) node[circle,draw=purple!80, inner sep=1pt] (6) {+}
    -- ++(0,-1) node[circle,draw=purple!80, inner sep=1pt] (5) {-}
    -- ++(2, 0) node[circle,draw=purple!80, inner sep=1pt] (4) {+}
    -- ++(0,1) node[circle,draw=purple!80, inner sep=1pt] (3) {-}
    -- cycle;

\end{tikzpicture}

\end{document}

示例图片

在此处输入图片描述

答案1

正如 Daniel N 所建议的,您可以使用 定义您的样式\tikzset

我不清楚您想要数学节点还是无衬线文本节点。

请注意,TikZ 矩阵本身就是一个节点,因此要绘制边框只需添加draw其选项(\draw (m-1-1.north west) rectangle (m-4-8.south east);那么就没用了)。

还要注意 for 循环,因为在 MWE 中你画了两次右线和底线。

\documentclass[border=10pt]{standalone}
%\usepackage[utf8]{inputenc}% No more needed in recent TeX distributions
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\tikzset{
    bluenode/.style={
        draw=blue,circle
        },
    rednode/.style={
        circle,draw=purple!80, 
        inner sep=1pt,
        fill=white,
        },
}

\begin{document}
\begin{tikzpicture}
\matrix(m) [draw, inner sep = 0pt,
    matrix of math nodes, nodes in empty cells,
    nodes={text height=1.5ex, text depth=.25ex, minimum size=1cm, inner sep=0pt}
    ]
{
 4      &   & 8     &   & 6     &   & 0     & (-)  \\
        & 3  &      & -1 &      &  3 &       & 4\\
 5      &   & 6     &   & 3     &   & 0     &  (-)  \\
        & 4 &       &  5 &       &  7 &    &   1  \\
};

\node[bluenode] at (m-2-6) {\phantom{3}};
\node[bluenode] at (m-4-4) {\phantom{5}};
\node[bluenode] at (m-4-6) {\phantom{7}};

% Lineas horizontales
\foreach \i in {2} {
  \draw (m-\i-1.south west) -- (m-\i-8.south east);
}

% Lineas verticales
\foreach \j in {2,4,6} {
  \draw (m-1-\j.north east) -- (m-4-\j.south east);
}

\path[draw,purple, dashed,very thick] (-1.8,0.9) node[rednode] (6) {$+$}
    -- ++(0,-1) node[rednode] (5) {$-$}
    -- ++(2,0) node[rednode] (4) {$+$}
    -- ++(0,1) node[rednode] (3) {$-$}
    -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容