I am aware that there is a question on this site with the exact same title as mine, but what I am after is quite different.

I have written some course notes whose contents could be organised in two ways: the first is the logical content of the document (the natural way the topics should be structured), the other is by lectures (i.e., lecture 1, lecture 2, etc.). The latter is useful for students, especially if they miss a lecture and would like to know exactly what they missed.

I like to indicate both in my table of contents, and my current solution is to have a command I created called \newlecture{day}{month}{year}, this adds a nice header in the text and adds a line to the table of contents:

Here is the code for the MWE:

\documentclass{article}

% Packages
\usepackage{lipsum,parskip}
\usepackage[british]{datetime2}

% Date format
\DTMlangsetup[en-GB]{monthyearsep={,\space}}

% Lecture counter
\newcounter{lecture}
\newcommand\newlecture[3]{%
\stepcounter{lecture}
\phantomsection\textbf{LECTURE \thelecture}\hfill{\itshape \DTMdisplaydate{#3}{#2}{#1}{-1}}%

\begin{document}

% Contents
\tableofcontents

\pagebreak

\newlecture{14}{10}{2021}
\section{Introduction}
\lipsum[1-5]
\lipsum[6-10]
\subsection{The Exam}
\lipsum[11-15]

\section{Some Basic Techniques}
\lipsum[16-20]
\subsection{Arithmetic Functions}
\lipsum[21-25]
\subsection{M\"obius Inversion and Dirichlet Convolution}
\lipsum[26-30]
\subsection{Partial Summation}
\lipsum[31-35]

\newlecture{21}{10}{2021}

\lipsum[36-40]
\subsection{The Euler--Maclaurin Formula}
\lipsum[41-45]

\section{Proof of the Prime Number Theorem}
\lipsum[46-50]
\subsection{Some Initial Bounds}
\lipsum[51-55]
\end{document}


How can I add these to my contents with a similar command? I know I can do something like \section{Title\marginnote{...}} but that would create a margin note where the section appears also. I'd rather keep the margin notes just for the ToC (plus, sometimes I have to stop mid-section, so the command \newlecture can appear at any stage, not necessarily at the start of a section or subsection).

Unfortunately, using \addcontentsline we would have to add a new line in between items in the TOC.

My solution encompasses the redefinition of \newlecture to be the following:

\newcommand{\newlecture}[3]{
\stepcounter{lecture}

\fbox{\parbox[-2]{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}{\centering
\scriptsize{\scshape Lecture \thelecture}\\
\DTMdisplaydate{#3}{#2}{#1}{-1}}}}}}


where particular commands inside \addcontentstoline need to be preceded by \protect. Feel free to modify this MWE as desired.

\documentclass{article}
% Packages
\usepackage{lipsum,parskip}
\usepackage{datetime2}

% Counter declaration
\newcounter{lecture}

% Date format
\DTMlangsetup[en-GB]{monthyearsep={,\space}}

\reversemarginpar

\newcommand{\newlecture}[3]{
\stepcounter{lecture}

\fbox{\parbox[-2]{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}{\centering
\scriptsize{\scshape Lecture \thelecture}\\
\DTMdisplaydate{#3}{#2}{#1}{-1}}}}}}

\begin{document}

% Contents
\tableofcontents

\pagebreak

\newlecture{14}{10}{2021}
\section{Introduction}
\lipsum[1-5]
\lipsum[6-10]
\subsection{The Exam}
\lipsum[11-15]

\section{Some Basic Techniques}
\lipsum[16-20]
\subsection{Arithmetic Functions}
\lipsum[21-25]
\subsection{M\"obius Inversion and Dirichlet Convolution}
\lipsum[26-30]
\subsection{Partial Summation}
\lipsum[31-35]

\newlecture{14}{10}{2021}

\lipsum[36-40]
\subsection{The Euler--Maclaurin Formula}
\lipsum[41-45]
\newlecture{14}{10}{2021}
\section{Proof of the Prime Number Theorem}
\lipsum[46-50]
\subsection{Some Initial Bounds}
\lipsum[51-55]
\end{document}


This is based on Georgio's solution but using \addtocontents. The marginpar is aligned to the preceding \section or \subsection TOC entry. A blank line was added to show the break between two lectures.

\documentclass{article}
% Packages
\usepackage{lipsum,parskip}
\usepackage{datetime2}

% Counter declaration
\newcounter{lecture}

% Date format
\DTMlangsetup[en-GB]{monthyearsep={,\space}}

\newcommand{\newlecture}[3]{% see \DTMdisplaydate
\stepcounter{lecture}\label{lecture\thelecture}%
\protect\fbox{\protect\parbox[t]{\the\dimexpr\marginparwidth-2\fboxsep-2\fboxrule}{\protect\centering
\protect\scriptsize{\protect\scshape Lecture \thelecture}\\
\DTMdisplaydate{#3}{#2}{#1}{-1}}}}}}}%

\begin{document}

% Contents
\reversemarginpar
\tableofcontents

\pagebreak

\section{Introduction}\newlecture{14}{10}{2021}
\lipsum[1-5]
\lipsum[6-10]
\subsection{The Exam}
\lipsum[11-15]

\section{Some Basic Techniques}
\lipsum[16-20]
\subsection{Arithmetic Functions}
\lipsum[21-25]
\subsection{M\"obius Inversion and Dirichlet Convolution}
\lipsum[26-30]
\subsection{Partial Summation}
\lipsum[31-35]

\phantomsection\newlecture{14}{10}{2021}

\lipsum[36-40]
\subsection{The Euler--Maclaurin Formula}
\lipsum[41-45]

\section{Proof of the Prime Number Theorem}\newlecture{14}{10}{2021}
\lipsum[46-50]
\subsection{Some Initial Bounds}
\lipsum[51-55]
\end{document}

• Wow, this is great :) I think it would be perfect if the lectures are clickable also. Jan 28 at 21:24
• I added hyperlinks to the preceding section. Jan 29 at 6:02