我想用图案填充我创建的两条路径之间。当我这样做时,填充始终是黑色。我想要其他颜色/图案。
我的 MWE 是:
\documentclass[a4paper,12pt,twoside]{book}
\usepackage{tikz}
\usepackage{circuitikz}
\usepackage{pstricks}
\let\clipbox\relax
\usepackage[percent]{overpic}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns,calc,positioning,shapes}
\begin{document}
\begin{figure}
\center
\begin{tikzpicture}
\draw[ultra thick, red, name path=A]
(-8,1.5) cos (-6,0) sin (-4,-1.5) cos (-2,0) sin (0,1.5) cos (2,0) sin (4,-1.5) cos (6,0) sin (8,1.5);
\draw[line width=2pt, black, dashed] (-8,0) -- (8,0);
\draw[line width=0.5pt, black, dashed, name path=B] (-8,-2) -- (8,-2);
\draw[->] (-8.2,-2) -- (8.2,-2) node[right] {$x$};
\draw[->] (-8,-2.2) -- (-8,10.2) node[above] {$y$};
\tikzfillbetween[of=A and B]{pattern=crosshatch};
\end{tikzpicture}
\end{figure}
\end{document}
我究竟做错了什么 ?
答案1
如果你想使用 pdflatex 并维护这个,你只需要删除pstricks
包,这样 XeLaTeX
就没有问题了pstricks
回答给出方法来做到这一点
\documentclass[a4paper,12pt,twoside]{book}
\usepackage{tikz}
\usepackage{circuitikz}
%\usepackage{pstricks}
\let\clipbox\relax
\usepackage[percent]{overpic}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns,calc,positioning,shapes}
\begin{document}
\begin{figure}
\center
\begin{tikzpicture}
\draw[ultra thick,red, name path=A]
(-8,1.5) cos (-6,0) sin (-4,-1.5) cos (-2,0) sin (0,1.5) cos (2,0) sin (4,-1.5) cos (6,0) sin (8,1.5);
\draw[line width=2pt, black, dashed] (-8,0) -- (8,0);
\draw[line width=0.5pt, black, dashed, name path=B] (-8,-2) -- (8,-2);
\draw[->] (-8.2,-2) -- (8.2,-2) node[right] {$x$};
\draw[->] (-8,-2.2) -- (-8,10.2) node[above] {$y$};
\tikzfillbetween[of=A and B]{pattern=crosshatch};
\end{tikzpicture}
\end{figure}
\end{document}
更新
另一种方法不使用fillbetween
库,只需在路径内使用剪切网格,在这种情况下,您可以使用scale
选项控制“图案”的密度
\documentclass[a4paper,12pt,twoside]{book}
\usepackage{tikz}
\usepackage{circuitikz}
%\usepackage{pstricks}
\let\clipbox\relax
\usepackage[percent]{overpic}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
%\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns,calc,positioning,shapes}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{scope}
\path[clip]
(-8,1.5) cos (-6,0) sin (-4,-1.5) cos (-2,0) sin (0,1.5) cos (2,0) sin (4,-1.5) cos (6,0)
sin (8,1.5)|-(-8,-2)--cycle;
\draw[rotate=30,step=0.3](-8,-6)grid(8,6);
\end{scope}
\draw[ultra thick,red]
(-8,1.5) cos (-6,0) sin (-4,-1.5) cos (-2,0) sin (0,1.5) cos (2,0)
sin (4,-1.5) cos (6,0) sin (8,1.5);
\draw[line width=2pt, black, dashed] (-8,0) -- (8,0);
\draw[->] (-8.2,-2) -- (8.2,-2) node[right] {$x$};
\draw[->] (-8,-2.2) -- (-8,10.2) node[above] {$y$};
\end{tikzpicture}
\end{figure}
\end{document}