\addcontentsline 链接没有带我到正确的页面

\addcontentsline 链接没有带我到正确的页面

代码如下:

\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}

目录上的链接可以点击,但它们无法将我带到正确的页码。我该如何解决这个问题?

答案1

正如@Celdor 指出的那样,我需要\phantomsection在之前使用\addcontentsline

以下是经过修正后的最终代码

\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}

相关内容