拟合节点中的自定义点图案

拟合节点中的自定义点图案

从拟合节点的北坐标到南坐标画一条虚线相当容易:

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

在此处输入图片描述

如果我可以通过选择点的大小和间距来适应自定义点图案,那就更好了,尤其是如果这是预定义的样式。此样式可以是命令的一部分,\node[fit...]而不需要\draw命令。

答案1

该样式dotted pattern是使用以下代码定义的tikz 中的虚线与圆点

您可以选择颜色、点的半径及其间距(第一张图片)或者应用定义为默认的样式(第二张图片)。

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}

相关内容