6

I would like a rectangle with the following style:

\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\path[fill=gray,pattern=bricks] (0,0) rectangle (5,-1);
\end{tikzpicture}
\end{document}

I use the preceding code but the output is merely:

Why does this method fail? Thank you!

6

pattern is also a kind of filling. So it overwrites the fill option. But you can use preaction to fill the path with the color before it is filled by the pattern.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\path
  [pattern=bricks]
  [preaction={fill=gray}]
  (0,0) rectangle (5,-1);
\end{tikzpicture}
\end{document}

enter image description here

1

Another solution would be to save your path in a macro, let's say \mypath and then apply \fill and \pattern to it.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
  \def\mypath{(0,0) rectangle (5,-1)}
    \fill [gray] \mypath;
    \pattern [pattern=bricks] \mypath;
\end{tikzpicture}
\end{document}

enter image description here

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.