![如何与遮阳和独立](https://linux22.com/image/342223/%E5%A6%82%E4%BD%95%E4%B8%8E%E9%81%AE%E9%98%B3%E5%92%8C%E7%8B%AC%E7%AB%8B.png)
我想画一个像图 1 这样的图,但是我的 Latex 给出了图 2。我认为问题在于:
\shade[top color=blue!20] (-1,-1.2) rectangle (3,3);
以下是 Latex 代码:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\draw[decoration={aspect=0.3, segment length=1.5mm, amplitude=2.4mm,coil},decorate] (1,5) -- (1,3.3);
\fill [pattern = north east lines] (-0.5,5) rectangle (2.5,5.2);
\draw[thick] (-0.5,5) -- (2.5,5);
%\filldraw[fill=blue!20, draw=blue!60] (-1,-1.2) rectangle (3,3);
\draw[line width=0.5pt] (1,3.3) -- (1,2.5);
\draw[line width=1pt] (-1,3) -- (3,3);
\draw[->] (1,0.2) -- (1,-0.8);
\node[draw=none,right=.1cm] at (1.3,4.25) (a) {$K$};
\node[draw=none,right=.1cm] at (0,-0.5) (a) {$f(t)$};
\draw[line width=6pt] (1,2.5) -- (1,-0.2);
\shade[top color=blue!20] (-1,-1.2) rectangle (3,3);
\end{tikzpicture}
\end{document}
输出不正确!
答案1
绘制 TikZ 图片时,您应该始终从背景中的事物开始,然后添加其前面的物体。请看以下示例:
\draw[red,fill] (0,0) circle (0.1);
\draw[blue,fill] (-1,-1) rectangle (1,1);
这样做的结果将只是矩形,因为圆形是在后面矩形。
您的图形也出现了同样的情况:通过将\shade
命令放在代码底部(即作为最后一个命令),此阴影矩形将显示在前景中,而您实际上希望它显示在背景中。作为解决方案,只需将命令移动到您的开头tikzpicture
:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
% First draw the background
\shade[top color=blue!20] (-1,-1.2) rectangle (3,3);
\draw[decoration={aspect=0.3, segment length=1.5mm, amplitude=2.4mm,coil},decorate] (1,5) -- (1,3.3);
\fill [pattern = north east lines] (-0.5,5) rectangle (2.5,5.2);
\draw[thick] (-0.5,5) -- (2.5,5);
%\filldraw[fill=blue!20, draw=blue!60] (-1,-1.2) rectangle (3,3);
\draw[line width=0.5pt] (1,3.3) -- (1,2.5);
\draw[line width=1pt] (-1,3) -- (3,3);
\draw[->] (1,0.2) -- (1,-0.8);
\node[draw=none,right=.1cm] at (1.3,4.25) (a) {$K$};
\node[draw=none,right=.1cm] at (0,-0.5) (a) {$f(t)$};
\draw[line width=6pt] (1,2.5) -- (1,-0.2);
\end{tikzpicture}
\end{document}
您将获得所需的结果: