# Loops and Tables

I'm trying to use LaTex to produce our family diary for next year. It's a page per week with a column for each person.

I have the basic layout working:

\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[a5paper, top=1cm, bottom=1cm, left=2cm, right=1cm]{geometry}
\usepackage[dayofweek]{datetime}   % Dates formatting
\usepackage{datenumber} % Counters for dates
\usepackage{tabularx}
\pagestyle{empty}
\newdateformat{mydate}{\shortdayofweekname{\day}{\month}{\year} \twodigit\THEDAY{} \shortmonthname[\THEMONTH]}

\newcommand{\headerrow}{\hline & Me & My Wife & Son & Daughter\\ \hline}
\newcommand{\dayrow}[1]{\AdvanceDate[#1] \mydate\today & & & &\\[2.2cm] \hline}
\newcommand{\weektable}{
\newpage
\begin{tabularx}{\textwidth}{|X|X|X|X|X|}
\dayrow{0}
\dayrow{1}
\dayrow{2}
\dayrow{3}
\dayrow{4}
\dayrow{5}
\dayrow{6}
\end{tabularx}
}

\SetDate[05/01/2015]
\begin{document}
\weektable{}
\end{document}


but it's ugly!!

I've tried using a loop to avoid the repetition in \weektable, but I'm falling foul of loops within tables and cannot get it to work.

I've also tried adding a parameter to \weektable so I can produce several weeks at once, but I can't work out how pass the result of a calculation into the command.

I know these are both similar to other questions but, despite reading everywhere I've found on here, I'm stuck and would appreciate some help!

This is a quick improvement for a working version, but it does not address some issues (i.e. table design etc.)

Use for example the forloop package to make the full year table...

However, since the OP uses \AdvanceDate in a table cell (a group), the actual day is not advanced after a week table is finished, so this has to be done after the tabularx environment.

\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[a5paper, top=1cm, bottom=1cm, left=2cm, right=1cm]{geometry}
\usepackage[dayofweek]{datetime}   % Dates formatting
\usepackage{datenumber} % Counters for dates
\usepackage{tabularx}
\usepackage{forloop}
\pagestyle{empty}

\newdateformat{mydate}{\shortdayofweekname{\day}{\month}{\year} \twodigit\THEDAY{} \shortmonthname[\THEMONTH]}

\newcommand{\headerrow}{\hline & Me & My Wife & Son & Daughter\\ \hline}
\newcommand{\dayrow}[1]{\AdvanceDate[#1] \mydate\today & & & &\\[2.2cm] \hline}
\newcommand{\weektable}{
\newpage
\begin{tabularx}{\textwidth}{|X|X|X|X|X|}
\dayrow{0}
\dayrow{1}
\dayrow{2}
\dayrow{3}
\dayrow{4}
\dayrow{5}
\dayrow{6}
\end{tabularx}
}

\SetDate[05/01/2015]

\newcounter{weeks}%
\begin{document}
\forloop{weeks}{1}{\value{weeks} < 53}{%
\weektable{}

}%
\end{document}


• Well, that avoids the need for any calculation rather nicely! – Owen Nov 12 '14 at 14:55
• and yes, I know the table design isn't great, but let's just say I'm working to a specification defined by a higher authority! – Owen Nov 12 '14 at 15:04
• @Owen: Your wife? ;-) – user31729 Nov 12 '14 at 15:06
• Yep - a.k.a "Senior Management" – Owen Nov 12 '14 at 15:12

You can use xparse and expl3 for repeating the task:

\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[a5paper, top=1cm, bottom=1cm, left=2cm, right=1cm]{geometry}

\usepackage{xparse}
\usepackage[dayofweek]{datetime}   % Dates formatting
\usepackage{datenumber} % Counters for dates
\usepackage{tabularx}

\newdateformat{mydate}{%
\shortdayofweekname{\day}{\month}{\year} % space
\twodigit\THEDAY{} % space
\shortmonthname[\THEMONTH]% no space
}

\newcommand{\headerrow}{\hline & Me & My Wife & Son & Daughter\\ \hline}
\newcommand{\dayrow}[1]{\AdvanceDate[#1] \mydate\today & & & &\\[2.2cm] \hline}
\newcommand{\printweektable}{%
\newpage\noindent
\begin{tabularx}{\textwidth}{|X|X|X|X|X|}
\dayrow{0}
\dayrow{1}
\dayrow{2}
\dayrow{3}
\dayrow{4}
\dayrow{5}
\dayrow{6}
\end{tabularx}%
}

\ExplSyntaxOn
\NewDocumentCommand{\weektable}{O{1}m}
{
\SetDate[#2]
\computedayofweek{\day}{\month}{\year}
% always start on Monday
\int_eval:n { 2 - \dayofweek \int_compare:nT { \dayofweek == 1 } { -7 } }
]
\prg_replicate:nn { #1 } { \printweektable }
}
\ExplSyntaxOff

\pagestyle{empty}

\begin{document}

\weektable[4]{04/01/2015}

\end{document}


The date can be arbitrary, the week will always start on a Monday such that the “starting” day is included.

For example, 4 January 2015 will be a Sunday, so the first page will start from Monday 29 December.

The optional argument tells how many weeks are to be generated (default one).

• Very nice! That avoids one potential cockup and a whole heap of wasted paper – Owen Nov 12 '14 at 15:42