Something along the lines of Peter Grill's updated version I have just noticed …
The style rect
takes four arguments that define the styles of the right, top, left and bottom line.
The style rect'
is somehow similar but it draws the whole line. I used rect'
in the second column to only draw over the already drawn line of the node itself. (This may result in rendering issues depending on the viewer and the zoom level. Mathematically it should draw over the whole line.)
The style rect''
works quite differently as it shortens every line automatically so that they don't overlap, this can be avoided by using the dontshortenme
style or the usual shorten ≶
styles. (Additionnaly styles like dontshortenme >
and dontshortenme <
may be added.)
The style rect
has the advantage that every line can have different styles.
The style rect'
has the advantage that the normal line would be left alone so that the dot-algorithm isn't used discontinuously. (Compare the dotted lines in the first row.)
The next step would be to check if connect edges have the same style and connect them.
Code
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{etoolbox}
\makeatletter
\tikzset{
rect/.style n args={4}{
draw=none,
rectangle,
append after command={
\pgfextra{%
\pgfkeysgetvalue{/pgf/outer xsep}{\oxsep}
\pgfkeysgetvalue{/pgf/outer ysep}{\oysep}
\def\arg@one{#1}
\def\arg@two{#2}
\def\arg@three{#3}
\def\arg@four{#4}
\begin{pgfinterruptpath}
\ifx\\#1\\\else
\draw[draw,#1] ([xshift=-\oxsep,yshift=+\pgflinewidth]\tikzlastnode.south east) edge ([xshift=-\oxsep,yshift=0\ifx\arg@two\@empty-\pgflinewidth\fi]\tikzlastnode.north east);
\fi\ifx\\#2\\\else
\draw[draw,#2] ([xshift=-\pgflinewidth,yshift=-\oysep]\tikzlastnode.north east) edge ([xshift=0\ifx\arg@three\@empty+\pgflinewidth\fi,yshift=-\oysep]\tikzlastnode.north west);
\fi\ifx\\#3\\\else
\draw[draw,#3] ([xshift=\oxsep,yshift=0-\pgflinewidth]\tikzlastnode.north west) edge ([xshift=\oxsep,yshift=0\ifx\arg@four\@empty+\pgflinewidth\fi]\tikzlastnode.south west);
\fi\ifx\\#4\\\else
\draw[draw,#4] ([xshift=0+\pgflinewidth,yshift=\oysep]\tikzlastnode.south west) edge ([xshift=0\ifx\arg@one\@empty-\pgflinewidth\fi,yshift=\oysep]\tikzlastnode.south east);
\fi
\end{pgfinterruptpath}
}
}
},
rect'/.style n args={4}{
rectangle,
append after command={
\pgfextra{%
\pgfkeysgetvalue{/pgf/outer xsep}{\oxsep}
\pgfkeysgetvalue{/pgf/outer ysep}{\oysep}
\begin{pgfinterruptpath}
\ifx\\#1\\\else
\draw[draw,#1] ([xshift=-\oxsep,yshift=0]\tikzlastnode.south east) edge ([xshift=-\oxsep,yshift=0]\tikzlastnode.north east);
\fi\ifx\\#2\\\else
\draw[draw,#2] ([xshift=-\pgflinewidth,yshift=-\oysep]\tikzlastnode.north east) edge ([xshift=0+\pgflinewidth,yshift=-\oysep]\tikzlastnode.north west);
\fi\ifx\\#3\\\else
\draw[draw,#3] ([xshift=\oxsep,yshift=0-\pgflinewidth]\tikzlastnode.north west) edge ([xshift=\oxsep,yshift=0+\pgflinewidth]\tikzlastnode.south west);
\fi\ifx\\#4\\\else
\draw[draw,#4] ([xshift=0+\pgflinewidth,yshift=\oysep]\tikzlastnode.south west) edge ([xshift=0-\pgflinewidth,yshift=\oysep]\tikzlastnode.south east);
\fi
\end{pgfinterruptpath}
}
}
},
dontshortenme/.style={
shorten >=0pt,
shorten <=0pt
},
rect''/.style n args={4}{
draw=none,
rectangle,
append after command={
\pgfextra{%
\pgfkeysgetvalue{/pgf/outer xsep}{\oxsep}
\pgfkeysgetvalue{/pgf/outer ysep}{\oysep}
\def\my@path{\path[shorten >=\pgflinewidth,shorten <=\pgflinewidth] ([xshift=-\oxsep]\tikzlastnode.south east) edge}
\def\arg@{#1}
\ifx\arg@\@empty
\def\arg@{draw=none}
\fi
\eappto\my@path{[\arg@] }
\appto\my@path{ ([xshift=-\oxsep]\tikzlastnode.north east)
([yshift=-\oysep]\tikzlastnode.north east) edge }
\def\arg@{#2}
\ifx\arg@\@empty
\def\arg@{draw=none}
\fi
\eappto\my@path{[\arg@] }
\appto\my@path{ ([yshift=-\oysep]\tikzlastnode.north west)
([xshift=\oxsep] \tikzlastnode.north west) edge }
\def\arg@{#3}
\ifx\arg@\@empty
\def\arg@{draw=none}
\fi
\eappto\my@path{[\arg@] }
\appto\my@path{ ([xshift=\oxsep]\tikzlastnode.south west)
([yshift=\oysep] \tikzlastnode.south west) edge }
\def\arg@{#4}
\ifx\arg@\@empty
\def\arg@{draw=none}
\fi
\eappto\my@path{[\arg@] }
\appto\my@path{ ([yshift=\oysep]\tikzlastnode.south east);}
\begin{pgfinterruptpath}
\my@path
\end{pgfinterruptpath}
}
}
}
}
\makeatother
\tikzset{
every node/.style={
inner sep=0pt,
minimum size=.5cm
}
}
\begin{document}
\begin{tikzpicture}[thick]
\node [draw, densely dotted,] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
%
\begin{tikzpicture}[thick]
\node [draw=black, densely dotted, rect'={solid,white}{}{}{}] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
%
\begin{tikzpicture}[thick]
\node [rect={}{densely dotted}{densely dotted}{densely dotted}] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
%
\begin{tikzpicture}[thick]
\node [rect={}{solid,draw=blue}{solid,draw=green}{solid,draw=orange}] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
%
\begin{tikzpicture}[thick]
\node [rect''={}{densely dashed,draw=blue}{draw=green,dontshortenme}{densely dotted,draw=orange}] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
\begin{tikzpicture}[thick]
\node [draw] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
%
\begin{tikzpicture}[thick]
\node [draw=black,rect'={white}{}{}{}] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
%
\begin{tikzpicture}[thick]
\node [rect={}{solid}{solid}{solid}] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
%
\begin{tikzpicture}[thick]
\node [rect={}{solid,draw=blue}{solid,draw=green,shorten >=\pgflinewidth}{solid,draw=orange,shorten <=-\pgflinewidth}] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
\end{document}
Output

\begin{tikzpicture}[thick]
\node [rect={}{solid,draw=blue}{solid,draw=green}{solid,draw=orange}] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
Original

Maybe preferred?
\begin{tikzpicture}[thick]
\node [rect={}{solid,draw=blue}{solid,draw=green,shorten >=\pgflinewidth}{solid,draw=orange,shorten <=-\pgflinewidth}] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}

Other line ends, new rect''
style
\begin{tikzpicture}[thick]
\node [rect''={}{densely dashed,draw=blue}{draw=green,dontshortenme}{densely dotted,draw=orange}] (1) at (0, 0) { $1$ };
\node [draw=red, draw opacity=0.5] (2) at (0.5,0) { $2$ };
\end{tikzpicture}
