我必须绘制下图。但我无法将线条放在矩形框内,并按照框内所写的颜色绘制线条。我还想在这里添加文字,也许是颜色名称,但不要有任何线条穿过文本(因为我尝试在红色和黄色的情况下显示它)。 我的MWE
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (1,3);
\shade[top color=yellow, bottom color=black] (0,0) rectangle (2,-1);
\filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1);
\draw (0,0) rectangle (3,1);
\draw (2,0) rectangle (5,1);
\draw (2,0) rectangle (3,3);
\end{tikzpicture}
\end{document}
答案1
轻松使用patterns
图书馆(pgfmanual 第 60 节)。
\documentclass[margin=3.14mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,patterns}
\begin{document}
\begin{tikzpicture}
\shade[top color=yellow, bottom color=black] (0,0) rectangle (2,-1);
\filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1);
\draw[pattern=north east lines,pattern color=red] (0,0) rectangle (1,3);
\draw[pattern=north west lines,pattern color=blue] (0,0) rectangle (3,1);
\draw[pattern=vertical lines] (2,0) rectangle (5,1);
\draw[pattern=horizontal lines,pattern color=yellow] (2,0) rectangle (3,3);
\end{tikzpicture}
\end{document}
编辑:带注释。只需修改样式即可mynode
更改行为。我还将边界设置为与线条相同的颜色。
\documentclass[margin=3.14mm]{standalone}
\usepackage{tikz}
\usepackage{contour}
\contourlength{1pt}
\tikzset{mynode/.style args={#1 | #2}{midway,%
node contents={\contour{white}{\textcolor{#1}{#2}}},%
font=\small,inner sep=0pt}}
\usetikzlibrary{calc,patterns}
\begin{document}
\begin{tikzpicture}
\shade[top color=yellow, bottom color=black] (0,0) rectangle (2,-1);
\filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1);
\draw[pattern=north east lines,pattern color=red,draw=red] (0,0) rectangle (1,3)
node[mynode=red | hello];
\draw[pattern=north west lines,pattern color=blue,draw=blue] (0,0) rectangle (3,1)
node[mynode=blue | ducks];
\draw[pattern=vertical lines] (2,0) rectangle (5,1)
node[mynode=black | hello];
\draw[pattern=horizontal lines,pattern color=yellow,draw=yellow] (2,0) rectangle (3,3)
node[mynode=yellow | world];
\end{tikzpicture}
\end{document}