Custom dot pattern in fitted node

fittikz-matrixtikz-pgf

It is fairly easy to draw a dotted line from the north to the south coordinate of a fitted node:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{fit,shapes.geometric,calc,matrix,math}

\begin{document}

\begin{tikzpicture}
    \matrix(m) [matrix of nodes, row sep=1ex, column sep=1ex, nodes in empty cells, nodes={draw, shape=rectangle,minimum height=5ex, anchor=center, minimum width=5ex},ampersand replacement=\&] {
            a  \&   1    \&   x  \&  o \&     2     \\
            b  \&        \&      \&    \&     3     \\
            c  \&        \&      \&    \&     y     \\
            d  \&        \&      \&    \&     4     \\
            e  \&   5    \&   v  \&  p \&     w     \\
    };
    \node[fit= (m-2-2.north west) (m-4-4.south east), draw=red,inner sep=0ex] (x)  {};
    \draw[loosely dotted] (x.north) -- (x.south);
\end{tikzpicture}

\end{document}

enter image description here

It would be much better if I could fit a custom dot pattern by choosing the size and spacing of the dots especially if this was predefined as a style. This style could be
part of the \node[fit...] command instead of requiring a \draw command.

Best Answer

The style dotted pattern was defined using the code from dotted-lines-in-tikz-with-round-dots

You can choose the color, the radius of the dots and their separation (first picture) or the style defined as default will be applied (second picture).

b

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{fit,shapes.geometric,calc,matrix,math}
\usetikzlibrary{decorations.markings}


%%https://tex.stackexchange.com/questions/101262/dotted-lines-in-tikz-with-round-dots
\pgfkeys{/tikz/.cd,
    circle color/.initial=black,
    circle color/.get=\circlecolor,
    circle color/.store in=\circlecolor,
}
    
\tikzset{dotted pattern/.style args={#1 and #2}{%
        postaction=decorate,
        decoration={
            markings,
            mark=
            between positions 0 and 1 step #2
            with
            {\fill[radius=#1,\circlecolor] (0,0) circle;}
        }
    },
    dotted pattern/.default={0.5pt and 1mm},
}
    
\begin{document}
    
\begin{tikzpicture}
    \matrix(m) [matrix of nodes, row sep=1ex, column sep=1ex, nodes in empty cells, nodes={draw, shape=rectangle,minimum height=5ex, anchor=center, minimum width=5ex},ampersand replacement=\&] {
        a  \&   1    \&   x  \&  o \&     2     \\
        b  \&        \&      \&    \&     3     \\
        c  \&        \&      \&    \&     y     \\
        d  \&        \&      \&    \&     4     \\
        e  \&   5    \&   v  \&  p \&     w     \\
    };
    \node[fit= (m-2-2.north west) (m-4-4.south east), draw=red,inner sep=0ex] (x)  {};
    \path [circle color=red, dotted pattern=1.2pt and 2mm]  (x.north) -- (x.south); 
\end{tikzpicture}

\bigskip
    
\begin{tikzpicture}
    \matrix(m) [matrix of nodes, row sep=1ex, column sep=1ex, nodes in empty cells, nodes={draw, shape=rectangle,minimum height=5ex, anchor=center, minimum width=5ex},ampersand replacement=\&] {
        a  \&   1    \&   x  \&  o \&     2     \\
        b  \&        \&      \&    \&     3     \\
        c  \&        \&      \&    \&     y     \\
        d  \&        \&      \&    \&     4     \\
        e  \&   5    \&   v  \&  p \&     w     \\
    };
    \node[fit= (m-2-2.north west) (m-4-4.south east), draw=red,inner sep=0ex] (x)  {};
    \path [dotted pattern]  (x.north) -- (x.south); 
\end{tikzpicture}
    
\end{document}