1

I am trying to create a vertical section with different height blocks and fill each block with different patterns using the following code.

My approach: I have saved the patterns name as a list of stings in the variable \names and trying to access its elements (\names[\i]) in the \draw command as pattern= \pgfmathparse{\names[\i]}\pgfmathresult. I am using \foreach loop and in its i th iteration, it should be providing i th pattern from the list.

Previous solutions were suggested in this post and this post, however, these didn't work.

In essence, I am facing a problem in passing down the pattern to the draw command. Alternatively, is there any way to automatically change the fill pattern in some given sequence?

The figure should look as shown below.

\\documentclass[10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns} 
\begin{document}
\begin{tikzpicture}
\def\zthick{{1,  0.5,  0.8,  1.2,  0.7,  0.4,  1.1,  1.7,  0.5}}
\def\names{{"dots","horizontal lines","vertical lines","grid","north east lines","north west lines","bricks", "crosshatch"}}%
\def\width{1}
\coordinate (L) at (\width,0);  
\foreach \i in {1,2,3}
{ \pgfmathsetmacro{\b}{\zthick[\i]};
  \draw [pattern=\pgfmathparse{\names[\i]}\pgfmathresult ] 
    (L) ++(-\width,0) rectangle ++(\width,-\b) coordinate (L);  }
\end{tikzpicture}
\end{document}

I understand this task can be achieved writing explicitly for each block, but I would like to know if there is any way to make it work with loops.

enter image description here

Amartya
  • 143

1 Answers1

1

You should probably not feed \pgfmathparse to the pattern option, but only its result. You can just use \pgfmathsetmacro twice.

So, your approach can work as follows:

\documentclass[10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document} \begin{tikzpicture}

\def\zthick{{1, 0.5, 0.8, 1.2, 0.7, 0.4, 1.1, 1.7, 0.5}} \def\names{{"dots","horizontal lines","vertical lines","grid","north east lines","north west lines","bricks", "crosshatch"}} \def\width{1}

\coordinate (L) at (\width,0);
\foreach \i in {1,2,3} { \pgfmathsetmacro{\b}{\zthick[\i]} \pgfmathsetmacro{\p}{\names[\i]} \draw [pattern=\p] (L) ++(-\width,0) rectangle ++(\width,-\b) coordinate (L); }

\end{tikzpicture} \end{document}

enter image description here


Note that this only works because of the functionality of \pgfmathparse. Generally speaking, in TeX you cannot access an item inside a list using \list[i] where i denotes the index of the item like you can in other programming languages.

Another way to solve this would be to make use of the fact that the \foreach macro allows to simultaneously loop through two list. If you have the item a/b, you can access a and b in just one loop. You could use this to achieve what you want.

Further, you would then need to advance a counter that holds the height of the last rectangle.

Probably something like this could work:

\documentclass[10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document} \begin{tikzpicture}

\def\bgrounds{ dots/1, horizontal lines/0.5,
vertical lines/0.8,
grid/1.2, north east lines/0.7, north west lines/0.4, bricks/1.1, crosshatch/1.7 }

\newlength\bwidth \setlength\bwidth{1cm} \newlength\bheight \setlength\bheight{0pt}

\foreach \i/\j in \bgrounds { \draw [pattern=\i] (-\bwidth,\bheight) rectangle ++(\bwidth,-\j); \global\advance\bheight by -\j cm }

\end{tikzpicture} \end{document}

enter image description here