5

I have the following code which produces two queues. I would like to fill the first packet of the first queue with black (or any other color) and keep the second queue unfilled. Can I do this without changing a lot my code?

The code 1:

\documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{chains,shapes.multipart}
\usetikzlibrary{shapes,calc,fit}
\usetikzlibrary{automata,positioning}

\tikzset{
    queuei/.pic={
        \draw[line width=1pt]
        (0,0) -- ++(2cm,0) -- ++(0,-1cm) -- ++(-2cm,0);
        \foreach \Val in {1,...,3}
        \draw ([xshift=-\Val*10pt]2cm,0) -- ++(0,-1cm);
        \node[above] at (1cm,0) {Queue $#1$};   
    },
}
\begin{document}    
\begin{tikzpicture}[>=latex]
% the shapes
\path 
(0,3cm) pic {queuei=1}
(0,1cm) pic {queuei=2};
\end{tikzpicture}
\end{document}

N.B. The desired output is as follows: enter image description here

  • If it is only the last rectangle you are interested, then you can add [pic actions] key to that rectangle. Then when you write pic[fill=red] it will be passed to pic actions. But you need to modify creating the little box drawing accordingly maybe taking it out of for each loop and drawing explicitly. See the manual for pic actions – percusse Sep 20 '15 at 17:43
5

I'd propose you a little modification to your pic using a rectangle split shape from the shapes.multipart library, which allows filling for the parts:

\documentclass[border=3pt]{standalone}
\usepackage{tikz}

\usetikzlibrary{chains,shapes.multipart}
\usetikzlibrary{shapes,calc,fit}
\usetikzlibrary{automata,positioning,shapes.multipart}

\newcounter{cntr}
\tikzset{
    queuei/.pic={
  \stepcounter{cntr}
        \node[outer sep=0pt,draw,rectangle split,rectangle split horizontal,minimum height=1cm,rectangle split parts=3] (queue-\thecntr) [pic actions] {};
        \draw 
          (queue-\thecntr.north west) -- ++(-1cm,0)
          (queue-\thecntr.south west) -- ++(-1cm,0);
        \node[above] at ([xshift=-0.5cm]queue-\thecntr.north) {Queue $#1$};   
    },
}
\begin{document}    
\begin{tikzpicture}[>=latex]
% the shapes
\path 
(0,3cm) pic[rectangle split part fill={white,white,gray!80}] {queuei=1}
(0,1cm) pic {queuei=2};
\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.