3

Here's the code:

\documentclass[a4paper]{article}

\usepackage{cmbright}
\usepackage{stix}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{amsmath, amssymb, amsthm}
\usepackage{mathtools}
\usepackage{tikz-cd}
\usepackage{titlesec}
\usepackage{pgfmath}
\usepackage{pgfcalendar}
\usepackage{hyperref}
\hypersetup{hidelinks}

\let\d=\pgfcalendarshorthand
\newcommand\formatdate[2]{\pgfcalendar{cal}{#1}{#1}{#2}}

\newcommand\firstdate{2022-01-01}
\newcommand\lastdate{2022-01-30}

\newcommand\grayrule{{\color{gray} \noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}}}

\renewcommand*\contentsname{Days}

\begin{document}
  % Table of Contents
  \tableofcontents
  \newpage
  
  \pgfcalendar{cal}{\firstdate}{\lastdate}{
    % Set a couple of variables
    \def\formatteddate{\d{y}0/\d{m}0/\d{d}0} % EXM: 2022/06/10
    \def\todayformatted{\d{y}0-\d{m}0-\d{d}0} % EXM: 2022-06-10

    \def\firstdayofyear{2022-01-01} % EXM: 2022-01-01

    \def\firstdayofmonth{\d{y}0-\d{m}0-01} % EXM: 2022-08-01
    \def\monthgoalsformat{./\d{y}0/\d{m}0/goals.tex} % EXM: 2022/01/goals.tex

    % Check if it's the first day of the year
    \pgfcalendarifdate{\todayformatted}{equals=\firstdayofyear}{
      % Add the year to the table of contents
      \noindent{\Huge \d{y}0}
      \addcontentsline{toc}{section}{\d{y}0}%
      \newline
      \newline
    }{}

    \ifdate{equals=\firstdayofmonth}{
      % Add the month to the table of contents
      % \section*{\d{m}t}
      \noindent{\LARGE \d{m}t}
      \addcontentsline{toc}{subsection}{\d{m}t}
      \newline
      \newline
      % Add the goals to the table of contents
      \noindent{\Large Goals for \d{m}t}
      \addcontentsline{toc}{subsubsection}{Goals for \textbf{\d{m}t}}
    }{}
    
    \noindent{\Large \d{m}t \d{d}- \d{y}-}
    \addcontentsline{toc}{subsubsection}{\d{m}t \d{d}0. \d{y}0}
    \newpage
  }
\end{document}

The links on the table of contents are clickable, but they don't bring me to the correct page number. How can I fix this?

1
  • 3
    Try to insert \phantomsection before each line containing a date, e.g. \phantomsection\noindent{\Large Goals for \d{m}t}%
    – Celdor
    Commented Jul 20, 2022 at 0:57

1 Answer 1

4

As @Celdor pointed out, I need to use \phantomsection before \addcontentsline.

Here's how the final code looks like with the correction

\documentclass[a4paper]{article}

\usepackage{cmbright}
\usepackage{stix}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{amsmath, amssymb, amsthm}
\usepackage{mathtools}
\usepackage{tikz-cd}
\usepackage{titlesec}
\usepackage{pgfmath}
\usepackage{pgfcalendar}
\usepackage{hyperref}
\hypersetup{hidelinks}

\let\d=\pgfcalendarshorthand
\newcommand\formatdate[2]{\pgfcalendar{cal}{#1}{#1}{#2}}

\newcommand\firstdate{2022-01-01}
\newcommand\lastdate{2022-01-30}

\newcommand\grayrule{{\color{gray} \noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}}}

\renewcommand*\contentsname{Days}

\begin{document}
  % Table of Contents
  \tableofcontents
  \newpage
  
  \pgfcalendar{cal}{\firstdate}{\lastdate}{
    % Set a couple of variables
    \def\formatteddate{\d{y}0/\d{m}0/\d{d}0} % EXM: 2022/06/10
    \def\todayformatted{\d{y}0-\d{m}0-\d{d}0} % EXM: 2022-06-10

    \def\firstdayofyear{2022-01-01} % EXM: 2022-01-01

    \def\firstdayofmonth{\d{y}0-\d{m}0-01} % EXM: 2022-08-01
    \def\monthgoalsformat{./\d{y}0/\d{m}0/goals.tex} % EXM: 2022/01/goals.tex

    % Check if it's the first day of the year
    \pgfcalendarifdate{\todayformatted}{equals=\firstdayofyear}{
      % Add the year to the table of contents
      \noindent{\Huge \d{y}0}
      \phantomsection\addcontentsline{toc}{section}{\d{y}0}%
      \newline
      \newline
    }{}

    \ifdate{equals=\firstdayofmonth}{
      % Add the month to the table of contents
      % \section*{\d{m}t}
      \noindent{\LARGE \d{m}t}
      \phantomsection\addcontentsline{toc}{subsection}{\d{m}t}
      \newline
      \newline
      % Add the goals to the table of contents
      \noindent{\Large Goals for \d{m}t}
      \phantomsection\addcontentsline{toc}{subsubsection}{Goals for \textbf{\d{m}t}}
    }{}
    
    \noindent{\Large \d{m}t \d{d}- \d{y}-}
    \phantomsection\addcontentsline{toc}{subsubsection}{\d{m}t \d{d}0. \d{y}0}
    \newpage
  }
\end{document}
3
  • 1
    This should be a comment or edited into the question as an addendum. Commented Jul 20, 2022 at 1:35
  • @barbarabeeton I came across a similar issue today. The answer here perfectly answered my situation. Using comments to answer questions is highly inappropriate on Stack Exchange, since "comments are ephemeral". And editing the question with the answer would make the question obsolete.
    – Peilonrayz
    Commented Aug 29, 2023 at 0:27
  • You've taken care of the problem by adding substantive details, which weren't present when I commented, Now it's a real answer. Commented Aug 29, 2023 at 0:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .