我正在使用 tikz 矩阵来生成阴影单元格的形状。我使用以下代码来生成我在此示例中寻找的形状。它只是一个具有一些定义填充和图案填充的矩阵。
\documentclass[11pt]{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{matrix,patterns,shadings,backgrounds}
\pgfdeclarelayer{myback}
\pgfsetlayers{myback,background,main}
\tikzset{myfillcolor/.style = {draw,fill=#1}}%
\NewDocumentCommand{\highlight}{O{black!20} m m}{%
\draw[myfillcolor=#1] (#2.north west)rectangle (#3.south east);
}
\NewDocumentCommand{\fillpatternh}{O{horizontal lines} O{black!20} m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west)rectangle (#4.south east);
}
\NewDocumentCommand{\fillpatternch}{O{crosshatch} O{black!20} m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west)rectangle (#4.south east);
}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\matrix (m)[matrix of nodes, style={nodes={minimum width=4em}}, minimum height=2em, ampersand replacement =\&]% row sep=-\pgflinewidth, column sep=-\pgflinewidth,
{
{} \& {} \& {} \\
{} \& {} \& {} \\
};
\begin{pgfonlayer}{background}
\fillpatternh{m-1-1}{m-1-1}%
\highlight{m-2-1}{m-2-1}%
\fillpatternch{m-1-2}{m-2-3}
\highlight{m-1-2}{m-1-2}
\end{pgfonlayer}
\end{tikzpicture}
\end{figure}
\end{document}
但是,我无法生成这些具有轮廓的“L”形。我所有的示例都需要具有矩形轮廓的重叠或多个形状。有人能建议一种生成图片中形状类型的方法吗?
谢谢
答案1
根据您的代码,声明一个更大的区域matrix
并填充相应的区域。
\draw[pattern=north west lines, pattern color=black!20] (m-4-2.north west) -|
(m-5-3.north west) -| (m-6-3.south east)-| (m-5-2.south east) -| cycle;
关于您的代码:如果您添加nodes in empty nodes
,则不需要{}
在每个矩阵元素内添加。
\documentclass[11pt]{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{matrix,patterns,shadings,backgrounds}
\pgfdeclarelayer{myback}
\pgfsetlayers{myback,background,main}
\tikzset{myfillcolor/.style = {draw,fill=#1}}%
\NewDocumentCommand{\highlight}{O{black!20} m m}{%
\draw[myfillcolor=#1] (#2.north west)rectangle (#3.south east);
}
\NewDocumentCommand{\fillpatternh}{O{horizontal lines} O{black!20} m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west)rectangle (#4.south east);
}
\NewDocumentCommand{\fillpatternch}{O{crosshatch} O{black!20} m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west) rectangle (#4.south east);
}
\NewDocumentCommand{\fillpatternw}{O{north west lines} O{black!20} m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west)rectangle (#4.south east);
}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\matrix (m)[matrix of nodes, style={nodes={minimum width=4em}}, minimum height=2em, ampersand replacement =\&]% row sep=-\pgflinewidth, column sep=-\pgflinewidth,
{
{} \& {} \& {} \\
{} \& {} \& {} \\
};
\begin{pgfonlayer}{background}
\fillpatternh{m-1-1}{m-1-1}%
\highlight{m-2-1}{m-2-1}%
\fillpatternch{m-1-2}{m-2-3}
\highlight{m-1-2}{m-1-2}
\end{pgfonlayer}
\end{tikzpicture}
\begin{tikzpicture}
\matrix (m)[matrix of nodes, nodes in empty cells, style={nodes={minimum width=4em}}, minimum height=2em, ampersand replacement =\&]% row sep=-\pgflinewidth, column sep=-\pgflinewidth,
{
\& \& \\
\& \& \\
\& \& \\
\& \& \\
\& \& \\
\& \& \\
\& \& \\
\& \& \\
};
\begin{pgfonlayer}{background}
\highlight{m-6-2}{m-6-2}%
\highlight{m-7-3}{m-7-3}
\fillpatternh{m-1-1}{m-3-2}%
\fillpatternh{m-5-1}{m-7-1}%
\fillpatternw{m-7-2}{m-8-2}%
\draw[pattern=north west lines, pattern color=black!20] (m-4-2.north west) -| (m-5-3.north west) -| (m-6-3.south east)-| (m-5-2.south east) -| cycle;
\end{pgfonlayer}
\end{tikzpicture}
\end{figure}
\end{document}
更新:
如果这些奇怪的图形总是相似的,你可以声明一个有四个参数的命令,左上角节点,中间顶部,右下角,中间下角。
\NewDocumentCommand{\fillfourpatternw}{O{north west lines} O{black!20} m m m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west) -| (#4.north west) -| (#5.south east) -| (#6.south east) -| cycle;
}
在这个特定例子中将是:
\fillfourpatternw{m-4-2}{m-5-3}{m-6-3}{m-5-2}
答案2
可能有更聪明的方法可以做到这一点,但这是我的方法:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes, style={nodes={minimum width=4em}}, minimum height=2em] {
1-1 & 1-2 & 1-3 & 1-4 & 1-5 \\
2-1 & 2-2 & 2-3 & 2-4 & 2-5 \\
3-1 & 3-2 & 3-3 & 3-4 & 3-5 \\
};
\begin{scope}[on background layer]
\path [fill=black!10]
(m-1-1.north west) -- (m-2-1.south west)
-- (m-2-2.south east) -- (m-3-2.south east)
-- (m-3-5.south east) -- (m-2-5.north east)
-- (m-2-3.north east) -- (m-1-3.north east)
-- cycle;
\draw (m-1-1.north west) -- (m-2-1.south west)
-- (m-2-2.south east) -- (m-3-2.south east)
-- (m-3-5.south east) -- (m-2-5.north east)
-- (m-2-3.north east) -- (m-1-3.north east)
-- cycle;
\end{scope}
\end{tikzpicture}
\end{document}