1

I want to fill between two paths I created with a pattern. When I do so, the filling is always black. I would like other colors/patterns.

My MWE is :

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

What am I doing wrong ?

1

You just need to remove pstricks package, with XeLaTeX there is no problem, if you want to use pdflatex and to maintain pstricks this answer give methods to do that

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

Update

Another method without using fillbetween library just with a clipped grid inside your path, in this case you can control density of "pattern" with scale option

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

enter image description here

  • It worked but is it possible to change the width and rate of the patern ? – zeslayer Jan 7 '17 at 17:02
  • 1
    Answer updated. – Salim Bou Jan 7 '17 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.