1

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:

MWE

Here is the code for the MWE:

\documentclass{article}

% Packages
\usepackage{lipsum,parskip}
\usepackage[hidelinks]{hyperref}
\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}}%
    \addcontentsline{toc}{section}{\normalfont\itshape Lecture \thelecture~(\DTMdisplaydate{#3}{#2}{#1}{-1})}}

\begin{document}
    
    % Contents
    \tableofcontents
    
    \pagebreak
    
    
    \newlecture{14}{10}{2021}
    \section{Introduction}
    \lipsum[1-5]
    \subsection{What this course is about}
    \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}

Now I think it would make the table of contents look cleaner if, Instead of a new line for lectures, I had a marginnote, something like this:

enter image description here

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).

2 Answers 2

2

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}

    \addcontentsline{toc}{section}{\protect\marginpar{
            \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[hidelinks]{hyperref}
\usepackage{datetime2}

% Counter declaration
\newcounter{lecture}

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

\reversemarginpar


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

    \addcontentsline{toc}{section}{\protect\marginpar{
            \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]
    \subsection{What this course is about}
    \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}
0
2

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[hidelinks]{hyperref}
\usepackage{datetime2}

% Counter declaration
\newcounter{lecture}

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

\newcommand{\newlecture}[3]{% see \DTMdisplaydate
    \stepcounter{lecture}\label{lecture\thelecture}%
    \addtocontents{toc}{\protect\marginpar{%
        \protect\hyperlink{\protect\getrefbykeydefault{lecture\thelecture}{anchor}{Doc-Start}}{%
            \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]
    \subsection{What this course is about}
    \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]
    
    \addtocontents{toc}{\protect\vskip 10pt}%
    \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}
2
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.