如何将 \addtocontents 条目从目录链接到实际章节/部分

如何将 \addtocontents 条目从目录链接到实际章节/部分

我必须\addtocontents在某些地方使用它来完全自定义目录中某些行的数据(即包含或不包含页码、包含或不包含节/章号,并在某些情况下指定它们),但我无法使用,\addcontentsline因为它不允许我指定章节或节号。现在,条目显示在目录中,但我无法正确链接它们,有什么建议吗?

梅威瑟:

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}
\usepackage{hyperref}

\begin{document}

\tableofcontents

%======== USING REGULAR ENTRIES ========
\chapter{Chapter 1} % properly linked of course
{\LARGE This is chapter 1}

\section{Section 1-1} % well linked as well
{\LARGE This is chapter 1 section 1}
\clearpage

%======== USING \addcontentsline ========
\phantomsection
\addcontentsline{toc}{chapter}{Chapter 2} % works fine
{\LARGE This is chapter 2}
\clearpage

\phantomsection
\addcontentsline{toc}{section}{Section 2-1} % link works
{\LARGE This is chapter 2 section 1}
\clearpage

%======== USING \addtocontents ========
\phantomsection
\addtocontents{toc}{\protect\contentsline{chapter} % this link does not work
{\protect\numberline{3}Chapter 3}{\thepage}{}}
{\LARGE This is chapter 3}
\clearpage

\phantomsection
\addtocontents{toc}{\protect\contentsline{section}{\protect\numberline{3-1}Section 3-1}{\thepage}{section*.2.4}} % ... neither does this
{\LARGE This is chapter 3 section 1}
\clearpage

\end{document}

我知道我必须把链接作为最后一个参数,\contentsline但我无法让它工作......我曾尝试过:

chapter*.\thechapter
chapter*.\thepage
section*.\thesection

...等等...我可以让它工作,\page.<PAGENO>但我不认为这是一个很好的解决方案...此外,我不能有章节或部分,\section*{}因为我实际上甚至不想要一个章节标题或任何在所需位置的东西......

答案1

第一步是用 或 替换\phantomsection\refstepcounter{chapter}然后\refstepcounter{section},您将获得实际的章节和节号来链接,而不是chapter*.1等。

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}
\usepackage{hyperref}

\begin{document}

\tableofcontents

%======== USING REGULAR ENTRIES ========
\chapter{Chapter 1} % properly linked of course
{\LARGE This is chapter 1}

\section{Section 1-1} % well linked as well
{\LARGE This is chapter 1 section 1}
\clearpage

%======== USING \addcontentsline ========
\refstepcounter{chapter}
\addcontentsline{toc}{chapter}{Chapter 2} % works fine
{\LARGE This is chapter 2}
\clearpage

\refstepcounter{section}
\addcontentsline{toc}{section}{Section 2-1} % link works
{\LARGE This is chapter 2 section 1}
\clearpage

%======== USING \addtocontents ========
\refstepcounter{chapter}
\addtocontents{toc}{\protect\contentsline{chapter}% this link does not work
{\protect\numberline{\thechapter}Chapter \thechapter}{}{chapter.\thechapter}}
{\LARGE This is chapter 3}
\clearpage

\refstepcounter{section}
\addtocontents{toc}{\protect\contentsline{section}{\protect\numberline{\thechapter-\arabic{section}}Section -1}{}{section.\thesection}}% ... neither does this
{\LARGE This is chapter 3 section 1}
\clearpage

\end{document}

答案2

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}
\usepackage{hyperref}





\begin{document}

\tableofcontents


%======== USING REGULAR ENTRIES ========
\chapter{Chapter 1} % properly linked of course
{\LARGE This is chapter 1}

\section{Section 1-1} % well linked as well
{\LARGE This is chapter 1 section 1}
\clearpage

%======== USING \addcontentsline ========
\phantomsection
\addcontentsline{toc}{chapter}{Chapter 2} % works fine
{\LARGE This is chapter 2}
\clearpage

\phantomsection
\addcontentsline{toc}{section}{Section 2-1} % link works
{\LARGE This is chapter 2 section 1}
\clearpage

%======== USING \addtocontents ========
\phantomsection
\addcontentsline{toc}{chapter}{\numberline{3}Chapter}

{\LARGE This is chapter 3}
\clearpage

\phantomsection
\addcontentsline{toc}{section}{\protect\numberline{3-1}Section 3-1}
{\LARGE This is chapter 3 section 1}
\clearpage

\end{document}

编辑一些带有\chapternohead\sectionnohead命令的改进版本。这些命令的星号变体省略了页码。我将更新一个可以使用其他编号的版本。

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}
\usepackage{xparse}
\usepackage{hyperref}


\makeatletter

\NewDocumentCommand{\chapternohead}{som}{%
  \refstepcounter{chapter}%
  \typeout{Chapter is \thechapter~#3}
  \IfValueTF{#2}{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{chapter}{\numberline{\thechapter} #2}{}{chapter.\theHchapter}}
    }{%
      \addtocontents{toc}{\protect\contentsline{chapter}{\numberline{\thechapter} #2}{\thepage}{chapter.\theHchapter}}
    }%
  }{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{chapter}{\numberline{\thechapter} #3}{}{chapter.\theHchapter}}
    }{%
      \addtocontents{toc}{\protect\contentsline{chapter}{\numberline{\thechapter} #3}{\thepage}{chapter.\theHchapter}}
    }%
  } %
  {\huge \textbf{#3}}%
}

\NewDocumentCommand{\sectionnohead}{som}{%
  \refstepcounter{section}%
  \IfValueTF{#2}{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{section}{\numberline{\thesection} #2}{}{section.\theHsection}}
    }{%
      \addtocontents{toc}{\protect\contentsline{section}{\numberline{\thesection} #2}{\thepage}{section.\theHsection}}
    }%
  }{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{section}{\numberline{\thesection} #3}{}{section.\theHsection}}
      }{%
        \addtocontents{toc}{\protect\contentsline{section}{\numberline{\thesection} #3}{\thepage}{section.\theHsection}}
      }%
    }%
{\LARGE \textbf{#3}}%
}

\makeatother


\begin{document}

\tableofcontents


%======== USING REGULAR ENTRIES ========
\chapter{Chapter 1} % properly linked of course
\section{Section 1-1} % well linked as well

\phantomsection
\addcontentsline{toc}{section}{Section 2-1} % link works
{\LARGE This is chapter 2 section 1}
\clearpage

%======== USING \addtocontents ========

%\phantomsection
\chapternohead[This goes to toc]{Chapter}

{\LARGE This is chapter 3}
\clearpage

\sectionnohead{Section 3-1}
{\LARGE This is chapter 3 section 1}
\clearpage

\end{document}

一些改进:有第 4 个参数,用于指定章节号

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{tocloft}
\usepackage{xparse}
\usepackage{hyperref}

\newlength\chapternoheadnameskip
\setlength{\chapternoheadnameskip}{0.2em}

\newlength\sectionnoheadnameskip
\setlength{\sectionnoheadnameskip}{0.2em}


\makeatletter

\NewDocumentCommand{\chapternohead}{somD<>{\thechapter}}{%
  \refstepcounter{chapter}%
  \typeout{Chapter is \thechapter~#3}
  \IfValueTF{#2}{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{chapter}{#4\hspace{\chapternoheadnameskip}#2}{}{chapter.\theHchapter}}
    }{%
      \addtocontents{toc}{\protect\contentsline{chapter}{#4\hspace{\chapternoheadnameskip}#2}{\thepage}{chapter.\theHchapter}}
    }%
  }{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{chapter}{#4\hspace{\chapternoheadnameskip}#3}{}{chapter.\theHchapter}}
    }{%
      \addtocontents{toc}{\protect\contentsline{chapter}{#4\hspace{\chapternoheadnameskip}#3}{\thepage}{chapter.\theHchapter}}
    }%
  } %
  {\huge \textbf{#3}}%
}

\NewDocumentCommand{\sectionnohead}{somD<>{\thechapter-\thesection}}{%
  \refstepcounter{section}%
  \IfValueTF{#2}{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{section}{#4\hspace{\sectionnoheadnameskip}#2}{}{section.\theHsection}}
    }{%
      \addtocontents{toc}{\protect\contentsline{section}{\numberline{#4}\hspace{\sectionnoheadnameskip}#3}{\thepage}{section.\theHsection}}
    }%
  }{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{section}{#4\hspace{\sectionnoheadnameskip}#2}{}{section.\theHsection}}
      }{%
        \addtocontents{toc}{\protect\contentsline{section}{\numberline{#4}\hspace{\sectionnoheadnameskip}#3}{\thepage}{section.\theHsection}}
      }%
    }%
    {\LARGE \textbf{#3}}%
}

\makeatother


\begin{document}

\tableofcontents


%======== USING REGULAR ENTRIES ========
\chapter{Chapter 1} % properly linked of course
\section{Section 1-1} % well linked as well

\phantomsection
\addcontentsline{toc}{section}{Section 2-1} % link works
{\LARGE This is chapter 2 section 1}
\clearpage

%======== USING \addtocontents ========

\chapternohead[This goes to toc]{Chapter}<\textsl{\huge \textcolor{blue}{\thechapter}}>

{\LARGE This is chapter 3}
\clearpage

\sectionnohead*[Toc entry for section 3-1]{Section 3-1}<\textcolor{violet}{\thechapter-1}>
{\LARGE This is chapter 3 section 1}
\clearpage

\end{document}

在此处输入图片描述

相关内容