# Latex Calendar get Date of a Cell

I created the syllabus for a course that I teach using the "calendar" package. Some entries of the table highlights the homework and the quizzes.

Is it possible to get the dates of the homework and the quizzes from the calendar and use them in another table that includes all of the important dates (dates of the homeworks, quizzes, midterm, final ,...etc)? The dates in the Important Dates Table should be automatically changed if the locations of the Quizzes and the Homework are changed in the Calendar.

I think we need to label the quizzes and the homework so that we could cross reference them.

Here is the code.

\documentclass[10pt]{article}
%\usepackage[ a4paper]{geometry}
\textwidth=7in
\textheight=9.5in
\topmargin=-1in
\hoffset  -.85in

\usepackage{enumitem}
\usepackage{termcal}
\usepackage{hyperref}

\newcommand{\MWClass}{
\calday[\bf Monday]{\classday}
\skipday
\calday[\bf Wednesday]{\classday}
\skipday
\skipday\skipday
\skipday
}

\setlist[itemize]{itemsep=1pt, topsep=0pt}
\setlist[enumerate]{itemsep=1pt, topsep=0pt}

\renewcommand{\thefootnote}{\fnsymbol{footnote}}

\begin{document}
\begin{center}
\begin{calendar}{01/11/2021}{2}
\setlength{\calboxdepth}{0.6in}
\MWClass

\caltexton{1}{\vspace{1ex} Course Introduction}

\caltextnext{
\vspace{1ex}
Lecture 1 \\ \vspace{1ex}
{\bf Chapter 9: Steady-State Power Analysis} \vspace{1ex}
\begin{itemize}
\item[] {\bf Sec 9.1} Instantaneous power
\end{itemize}}

\caltexton{1}{}
\caltextnext{\vspace{1.5ex}
{\bf \color{red} HW 1 (Chapter 9)}}
\caltextnext{\vspace{1.5ex}
{\bf \color{red} Quiz 1 (Chapter 9)}}

\end{calendar}
\end{center}

\begin{center}
{\Large Important Dates}\\ \vspace{-1ex}
\end{center}

\begingroup
\normalsize

\renewcommand{\arraystretch}{2}
\centering

\begin{longtable}{|l|p{0.15\textwidth}|l|}
\hline
& \bfseries Chapter & \bfseries Due Date  \\ \hline \endhead
HW 1 & Ch 9 & Date of HW 1 in the Clanedar\\ \hline
Quiz 1 & Ch 9 & Date of Quiz 1 in the Clanedar\\ \hline
\end{longtable}
\endgroup

\clearpage
\newpage

\end{document}

• As always on the site you are much much more likely to get any help if you provide a minimal selfcontained example that others can copy and play with as is. As it sits now, hardly anyone knows what you are talking about because there is no code to relate it to. Jan 12 '21 at 11:39
• I updated the question with the code. Will appreciate your help. Jan 12 '21 at 12:25

This can be done using list processing macros from the etoolbox package. The idea is to define a new command that shows the important events in the calendar, but also stores the information (name, chapter, date) in a list. Then after the calendar is finished you can loop the list inside of the longtable to show the items.

The list processing macros from etoolbox are called list[type]add where the type represents the way of storing the information in the list. In the code below \listxadd is used, which means expanded and global. Expanded is needed because the date value must be stored as a raw number instead of a macro which keeps changing with further calendar days, and global because the values are needed outside of the calendar in the table. The values are stored in the list as a table row, i.e., name & chapter & date.

Update: you can manually add rows to the table by inserting extra \listadd commands at the corresponding positions in the calendar. In this case it is more convenient to use \listgadd (add global without expansion) because that allows for more flexibility in the type of contents that is added. For example, a \multicolumn command is not allowed with \listxadd because the immediate expansion of the multicolumn interferes with list processing. Note that the extra rows must be added inside \caltextnext or \caltexton, because otherwise the rows are added at the end.

The formatting of the table rows can be adjusted by modifying the \listxadd arguments within \importantitem, for example adding an extra argument for remarks or including the year.

The processing consists of defining the loop function \do, which needs to be done outside of the table, and calling this function on all elements of the list using \dolistloop. The \do function contains \\\hline, i.e., it finishes the table row.

An additional issue is printing the day as ordinal number like 11th or 3rd. The termcal package has a macro for this (\ordinaldate) but that macro causes an error inside of the list processing of etoolbox. Therefore a small part of the nth package is copied to generate the ordinal suffix.

Code:

\documentclass[10pt]{article}
\usepackage{etoolbox}
%\usepackage[ a4paper]{geometry}
\textwidth=7in
\textheight=9.5in
\topmargin=-1in
\hoffset  -.85in

% copied from nth.sty  version 2002/27/02 Donald Arseneau
\def\nth#1{%
#1\expandafter \nthSuff \expandafter 0\number\ifnum #1<0-\fi#1\delimiter%
}
\def\nthSuff#1#2#3{%
\ifx \delimiter#3% #1#2 are last two digits
\ifnum #1=1 th%  teens are always th''
\else % use appropriate suffix
\ifcase #2 th\or st\or nd\or rd\else th\fi
\fi
\else % continue scanning for last two digits
\expandafter \nthSuff \expandafter #2\expandafter #3%
\fi}
% end of copied nth code

\usepackage{enumitem}
\usepackage{termcal}
\usepackage{hyperref}

% arguments: #1=name, #2=chapter, #3=remarks
\newcommand{\importantitem}[3]{%
\textbf{\textcolor{red}{#1 (Chapter #2)}}%
%Weekday, Day Month, Year + {Some Custom Text}
\listxadd{\impitems}{#1 & Ch #2 & \nth{\thedate}\ \monthname, \theyear\ #3}
}

\newcommand{\MWClass}{
\calday[\bf Monday]{\classday}
\skipday
\calday[\bf Wednesday]{\classday}
\skipday
\skipday\skipday
\skipday
}

\setlist[itemize]{itemsep=1pt, topsep=0pt}
\setlist[enumerate]{itemsep=1pt, topsep=0pt}

\renewcommand{\thefootnote}{\fnsymbol{footnote}}

\begin{document}
\begin{center}
\begin{calendar}{01/11/2021}{2}
\setlength{\calboxdepth}{0.6in}
\MWClass

\caltexton{1}{\vspace{1ex} Course Introduction}

\caltextnext{
\vspace{1ex}
Lecture 1 \\ \vspace{1ex}
{\bf Chapter 9: Steady-State Power Analysis} \vspace{1ex}
\begin{itemize}
\item[] {\bf Sec 9.1} Instantaneous power
\end{itemize}}

\caltexton{1}{}
\caltextnext{\vspace{1.5ex}
\importantitem{HW 1}{9}{at 11:59 PM (Online submission)}%
}
\caltextnext{\vspace{1.5ex}\arabic{textdaycount}
\importantitem{Quiz 1}{9}{at 11:59 PM (Online submission)}%
\listgadd{\impitems}{Office hour & all chapters & Every Monday 10:00h}%
}

\end{calendar}
\end{center}

\begin{center}
{\Large Important Dates}\\ \vspace{-1ex}
\end{center}

\begingroup
\normalsize

\renewcommand{\arraystretch}{2}
\centering

\renewcommand*{\do}[1]{#1\\\hline} % define item processing macro
\begin{longtable}{|l|p{0.15\textwidth}|l|}
\hline
& \bfseries Chapter & \bfseries Due Date  \\ \hline \endhead
\dolistloop{\impitems} % loop the list to generate table rows
\end{longtable}
\endgroup

\end{document}


Result:

With update:

• Thanks a lot for your reply. I really appreciate it. 1- Is it possible to write the Due Date as follows: Weekday, Day Month, Year + {Some Custom Text} Here is an example: Monday, 8\textsuperscript{th} Febraury, 2021 at 11:59 PM (Online submission) 2- Also, is it possible to add a normal table entry in the Important Dates Table in between two ImportantItems. I am asking since a loop is used and I think we can't break the loop to add such item. Jan 12 '21 at 14:07
• @AbdulrahmanKalbat I think both of these things would be possible as well. I'll try to address these points in an edit tomorrow. Jan 12 '21 at 20:16
• I would appreciate it. Your code is very easy to follow. Jan 13 '21 at 7:00
• @AbdulrahmanKalbat I added a first update, it is still missing the day names. Jan 13 '21 at 11:36
• Thanks for your code. It is working perfectly. One last question. I would like to add a homework with deadline on Saturday instead of class days Monday or Wednesday. I want to show it in the term calendar as a single row that spans two columns of Monday and Wednesday. The new cell will show some text as “Homework 1” and the cell will also show the date of the Saturday based on the dates of the class days before and after. The table “Important Dates” will also include this Homework that is on Saturday. Sep 2 '21 at 18:02

Using the LaTeX 2ε-kernel's \@starttoc/\addtocontents-mechanism you can define a command

\ImportantDate{⟨WorkAssignment⟩}{⟨chapter-number⟩}{⟨customary text⟩}

⟨WorkAssignment⟩ = HW/Homework 1 or Quiz 3
⟨chapter-number⟩ = Number of Chapter
⟨customary text⟩ = Customary text

which

• delivers: ⟨WorkAssignment⟩~(Chapter~⟨chapter-number⟩)%
• via \addtocontents creates a \@writefile-entry for the file \jobname.loids (loids = list of important dates) denoting the call of the macro \LineWithImportantDate with arguments holding all information needed for creating another row of the longtable.

Then you can have a macro \listofImportantDates which—similar to \tableofcontents—via \@starttoc imports the .loids-file and creates the \write-handle for the .loids-file.

You can use \AtBeginDocument (from the LaTeX 2ε-kernel) and \AfterLastShipout (from the package atveryend) for prepending/appending the longtable-preamble/postamble to the .loids-file.

You need to redefine \calprintdate so that also a \hypertarget is created.

You can use the macros of the package datetime2

• for calculating the day of the week
• obtaining the name of the day of the week
• obtaining the name of the month
• obtaining the day of the month as an ordinal number (st, nd, rd, th appended).

You can define commands \Homework and \Quiz which step a counter "homeworks" respective "quizzes" and then call \ImportantDate{⟨WorkAssignment⟩}, leaving the gathering of remaining arguments to \ImportantDate.

Syntax is the same as with \ImportantDate except that ⟨WorkAssignment⟩ is auto-generated:

\Homework{⟨chapter-number⟩}{⟨customary text⟩}
\Quiz{⟨chapter-number⟩}{⟨customary text⟩}

Starred variants of these commands don't produce hyperlinks from the List of important dates to the calendar.

Edit 1: As with slightly older LaTeX 2ε-kernels the \@writefile-mechanism doesn't take \protected@file@percent into account, I removed all instances of \protected@file@percent from my code.

The code (saved as test.tex) compiled successfully using TeXLive 2020 under Debian Buster x86. I had to compile four times without deleting auxiliary-files in between until everything coming from auxiliary files that get created/altered during the latex-run (longtable, hypertargets) matched out correctly.

Edit 2: I added a command

\ImportantDateNote{⟨amount of empty cells at the left of the cell with the note (range from 0 to 2)⟩}%
{⟨amount of cells the note shall span (range from 1 to 3)⟩}%
{⟨flag <=0 or >0 denoting if vertical bar shall be drawn between two consecutive empty cells⟩}%
{⟨alignmant of note l r c p{0.15\textwidth}⟩}%
{⟨text of note⟩}

for placing whatsoever notes into the table of important dates.

The table has three columns, thus the sum of ⟨amount of empty cells at the left of the cell with the note (range from 0 to 2)⟩ and ⟨amount of cells the note shall span (range from 1 to 3)⟩ must not exceed the value 3.

\documentclass[10pt]{article}
%\usepackage[a4paper]{geometry}
\textwidth=7in
\textheight=9.5in
\topmargin=-1in
\hoffset  -.85in

\usepackage{enumitem}
\usepackage{termcal}
\usepackage{xcolor}
\usepackage{hyperref}

\newcommand{\MWClass}{%%%
\calday[\bf Monday]{\classday}%%%
\skipday
\calday[\bf Wednesday]{\classday}%%%
\skipday
\skipday\skipday
\skipday
}

\setlist[itemize]{itemsep=1pt, topsep=0pt}
\setlist[enumerate]{itemsep=1pt, topsep=0pt}

\renewcommand{\thefootnote}{\fnsymbol{footnote}}

%////////////////////////////////////////////////////////////////////////////
\usepackage[calc, en-GB-numeric]{datetime2}
\usepackage{atveryend}
\DTMlangsetup[en-GB]{ord=raise}
\makeatletter
\newcommand*\ImportantDate@Chaptername{Chapter}%
\newcommand*\ImportantDate@AbbreviatedChaptername{Chapter}%
\newcommand\ImportantDateTempa{}%
\@ifdefinable\ImportantDate{%
\DeclareRobustCommand\ImportantDate{%
\@ifstar{\@ImportantDate*}{\@ImportantDate{}}%
}%
}%
\newcommand\@ImportantDate[4]{%
% #1 - Star/NoStar
% #2 - WorkAssignment - e.g., HW1 or Quiz 1
% #3 - number of chapter
% #4 - customary text
#2 (\ImportantDate@Chaptername~#3)%
\begingroup
\DTMsavenoparsedate{storeddate}{\arabic{year}}{\arabic{month}}{\arabic{date}}{-1}%
\DTMcomputedayofweekindex{\DTMfetchyear{storeddate}-\DTMfetchmonth{storeddate}-\DTMfetchday{storeddate}}{\ImportantDateTempa}%
\string\LineWithImportantDate#1\@percentchar^^J%
\space\space{\detokenize{#2}}\@percentchar^^J%
\space\space{\detokenize{#3}}\@percentchar^^J%
\space\space{\detokenize{#4}}\@percentchar^^J%
\space\space{\arabic{year}}\@percentchar^^J%
\space\space{\arabic{month}}\@percentchar^^J%
\space\space{\arabic{date}}\@percentchar^^J%
\space\space{\ImportantDateTempa}\@percentchar^^J%
\space\space{caldate.\arabic{year}-\arabic{month}-\arabic{date}}^^J%
}%
\endgroup
}%
\@ifdefinable\LineWithImportantDate{%
\DeclareRobustCommand\LineWithImportantDate{%
}%
}%
\newcommand\@LineWithImportantDate[9]{%
#1{#9}{#2}&%
#1{#9}{\ImportantDate@AbbreviatedChaptername~#3}&%
#1{#9}{%
\DTMWeekdayname{#8}, \DTMordinal{#7}~\DTMMonthname{#6}, #5#4%
}%
\\\hline
}%
\newcommand\ImportantDateNote[5]{%
% #1 - amount of empty cells at the left of the cell with the note
% #2 - amount of cells the note shall span
% #3 - flag <=0/>0 denoting if vertical bar between 2 consecutive empty cells
% #4 - alignmant of note l r c p{0.15\textwidth}
% #5 - text of note
\@bsphack
\@esphack
}%
\newcommand\LineWithImportantNote[5]{%
\ifnum#1>0 \exchange{%
\ifnum #3>0 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\ifnum#1>1 \expandafter\@secondoftwo\else\expandafter\@firstoftwo\fi
}{\@firstoftwo}%
{\multicolumn{#1}{|l}{}&}%
{\multicolumn{1}{|l}{}&\multicolumn{1}{|l}{}&}%
}\fi
\multicolumn{#2}{|#4|}{#5}%
\ifnum\number\numexpr3-#1-#2\relax>0 \exchange{%
\ifnum #3>0 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\ifnum\number\numexpr3-#1-#2\relax>1 \expandafter\@secondoftwo\else\expandafter\@firstoftwo\fi
}{\@firstoftwo}%
{&\multicolumn{\numexpr3-#1-#2\relax}{l|}{}}%
{&\multicolumn{1}{l|}{}&\multicolumn{1}{l|}{}}%
}\fi
\\\hline
}%
\newcounter{quizzes}%
\newcounter{homeworks}%
\newcommand\homeworksname{Homework}%
\newcommand\quizzesname{Quiz}%
\@ifdefinable\Stopromannumeral{\chardef\Stopromannumeral=\^^00}%
\newcommand\exchange[2]{#2#1}%
\@ifdefinable\Homework{%
\DeclareRobustCommand\Homework{%
\@ifstar{\@Homework{\expandafter*}}{\@Homework{}}%
}%
}%
\newcommand\@Homework[1]{%
\stepcounter{homeworks}%
\expandafter\ImportantDate#1\expandafter{%
\romannumeral
\expandafter\exchange\expandafter
{\number\value{homeworks}}%
{\expandafter\Stopromannumeral\homeworksname~}%
}%
}%
\@ifdefinable\Quiz{%
\DeclareRobustCommand\Quiz{%
\@ifstar{\@Quiz{\expandafter*}}{\@Quiz{}}%
}%
}%
\newcommand\@Quiz[1]{%
\stepcounter{quizzes}%
\expandafter\ImportantDate#1\expandafter{%
\romannumeral
\expandafter\exchange\expandafter
{\number\value{quizzes}}%
{\expandafter\Stopromannumeral\quizzesname~}%
}%
}%
\renewcommand\calprintdate{%
\hbox{%
\hypertarget{%
caldate.\arabic{year}-\arabic{month}-\arabic{date}%
}{}%
}%
% You can change this to do some more sophisticated/more beautiful things.
\ifnewmonth\framebox{\monthname\ \ordinaldate}%
\else \ordinaldate\fi
}%
}%
\AtBeginDocument{%
&\string\bfseries\space Chapter&\string\bfseries\space Due Date\string\\ \string\hline\space \string\endhead
}%
}%
\AfterLastShipout{%
\begingroup
\let\writecopy=\write
\def\write{\noexpand\immediate\noexpand\writecopy}%
\endgroup
}%
\newcommand\listofImportantDates{%
\IfFileExists{\jobname.loids}{%
\par
\begingroup
\centering
{\Large Important Dates}\\ \vspace{-1ex} %
\par
\@starttoc{loids}%
\par
\endgroup
}{%
\@starttoc{loids}%
}%
}%
\makeatother
%////////////////////////////////////////////////////////////////////////////

\begin{document}

\listofImportantDates

\clearpage
\newpage

\begin{center}
\begin{calendar}{01/11/2021}{2}
\setlength{\calboxdepth}{0.6in}
\MWClass

\caltexton{1}{\vspace{1ex} Course Introduction}%
\caltextnext{%
\vspace{1ex}  %
Lecture 1 \\ \vspace{1ex}
{\bf Chapter 9: Steady-State Power Analysis}%
\vspace{1ex}%
\begin{itemize}
\item[] {\bf Sec 9.1} Instantaneous power%
\end{itemize}%
}

\caltexton{1}{}%
\caltextnext{%
\vspace{1.5ex}%
{\bf\color{red}\Homework{9}{ at 11:59 PM (Online submission)}}%
\ImportantDateNote{1}{2}{0}{c}{\emph{The sooner you do your homework, the sooner it will stop weighing your mind!}}%
}%
\caltextnext{%
\vspace{1.5ex}%
{\bf\color{red}\Quiz*{9}{ at 11:59 PM (Offline omission)}}%
\ImportantDateNote{0}{1}{1}{l}{\emph{A note.}}%
\ImportantDateNote{1}{1}{0}{l}{\emph{A note.}}%
\ImportantDateNote{2}{1}{1}{l}{\emph{A note.}}%
\ImportantDateNote{2}{1}{0}{l}{\emph{A note.}}%
\ImportantDateNote{0}{2}{0}{l}{\emph{A note.}}%
\ImportantDateNote{0}{3}{0}{l}{\emph{A note.}}%
\ImportantDateNote{1}{2}{0}{l}{\emph{A note.}}%
\ImportantDateNote{0}{1}{0}{l}{\emph{A note.}}%
}%

\end{calendar}
\end{center}

\end{document}


• Thanks a lot for your efforts. I copied this code into TexStudio but it didn't work. I am not sure if you pasted the code here correctly or not. Jan 13 '21 at 6:59
• I updated the TexLive and it is working now. Jan 13 '21 at 12:11
• @AbdulrahmanKalbat Glad to see it's working now. Nevertheless, I would be grateful for a message, which error(s) exactly occurred, so that I can adapt the code so that it also runs on older platforms. (I have now also tested with a MiKTeX 2.9 from November 2010 running under Windows 7 with added datetime2 package: There is no \protected@file@percent in this older LaTeX 2e-kernel yet and things compile without error messages only if you remove all instances of \protected@file@percent` from the code.) Jan 13 '21 at 13:53