% !TeX root=demo.tex
\ProvidesPackage{grid-system}[2013/11/04 v. 0.1 LaTeX Grid System]

\RequirePackage{calc}
\RequirePackage{xkeyval}
\RequirePackage{ifthen}

\newcommand{\gridsystem@cellwidth}[2]{#1\linewidth/#2}

\newlength{\gridsystem@cellsep}
\setlength{\gridsystem@cellsep}{1.75em}

\newlength{\gridsystem@one@cellwidth}

\newcounter{gridsystem@cellinrow}

% Macros to finish cells and rows, so that the areas between cells and rows can be defined very flexibly.
\newcommand{\gridsystem@finishrow}{\bigskip}
\newcommand{\gridsystem@finishcell}{\hskip\gridsystem@cellsep}
\newcommand{\gridsystem@finishlastcell}{}

\define@key{row}{cellsep}{\setlength\gridsystem@cellsep{#1}}

%\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1#1}

\newenvironment{row}[3][cellsep=1.75em]{
	\setkeys{row}{#1}%
	\setcounter{gridsystem@cellinrow}{0}
	\newenvironment{cell}[1]{%
		\addtocounter{gridsystem@cellinrow}{##1}%
		\setlength{\gridsystem@one@cellwidth}{(\linewidth-\gridsystem@cellsep*(#3-1))/#2}%
		\begin{minipage}[t]{##1\gridsystem@one@cellwidth}%
	}{%
		\end{minipage}%
		\@ignoretrue% Ignore spaces
		\ifthenelse{\value{gridsystem@cellinrow}<#2}{%
			\gridsystem@finishcell%
		}{%
			\gridsystem@finishlastcell%
		}%
	}
}{
	\gridsystem@finishrow%
}
