在节点定义内连接节点的锚点

在节点定义内连接节点的锚点

我想用一条线连接一个节点的锚点(北 + 南,东 + 西)。这是一项简单的任务,但我需要这样做在节点定义本身内,而不是像下面我的例子那样通过额外的绘制命令。我认为这应该是可能的,例如使用后置操作或装饰,但我不知道如何在节点上使用它们。有什么想法吗?

最大能量损失 #1

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\node[draw, minimum width = 1cm, minimum height = 0.5cm] (N) at (3,0) {};
\draw (0,0) -- (N) -- (6,0);

% I want to put the following within the node defintion
\draw (N.west) -- (N.east);

\end{tikzpicture}
\end{document}

在此处输入图片描述

最大能量损失 #2

想象一下,我只想通过绘制节点来绘制这个多边形。

\documentclass{article}
\usepackage{tikz}

\tikzset{squarebox/.style={minimum width = 1cm, minimum height = 1cm}}

\begin{document}
\begin{tikzpicture}

\node[squarebox] (WE) at (2cm,3cm) {}; % West-East-Node
\node[squarebox] (NE) at (3cm,3cm) {}; % North-East-Node
\node[squarebox] (NS) at (3cm,2cm) {}; % North-South-Node
\node[squarebox] (ES) at (3cm,1cm) {}; % East-South-Node
\node[squarebox] (EW) at (2cm,1cm) {}; % East-East-Node
\node[squarebox] (SW) at (1cm,1cm) {}; % South-West-Node
\node[squarebox] (SN) at (1cm,2cm) {}; % South-North-Node
\node[squarebox] (WN) at (1cm,3cm) {}; % West-North-Node

% I want to put the following within the node defintions
\draw (WE.east)  -- (WE.west);
\draw (NE.west)  -- (NE.south);
\draw (NS.north) -- (NS.south);
\draw (ES.north) -- (ES.west);
\draw (EW.east)  -- (EW.west);
\draw (SW.east)  -- (SW.north);
\draw (SN.south) -- (SN.north);
\draw (WN.south) -- (WN.east);


\end{tikzpicture}
\end{document}

在此处输入图片描述

备注#1:我其实并不想画框。只想画线,然后使用锚点。

备注#2:我知道我可以用 声明形状\pgfdeclareshape{},这将是我的下一次尝试。但这似乎有点过头了。

非常感谢!

答案1

您似乎正在寻找append after command

\documentclass{article}
\usepackage{tikz}

\tikzset{squarebox/.style={minimum width = 1cm, minimum height = 1cm}}

\begin{document}
\begin{tikzpicture}[line cap=round,%
    en/.style={append after command={(\tikzlastnode.east) edge  (\tikzlastnode.north)}},
    ew/.style={append after command={(\tikzlastnode.east) edge (\tikzlastnode.west)}},
    es/.style={append after command={(\tikzlastnode.east) edge (\tikzlastnode.south)}},
    sw/.style={append after command={(\tikzlastnode.south) edge (\tikzlastnode.west)}},
    ns/.style={append after command={(\tikzlastnode.north) edge (\tikzlastnode.south)}},
    nw/.style={append after command={(\tikzlastnode.north) edge (\tikzlastnode.west)}},]

\node[squarebox,ew] (WE) at (2cm,3cm) {}; % West-East-Node
\node[squarebox,sw] (NE) at (3cm,3cm) {}; % North-East-Node
\node[squarebox,ns] (NS) at (3cm,2cm) {}; % North-South-Node
\node[squarebox,nw] (ES) at (3cm,1cm) {}; % East-South-Node
\node[squarebox,ew] (EW) at (2cm,1cm) {}; % East-East-Node
\node[squarebox,en] (SW) at (1cm,1cm) {}; % South-West-Node
\node[squarebox,ns] (SN) at (1cm,2cm) {}; % South-North-Node
\node[squarebox,es] (WN) at (1cm,3cm) {}; % West-North-Node



\end{tikzpicture}
\end{document}

在此处输入图片描述

或者不使用缩写(并且outer sep=0pt有更好的连接)。

\documentclass{article}
\usepackage{tikz}

\tikzset{squarebox/.style={minimum width = 1cm, minimum height = 1cm,outer sep=0pt}}

\begin{document}
\begin{tikzpicture}[line cap=round,%
    node edge/.style 2 args={%
      append after command={(\tikzlastnode.#1) edge (\tikzlastnode.#2)},
        }]

\node[squarebox,node edge={east}{west}] (WE) at (2cm,3cm) {}; % West-East-Node
\node[squarebox,node edge={south}{west}] (NE) at (3cm,3cm) {}; % North-East-Node
\node[squarebox,node edge={north}{south}] (NS) at (3cm,2cm) {}; % North-South-Node
\node[squarebox,node edge={north}{west}] (ES) at (3cm,1cm) {}; % East-South-Node
\node[squarebox,node edge={east}{west}] (EW) at (2cm,1cm) {}; % East-East-Node
\node[squarebox,node edge={east}{north}] (SW) at (1cm,1cm) {}; % South-West-Node
\node[squarebox,node edge={north}{south}] (SN) at (1cm,2cm) {}; % South-North-Node
\node[squarebox,node edge={east}{south}] (WN) at (1cm,3cm) {}; % West-North-Node
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容