[Tex/LaTex] Cut one side of a rectangle node in TikZ

tikz-pgf

I would like to draw two consecutive rectangle nodes (one is next to the other with an intersected edge). How to cut one side of a rectangle node in TikZ so that the intersected side looks like others?

Consider the example below, I don't want the "duplicated" edge between node 1 and 2.

\begin{tikzpicture}

  \node [style={draw, inner sep=0pt, minimum size=0.5cm}]           (1) at (0, 0)  { $1$ };
  \node [style={draw, inner sep=0pt, minimum size=0.5cm}]           (2) at (0.5, 0)  { $2$ };

\end{tikzpicture}

Best Answer

Update:

Here is a better solution, but this has an issue, in that the three sided node lines can be any color you want as long its black :-). The three sided style defined here is based loosely on How to modify nodes in TikZ to automatically add a line on their top?. The corrected node on the right here was drawn as:

  \node [three sided               ] (1) at (0,  0)  { $1$ };
  \node [draw=red, draw opacity=0.5] (2) at (0.5,0)  { $2$ };

enter image description here

Further Enhancements:

  • Allow for other line colors for the three sided node.

Code:

\documentclass{article}
\usepackage{tikz}

\tikzset{three sided/.style={
        draw=none,
        append after command={
            [shorten <= -0.5\pgflinewidth]
            ([shift={(-1.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north east)
        edge([shift={( 0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west) 
            ([shift={( 0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west)
        edge([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west)            
            ([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west)
        edge([shift={(-1.0\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east)
        }
    }
}

\begin{document} 
\begin{tikzpicture}[thick, inner sep=0pt, minimum size=0.5cm]
  \node [style={draw}] (1) at (0,  0)  { $1$ };
  \node [style={draw=red, draw opacity=0.5}] (2) at (0.5,0)  { $2$ };
\end{tikzpicture}
%
\begin{tikzpicture}[thick, inner sep=0pt, minimum size=0.5cm]
  \node [three sided               ] (1) at (0,  0)  { $1$ };
  \node [draw=red, draw opacity=0.5] (2) at (0.5,0)  { $2$ };
\end{tikzpicture}
\end{document}

You example did not really illustrate the issue, so I have adapted it to more clearly illustrate the issue, and also allows you to see that it is not duplicated in the second version. The two on the left show the problem, and the two on the right should be the desired result:

enter image description here

Note:

  • This is a very manual solution that requires one to use draw=none for the node, followed by a call to \DrawNode to do the actual drawing. There must be some magic postaction that can be applied to have the same effect which woudl simply the usage.
  • Also, I am not sure why you had style={} with the node options.

Code:

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

\newcommand{\DrawNode}[2][]{%
    \draw  [#1]
        ([shift={(-\pgflinewidth,-0.5\pgflinewidth)}]#2.north east) -- 
        ([shift={(0.5\pgflinewidth,-0.5\pgflinewidth)}]#2.north west) -- 
        ([shift={(0.5\pgflinewidth,+0.5\pgflinewidth)}]#2.south west) -- 
        ([shift={(-\pgflinewidth,+0.5\pgflinewidth)}]#2.south east)
        ;
}%

\begin{document} 
\begin{tikzpicture}[thick]
  \node [style={draw, densely dotted, inner sep=0pt, minimum size=0.5cm}] (1) at (0,  0)  { $1$ };
  \node [style={draw=red, draw opacity=0.5, inner sep=0pt, minimum size=0.5cm}] (2) at (0.5,0)  { $2$ };
\end{tikzpicture}
%
\begin{tikzpicture}[thick]
  \node [draw=none, inner sep=0pt, minimum size=0.5cm] (1) at (0,  0)  { $1$ };
  \DrawNode[densely dotted]{1} 
  \node [draw=red, draw opacity=0.5, inner sep=0pt, minimum size=0.5cm] (2) at (0.5,0)  { $2$ };
\end{tikzpicture}

\begin{tikzpicture}[thick]
  \node [style={draw, inner sep=0pt, minimum size=0.5cm}] (1) at (0,  0)  { $1$ };
  \node [style={draw=red, draw opacity=0.5, inner sep=0pt, minimum size=0.5cm}] (2) at (0.5,0)  { $2$ };
\end{tikzpicture}
%
\begin{tikzpicture}[thick]
  \node [draw=none, inner sep=0pt, minimum size=0.5cm] (1) at (0,  0)  { $1$ };
  \DrawNode{1} 
  \node [draw=red, draw opacity=0.5, inner sep=0pt, minimum size=0.5cm] (2) at (0.5,0)  { $2$ };
\end{tikzpicture}
\end{document}