带十字符号的参数化圆节点

带十字符号的参数化圆节点

我正在尝试获得这样的结果:

在此处输入图片描述

使用参数化节点,该节点有 4 个参数,用于 4 个圆形部分。我设法实现了以下代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\tikzset{add/.style n args={4}{
    draw,
    circle,
    minimum width=6mm,
    path picture={
        \draw
            (path picture bounding box.south east) -- (path picture bounding box.north west)
            (path picture bounding box.south west) -- (path picture bounding box.north east);
        \node at ($(path picture bounding box.center)+(0,0.17)$)        {\tiny #1};
        \node at ($(path picture bounding box.center)+(0.17,0)$)        {\tiny #2};
        \node at ($(path picture bounding box.center)+(0,-0.17)$)       {\tiny #3};
        \node at ($(path picture bounding box.center)+(-0.17,0)$)       {\tiny #4};
        }
    }
}

\begin{document}

\begin{tikzpicture}[node distance=0.4cm]
    \node[add={1}{2}{3}{4}] (add1)  {}; 
    \node[add={1}{2}{3}{4},above=of add1]   (add2)  {}; 
    \begin{scope}[xshift=1cm,color=red]
        \node[add={1}{2}{3}{4}] (add3)  {}; 
        \node[add={1}{2}{3}{4}]  at (0,1) (add4)    {}; 
    \end{scope}
\end{tikzpicture}

\end{document}

输出:

在此处输入图片描述

可以看出,当一个节点相对于另一个节点定位时会出现问题。有什么办法可以解决这个问题吗?

答案1

解决方案是添加anchor=center/.style。

谢谢马克!

    \documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}


\tikzset{add/.style n args={4}{
    draw,
    circle,
    minimum width=6mm,
    path picture={
        \draw
            (path picture bounding box.south east) -- (path picture bounding box.north west)
            (path picture bounding box.south west) -- (path picture bounding box.north east);
        \node[anchor=center] at ($(path picture bounding box.center)+(0,0.17)$)     {\tiny #1};
        \node[anchor=center] at ($(path picture bounding box.center)+(0.17,0)$)     {\tiny #2};
        \node[anchor=center] at ($(path picture bounding box.center)+(0,-0.17)$)    {\tiny #3};
        \node[anchor=center] at ($(path picture bounding box.center)+(-0.17,0)$)    {\tiny #4};
        }
    }
}

\begin{document}

\begin{tikzpicture}[node distance=1cm]
    \node[add={1}{2}{3}{4}] (add1)  {}; 
    \node[add={1}{2}{3}{4},above=of add1]   (add2)  {}; 
    \begin{scope}[xshift=1cm,color=red]
        \node[add={1}{2}{3}{4}] (add3)  {}; 
        \node[add={1}{2}{3}{4}]  at (0,1.3) (add4)  {}; 
    \end{scope}
\end{tikzpicture}

\end{document}

答案2

这是一个非 tikz 方法:

\documentclass{article}
\usepackage{graphicx}
\usepackage{stackengine}
\begin{document}
\stackinset{c}{}      {c}{ 2.3ex}{N}{%
\stackinset{c}{}      {c}{-2.3ex}{S}{%
\stackinset{c}{ 2.3ex}{c}{}      {E}{%
\stackinset{c}{-2.3ex}{c}{}      {W}{%
\scalebox{3.5}{$\bigotimes$}%
}}}}
\end{document}

在此处输入图片描述

相关内容