# Drawing aligned boxes with shaded section

I would like to draw several boxes with vertical line split but so far have not been very successful.

Here's the code I attempted so far

\documentclass{article}
\usepackage{tikz}
\usepackage{framed,graphicx,xcolor}
\begin{document}

\tikzstyle{block} = [draw, rectangle,color=gray, fill=gray!50, minimum height=4mm]
\tikzstyle{block1} = [draw, rectangle,minimum height=4mm, minimum width=40mm]
\tikzstyle{block2} = [draw, rectangle,minimum height=4mm, minimum width=45mm]
\begin{tikzpicture}
\node [block, minimum width=5mm] (11) {};
\node [block2, minimum width=45mm, right of =11,xshift=10mm] (12) {};

\node [block1, minimum width=5mm, below of =11] (21) {};
\node [block, minimum width=5mm, right of =21,xshift=-5mm] (22) {};
\node [block1, minimum width=40mm, right of =21,xshift=17.5mm] (20) {};

\node [block1, minimum width=10mm, below of =21] (31) {};
\node [block, minimum width=5mm, right of =31,xshift=-5mm] (32) {};
\node [block1, minimum width=35mm, right of =31, xshift=15mm] (30) {};

\node [block1, minimum width=15mm, below of =31] (41) {};
\node [block, minimum width=5mm, right of =41,xshift=-5mm] (42) {};
\node [block1, minimum width=30mm, right of =41, xshift=12.5mm] (40) {};

\node [block1, minimum width=20mm, below of =41] (51) {};
\node [block, minimum width=5mm, right of =51,xshift=2mm] (52) {};
\node [block1, minimum width=25mm, right of =51, xshift=17mm] (50) {};

\end{tikzpicture}
\end{document}


The attached figure is what I hope to achieve as the result !

Is there anyone who can help me with this? Or is there a better way to doing it? Many thanks in advance.

[Edited] I have updated the image to clarify some details. Perhaps figure itself is not clearly shown that all the boxes should be of the same length with same alignment to each other. The shaded small portion in each of the bar (box) should be position in such a way that it seems to be sliding from one slot to another without space in between or overlapping. I would like to show that the shaded portion is continuously sliding across them in the following bars with the same small steps. Thanks!

-

Some of the other solutions seem rather complicated. All you need is a loop. Use \Slider{<number>} to draw <number>+1 bars with the moving slider; the width and height of the bar can be changed using \BarWd and \BarHt (resp.); the width of the slider is given by \SliderWd and the vertical separation between consecutive bars is given by \BarSep.

\documentclass{article}
\usepackage{tikz}

\newcommand\BarWd{6cm}
\newcommand\BarHt{10pt}
\newcommand\SliderWd{20pt}
\newcommand\BarSep{10pt}

\newcommand\Slider[1]{%
\begin{tikzpicture}
\foreach \i in {0,1,...,#1}
{
\draw[fill=gray] (\i*\SliderWd,-\i*\BarHt-\i*\BarSep) rectangle +(\SliderWd-\pgflinewidth,\BarHt);
\draw (0,-\i*\BarHt-\i*\BarSep) rectangle +(\BarWd-\pgflinewidth,\BarHt);
}
\end{tikzpicture}%
}

\begin{document}

\noindent\Slider{3}\par\bigskip

\renewcommand\BarWd{\textwidth}
\renewcommand\BarHt{20pt}
\renewcommand\SliderWd{1.5cm}
\renewcommand\BarSep{20pt}

\noindent\Slider{6}

\end{document}


-
thanks for your help! This is exactly what I intended to get. – user46182 Feb 15 '14 at 1:10

Another try, with MetaPost code included in a LuaLaTeX file, making use of the most recent luamplib package available on CTAN.

Like Gonzalo Medina, I'm using a simple loop and three parameters, namely the width and the height of the part, and the length of the shaded part of the bar. Moreover, I'm assuming that I must draw as many bars as the maximal number of shaded parts possible. For example, if the bar length is 7 and the slider length is 2, it will draw 3 = 7 div 2 bars.

\documentclass{scrartcl}
\usepackage{luamplib}
\mplibsetformat{metafun}
\everymplib{%
verbatimtex
\leavevmode
etex;
u := 1cm; % Scale
% Macro creating the bars
def bars(expr width, height, shaded_width) =
numeric n; n := width div shaded_width;
for i=1 upto n:
bar := unitsquare xyscaled ((width, height)*u)
shifted (0, -1.5height*(i-1)*u);
draw bar;
% for not drawing the same lines twice
if point 0 of bar <> point 0 of shaded_part:
fi;
if point 1 of bar <> point 1 of shaded_part:
fi;
endfor;
enddef;
beginfig(0)}
\everyendmplib{endfig;}

\begin{document}
\begin{center}
\begin{mplibcode}
bars(8, 0.75, 2);
\end{mplibcode}
\par\vspace{3\bigskipamount}
\begin{mplibcode}
bars(5.5, 0.5, 1);
\end{mplibcode}
\end{center}
\end{document}


-
thanks for your suggestion too. it helps a lot! – user46182 Feb 15 '14 at 1:11

Taking this timeline maker (Timeline in Beamer presentation) out of the box, without any changes, nor tikz:

\documentclass{article}
\usepackage{stackengine}
\renewcommand\useanchorwidth{T}
\usepackage{xcolor}
\def\theyearwidth{1.5pt}
\newlength\yrsfboxrule
\yrsfboxrule .4\fboxrule
\newcommand\yearwidth[1]{\def\theyearwidth{#1}\ignorespaces}
\newcommand\skipyears[2][white]{%
\fboxrule\yrsfboxrule%
\fboxsep=-\yrsfboxrule%
\fcolorbox{gray}{#1}{\strut\hspace{#2}}%
\ignorespaces%
}
\newcommand\showyear[2][black]{%
\fboxsep=0pt%
\stackon{%
\colorbox{#1}{\strut\hspace{\theyearwidth}}%
}{\sffamily\small#2}%
\ignorespaces%
}
\begin{document}
\yearwidth{5mm}
\hspace{2mm}\showyear[gray]{}\skipyears{45mm}\par
\hspace{1.5mm}\skipyears{5mm}\showyear[gray]{}\skipyears{41mm}\par
\hspace{.5mm}\skipyears{10mm}\showyear[gray]{}\skipyears{38mm}\par
\skipyears{15mm}\showyear[gray]{}\skipyears{35mm}\par
\skipyears{23mm}\showyear[gray]{}\skipyears{28mm}\par
\end{document}


-