创建无缝 XOR 符号作为节点

创建无缝 XOR 符号作为节点

我想在我的 TikZ 图中有一个 XOR 节点,但找不到任何可用的符号,因此自己做了一个,但我对它不太满意。我使用了如何在 tikz 中将 XOR 门符号绘制为节点?作为基础。

\tikzstyle{XOR} = [draw,circle]

使用方法如下:

 \node [XOR] {\large +};

但是,周围总是有填充+。我希望+与圆圈“连接”。

这是我的 MWE:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,fit,calc,positioning,automata}
\begin{document}

\tikzstyle{XOR} = [draw,circle]
\tikzstyle{line}    = [draw, -latex']

\begin{tikzpicture}[auto,>=latex', scale = 1, transform shape]
    \tikzstyle{line}=[draw, -latex']

    \node (XOR-aa)[XOR] {\large +};


    \node [above of=XOR-aa,node distance=1.5cm,text width=1.5cm,anchor=south,align=center] (bla) {Bla};

    \node [right of=XOR-aa,node distance=3cm,text width=1.5cm,anchor=east,align=center] (Blob) {Blob};

    \node [left of=XOR-aa,node distance=3cm,text width=1.5cm,anchor=west,align=center] (blubb) {Blubb};

    \path[line] (XOR-aa) edge (Blob)
                (bla) edge (XOR-aa)
                (blubb) edge (XOR-aa);
\end{tikzpicture}
\end{document}

答案1

我认为最好的办法是将 定义XOR为使+符号自动位于圆圈内。一种可能性是:

\tikzset{XOR/.style={draw,circle,append after command={
        [shorten >=\pgflinewidth, shorten <=\pgflinewidth,]
        (\tikzlastnode.north) edge (\tikzlastnode.south)
        (\tikzlastnode.east) edge (\tikzlastnode.west)
        }
    }
}

完整示例:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,fit,calc,positioning,automata}
\begin{document}

\tikzset{XOR/.style={draw,circle,append after command={
        [shorten >=\pgflinewidth, shorten <=\pgflinewidth,]
        (\tikzlastnode.north) edge (\tikzlastnode.south)
        (\tikzlastnode.east) edge (\tikzlastnode.west)
        }
    }
}
\tikzset{line/.style={draw, -latex',shorten <=1bp,shorten >=1bp}}

\begin{tikzpicture}[auto]

    \node (XOR-aa)[XOR,scale=1.2] {};


    \node [above of=XOR-aa,node distance=1.5cm,text width=1.5cm,anchor=south,align=center] (bla) {Bla};

    \node [right of=XOR-aa,node distance=3cm,text width=1.5cm,anchor=east,align=center] (Blob) {Blob};

    \node [left of=XOR-aa,node distance=3cm,text width=1.5cm,anchor=west,align=center] (blubb) {Blubb};

    \path[line] (XOR-aa) edge (Blob)
                (bla) edge (XOR-aa)
                (blubb) edge (XOR-aa);
\end{tikzpicture}
\end{document}

请特别注意如何line定义:

\tikzset{line/.style={draw, -latex',shorten <=1bp,shorten >=1bp}}

允许shorten箭头不接触XOR:如果没有这个,出发的箭头可能会与混淆+

结果是:

在此处输入图片描述

该方法可以XOR非常轻松地缩放符号而不会出现问题;例如使用:

\node (XOR-aa)[XOR,scale=2.5] {};

在之前的 MWE 中导致:

在此处输入图片描述

答案2

作为 Claudio 的替代方案XOR style,以下代码提供了xor shape基于forbidden sign形状的代码。

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{positioning}

\makeatletter
\pgfdeclareshape{xor}
{
  \inheritsavedanchors[from=circle] 
  \inheritanchorborder[from=circle]
  \inheritanchor[from=circle]{north}
  \inheritanchor[from=circle]{north west}
  \inheritanchor[from=circle]{north east}
  \inheritanchor[from=circle]{center}
  \inheritanchor[from=circle]{west}
  \inheritanchor[from=circle]{east}
  \inheritanchor[from=circle]{mid}
  \inheritanchor[from=circle]{mid west}
  \inheritanchor[from=circle]{mid east}
  \inheritanchor[from=circle]{base}
  \inheritanchor[from=circle]{base west}
  \inheritanchor[from=circle]{base east}
  \inheritanchor[from=circle]{south}
  \inheritanchor[from=circle]{south west}
  \inheritanchor[from=circle]{south east}
  \inheritbackgroundpath[from=circle]
  \foregroundpath{
    \centerpoint%
    \pgf@xc=\pgf@x%
    \pgf@yc=\pgf@y%
    \pgfutil@tempdima=\radius%
    \pgfmathsetlength{\pgf@xb}{\pgfkeysvalueof{/pgf/outer xsep}}%  
    \pgfmathsetlength{\pgf@yb}{\pgfkeysvalueof{/pgf/outer ysep}}%  
    \ifdim\pgf@xb<\pgf@yb%
      \advance\pgfutil@tempdima by-\pgf@yb%
    \else%
      \advance\pgfutil@tempdima by-\pgf@xb%
    \fi%
    \pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{\pgfutil@tempdima}{0pt}}}
    \pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{-\pgfutil@tempdima}{0pt}}}
    \pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{0pt}{-\pgfutil@tempdima}}}
    \pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{0pt}{\pgfutil@tempdima}}}
    \pgfsetarrowsstart{}
    \pgfsetarrowsend{}
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}
\node[xor, draw=red, fill=red!30] (A) {};
\node[xor, draw, fill=green!30, right=1cm of A, minimum size=5mm, line width=.5mm] (B) {};
\draw (A)--(B);
\draw[<-] (A)--++(90:1cm) node[above](bla) {Bla};
\draw[->] (bla)--(B);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容