TransWikia.com

Latex Calendar get Date of a Cell

TeX - LaTeX Asked by Abdulrahman Kalbat on March 18, 2021

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.

enter image description here

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
headheight=0in
headsep=.5in
hoffset  -.85in

usepackage{enumitem}
usepackage{termcal}
usepackage{hyperref}
hypersetup{colorlinks= true, allcolors= blue}

newcommand{MWClass}{
calday[bf Monday]{classday} 
skipday
calday[bf Wednesday]{classday} 
skipday
skipdayskipday
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.15textwidth}|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}

2 Answers

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
headheight=0in
headsep=.5in
hoffset  -.85in

% copied from nth.sty  version 2002/27/02 Donald Arseneau
defnth#1{%
#1expandafter nthSuff expandafter 0numberifnum #1<0-fi#1delimiter%
}
defnthSuff#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 thor stor ndor rdelse thfi
   fi
 else % continue scanning for last two digits
   expandafter nthSuff expandafter #2expandafter #3%
 fi}
% end of copied nth code

usepackage{enumitem}
usepackage{termcal}
usepackage{hyperref}
hypersetup{colorlinks= true, allcolors= blue}

% 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
skipdayskipday
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)}%
listgadd{impitems}{multicolumn{3}{|l|}{textit{Late submissions will not be graded}}}%
}
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]{#1hline} % define item processing macro
begin{longtable}{|l|p{0.15textwidth}|l|}
hline
& bfseries Chapter & bfseries Due Date   hline endhead
dolistloop{impitems} % loop the list to generate table rows
end{longtable}
endgroup

end{document}

Result:

enter image description here

With update:

enter image description here

Correct answer by Marijn on March 18, 2021

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.15textwidth}⟩}%
                  {⟨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
headheight=0in
headsep=.5in
hoffset  -.85in

usepackage{enumitem}
usepackage{termcal}
usepackage{xcolor}
usepackage{hyperref}
hypersetup{colorlinks=true, allcolors= blue}

newcommand{MWClass}{%%%
calday[bf Monday]{classday}%%%
skipday
calday[bf Wednesday]{classday}%%%
skipday
skipdayskipday
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}%
newcommandImportantDateTempa{}%
@ifdefinableImportantDate{%
  DeclareRobustCommandImportantDate{%
    @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}%
  addtocontents{loids}{%
    stringLineWithImportantDate#1@percentchar^^J%
    spacespace{detokenize{#2}}@percentchar^^J%
    spacespace{detokenize{#3}}@percentchar^^J%
    spacespace{detokenize{#4}}@percentchar^^J%
    spacespace{arabic{year}}@percentchar^^J%
    spacespace{arabic{month}}@percentchar^^J%
    spacespace{arabic{date}}@percentchar^^J%
    spacespace{ImportantDateTempa}@percentchar^^J%
    spacespace{caldate.arabic{year}-arabic{month}-arabic{date}}^^J%
  }%
  endgroup
}%
@ifdefinableLineWithImportantDate{%
  DeclareRobustCommandLineWithImportantDate{%
    @ifstar{@LineWithImportantDate{@secondoftwo}}{@LineWithImportantDate{hyperlink}}%
  }%
}%
newcommand@LineWithImportantDate[9]{%
  #1{#9}{#2}&%
  #1{#9}{ImportantDate@AbbreviatedChaptername~#3}&%
  #1{#9}{%
    DTMWeekdayname{#8}, DTMordinal{#7}~DTMMonthname{#6}, #5#4%
  }%
  hline
}%
newcommandImportantDateNote[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.15textwidth}
  % #5 - text of note
  @bsphack
  addtocontents{loids}{stringLineWithImportantNote{#1}{#2}{#3}{#4}{#5}}%
  @esphack
}%
newcommandLineWithImportantNote[5]{%
  ifnum#1>0 exchange{%
     ifnum #3>0 expandafter@firstoftwoelseexpandafter@secondoftwofi
     {%
       ifnum#1>1 expandafter@secondoftwoelseexpandafter@firstoftwofi
     }{@firstoftwo}%
     {multicolumn{#1}{|l}{}&}%
     {multicolumn{1}{|l}{}&multicolumn{1}{|l}{}&}%
  }fi
  multicolumn{#2}{|#4|}{#5}%
  ifnumnumbernumexpr3-#1-#2relax>0 exchange{%
     ifnum #3>0 expandafter@firstoftwoelseexpandafter@secondoftwofi
     {%
       ifnumnumbernumexpr3-#1-#2relax>1 expandafter@secondoftwoelseexpandafter@firstoftwofi
     }{@firstoftwo}%
     {&multicolumn{numexpr3-#1-#2relax}{l|}{}}%
     {&multicolumn{1}{l|}{}&multicolumn{1}{l|}{}}%
  }fi
  hline
}%
newcounter{quizzes}%
newcounter{homeworks}%
newcommandhomeworksname{Homework}%
newcommandquizzesname{Quiz}%
@ifdefinableStopromannumeral{chardefStopromannumeral=`^^00}%
newcommandexchange[2]{#2#1}%
@ifdefinableHomework{%
  DeclareRobustCommandHomework{%
     @ifstar{@Homework{expandafter*}}{@Homework{}}%
  }%
}%
newcommand@Homework[1]{%
  stepcounter{homeworks}%
  expandafterImportantDate#1expandafter{%
    romannumeral
    expandafterexchangeexpandafter
    {numbervalue{homeworks}}%
    {expandafterStopromannumeralhomeworksname~}%
  }%
}%
@ifdefinableQuiz{%
  DeclareRobustCommandQuiz{%
     @ifstar{@Quiz{expandafter*}}{@Quiz{}}%
  }%
}%
newcommand@Quiz[1]{%
  stepcounter{quizzes}%
  expandafterImportantDate#1expandafter{%
    romannumeral
    expandafterexchangeexpandafter
    {numbervalue{quizzes}}%
    {expandafterStopromannumeralquizzesname~}%
  }%
}%
renewcommandcalprintdate{%
  hbox{%
    Hy@raisedlink{%
      hypertarget{%
          caldate.arabic{year}-arabic{month}-arabic{date}%
      }{}%
    }%
    % You can change this to do some more sophisticated/more beautiful things.
    ifnewmonthframebox{monthname ordinaldate}%
    else ordinaldatefi
  }%
}%
AtBeginDocument{%
  addtocontents{loids}{stringbegingroup}%
  addtocontents{loids}{stringrenewcommand{stringarraystretch}{2}}%
  addtocontents{loids}{stringbegin{longtable}{|l|p{0.15stringtextwidth}|l|}}%
  addtocontents{loids}{stringhline}%
  addtocontents{loids}{%
    &stringbfseriesspace Chapter&stringbfseriesspace Due Datestring stringhlinespace stringendhead
  }%
}%
AfterLastShipout{%
  begingroup
  letwritecopy=write
  defwrite{noexpandimmediatenoexpandwritecopy}%
  addtocontents{loids}{stringend{longtable}}%
  addtocontents{loids}{stringendgroup}%
  endgroup
}%
newcommandlistofImportantDates{%
  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}%
  {bfcolor{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}%
  {bfcolor{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}

enter image description here

enter image description here

Answered by Ulrich Diez on March 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP