0

I would like to connect the anchors of a node (north + south, east + west) by a line. An easy task, but I need to do it within the node definition itself, not by an additional drawing command like in my example below. I think it should be possible, e.g. with postactions or decorations, but I am stuck on how to use them on nodes. Any ideas?

MWE #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}

enter image description here

MWE #2

Imagine I want to draw this polygon only by drawing nodes.

\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}

enter image description here

Remark #1: I actually don't want to draw the box. Just the line, and use the anchors.

Remark #2: I know I can declare shapes with \pgfdeclareshape{}, which would be my next try. But it appeas to be overkill.

Thank you very much!

3
  • your requirement is not quite clear to me - maybe a handrawn sketch would clarify what is the expected output
    – js bibra
    Commented Nov 2, 2020 at 12:11
  • @jsbibra the expected output is exactly as shown, but I would like to omit the line \draw (N.west) -- (N.east); and make part of the node definition instead. Imagine that I finally want to connect line segments, which are actually nodes with a fixed width and height, over their anchors. Commented Nov 2, 2020 at 13:19
  • @jsbibra The second example should make it clear. Commented Nov 2, 2020 at 13:32

1 Answer 1

2

You seem to be looking for 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}

enter image description here

Or without abbreviations (and outer sep=0pt to have nicer joins).

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .