如何在目录中删除 \chapter 的页码并允许 \chapter* 的页码

如何在目录中删除 \chapter 的页码并允许 \chapter* 的页码

如何使用类删除页码但允许目录中\chapter的页码?\chapter*report

答案1

为了区分目录中的\chapter和 的管理,我创建了两个不同的 hangling 宏。第一个是传统的,我用丢弃其第二个参数(页码)的东西替换了它。第二个(for )复制了 for (或)所做的事情,但只是调用它。\chapter*\l@chapter\chapter*\l@chapter\chapter\l@schapter

以下最小示例有两个章节。第一章是常规的\chapter,而第二章是\chapter*。后者需要使用schapter类型手动插入到目录中(请参阅创建未编号的章节/部分(并将它们添加到目录和/或页眉))。

在此处输入图片描述

\documentclass{report}

\makeatletter
\let\l@schapter\l@chapter % Copy \l@chapter into \l@schapter
\let\old@l@chapter\l@chapter % Copy \l@chapter into \old@l@chapter
\renewcommand{\l@chapter}[2]{\old@l@chapter{#1}{}}% Discard second argument of \l@chapter
\makeatother

\begin{document}

\tableofcontents

\chapter{A chapter}

\chapter*{Another chapter}
\addcontentsline{toc}{schapter}{Another chapter}

\end{document}

答案2

这是一种实现我认为您正在寻找的方法,使用nameref和。tocloftetoolbox

它用于nameref将未编号的章节添加到目录中。之后\chapter*你可以看到我添加了两行。

\chapter要从目录中的编号中删除页码,我使用了tocloft\cftpagenumbersoff{chapter}\cftpagenumberson{chapter}命令。使用etoolbox我在 之前添加了\cftpagenumbersoff{chapter}实例,\chapter\cftpagenumberson{chapter}在 之后立即添加了 以恢复编号。请注意,您必须使用\@chapter才能将其专门应用于\chapter而不是\chapter*

资料来源:

将未编号的部分添加到目录中

将章节添加到目录中但不添加页码

是否可以在章节开头添加命令?

在此处输入图片描述

\documentclass{report}
\usepackage{nameref}
\usepackage{tocloft}
\usepackage{etoolbox}

\makeatletter
\pretocmd{\@chapter}{% <--- IMPORTANT
\addtocontents{toc}{\cftpagenumbersoff{chapter}}% <--- IMPORTANT
}{}{}
\apptocmd{\@chapter}{% <--- IMPORTANT
\addtocontents{toc}{\cftpagenumberson{chapter}}% <--- IMPORTANT
}{}{}
\makeatother

\begin{document}

\tableofcontents

\chapter{A chapter}

\chapter*{Another chapter}
\label{chap:test}
\addcontentsline{toc}{chapter}{\nameref{chap:test}}

\chapter{And another chapter}

\end{document}

答案3

我想我已经找到了问题的答案:

\documentclass[a4paper,12pt]{report}
\usepackage{graphicx,amsmath,amsthm,geometry} %enumerate% directly loaded from texstudio
\usepackage{lipsum} % for generating dummy texts
\usepackage{tocloft}  %for table of contents and list of figure formatting
\usepackage{fmtcount} % for textual representation of numbers
\usepackage{etoolbox} % for changing internal parameters of latex
\usepackage{fancyhdr}  % for pagestyles
\usepackage{times}    % for times new roman font
\usepackage[linktoc=all]{hyperref}   % for link to chapter and section. if you comment hyperref package, you must also comment all the \phantomsection in this code.
\usepackage{tikz} % for drawing
\usepackage{titletoc} %for table of content formatting

%%% this code centred, adjust chapters' distance from the title and convert chapter 1 to chapter one
    \makeatletter  
\def\@makechapterhead#1{%
    \vspace*{-60\p@}% default: 50pt % vertical distance between chapter and top of page
    {\parindent \z@ \centering \normalfont% default: \raggedright
        \ifnum \c@secnumdepth >\m@ne
        \huge\bfseries \@chapapp\space \NUMBERstring{chapter}%\the
        \par\nobreak
        \vskip 20\p@% default: 20\p@  % vertical distance between chapter and title below it
        \fi
        \interlinepenalty\@M
        \Huge \bfseries #1\par\nobreak
        \vskip 40\p@
}}
\def\@makeschapterhead#1{%
    \vspace*{-80\p@}% default: 50pt % vertical distance between unnumbered chapter and top of page
    {\parindent \z@ \centering% default: \raggedright
        \normalfont
        \interlinepenalty\@M
        \Huge \bfseries  #1\par\nobreak
        \vskip 40\p@
}}
\makeatother



%%% this code changes the names of contets, list of figures,abstract, chapters and references
\renewcommand{\contentsname}{\large{Table of Contents}}
\renewcommand{\listfigurename}{\large{List of Figures}}
\renewcommand{\abstractname}{\large{Abstract}}
\renewcommand{\bibname}{\large{References}}
\renewcommand{\chaptername}{\large{CHAPTER}}


%This code centres table of content
\renewcommand{\cfttoctitlefont}{\hfill\Large\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}

%This code centres list of figure
\renewcommand{\cftloftitlefont}{\hfill\Large\bfseries}
\renewcommand{\cftafterloftitle}{\hfill}

\linespread{1.8}

%this code redefines the chapter format in table of contents
\newlength{\len}
\titlecontents{chapter}[1.2em]{\bfseries}%\bfseries
{\contentslabel[\large{\chaptername} \NUMBERstringnum{\thecontentslabel}:]{3.6em}\settowidth{\len} {Chapter \NUMBERstringnum{\thecontentslabel}}\hspace*{\len}}%]
{\large{\thecontentslabel}\hspace*{-5.1cm}}%\contentslabel[]{}
{\hfill\contentspage}% replace \hfill with \cftdotfill{\cftdotsep} if you want dots in chapters in toc


% this code for list of figures with figure, cation and page.
\renewcommand{\cftafterloftitle}{\hfill{}
    \\[\baselineskip]\mbox{\textbf{Figure}}\hfill{\textbf{Caption}}\hfill{\normalfont \textbf{Page}}}


%this code removes the references label(their numbers) on the reference page
\makeatletter
\renewcommand\@biblabel[1]{}
\makeatother


\begin{document}

    \pagenumbering{roman}

    \fancyhf{}
    \fancyfoot[C]{\thepage}
    \pagestyle{fancy}
    \fancypagestyle{plain} 
    \fancyhf{}
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{0pt}

\phantomsection 
\addcontentsline{toc}{chapter}{\numberline{}Title Page}
\chapter*{Title}

\newpage
\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}Certification}

    \chapter*{\large{Certification}}

\newpage
\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}Dedication}

    \chapter*{\large{Dedication}}


\newpage
\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}Acknowledgements}

\chapter*{\large{Acknowledgements}}


\newpage

\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}Table of Contents}
\setlength{\cftbeforetoctitleskip}{-2.5cm} %to control vertical spacing
\tableofcontents
\clearpage
\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}List of Figures}
\setlength{\cftbeforeloftitleskip}{-2.5cm} %to control vertical spacing
\listoffigures 
\clearpage
\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}Abstract}
\chapter*{\large{Abstract}}


\newpage
\phantomsection 
\addcontentsline{toc}{chapter}{\numberline{}Nomenclatures}

    \chapter*{\large{Nomenclatures}}


\newpage  
\pagenumbering{arabic}

%this code redefines the chapter format in table of contents
\titlecontents{chapter}[1.2em]{\bfseries}%\bfseries
{\contentslabel[\large{\chaptername} \NUMBERstringnum{\thecontentslabel}:]{3.6em}\settowidth{\len} {Chapter \NUMBERstringnum{\thecontentslabel}}\hspace*{\len}}%]
{\large{\thecontentslabel}\hspace*{-5.1cm}}%\contentslabel[]{}
{\hfill\cftpagenumbersoff{chapter}}% 

\chapter{\large{INTRODUCTION}}

\fancyhf{}
%\fancyhead[C]{\thepage}
\pagestyle{fancy}
\fancypagestyle{plain} 
\fancyhf{}
\fancyhead[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}
%\pagenumbering{arabic}
\setcounter{page}{1}
\section{Introduction}

\section{Section 1}
\subsection{Subsection 1}
\subsection{Subsection 2}


\section{Section 2} 

\chapter{\large{LITERATURE REVIEW}}
\section{Section 1}

\section{Section 2}

 \chapter{\large{RESEARCH METHODOLOGY}}  
\section{Section 1}

\section{Section 2}



 \section{Section 3}

\chapter {\large{RESULTS AND DISCUSSION}}
\section{Section 1}
\section{Section 2}
\section{Section 3}

\chapter {\large{CONCLUSION}}
\section{Summary}
\section{Conclusion}
\section{Recommendation}
\newpage
\phantomsection

%this code redefines the chapter format in table of contents
\titlecontents{chapter}[1.2em]{\bfseries}
{\contentslabel[\large{\chaptername} \NUMBERstringnum{\thecontentslabel}:]{3.6em}\settowidth{\len} {Chapter \NUMBERstringnum{\thecontentslabel}}\hspace*{\len}}%]
{\large{\thecontentslabel}\hspace*{-5.1cm}}
{\hfill\contentspage}%

\addcontentsline{toc}{chapter}{\numberline{} REFERENCES}
\thispagestyle{empty}
\begin{thebibliography}{99}
\bibitem{1} First reference
\bibitem{2} Second reference
\bibitem{3} Third reference
\bibitem{4} Fourth reference
\bibitem{5} Fifth reference
\end{thebibliography}

\end{document}

答案4

\addtocontents{toc}{\cftpagenumbersoff{chapter}}在第一章之前和参考文献之前编写此代码\addtocontents{toc}{\cftpagenumberson{chapter}}以获得所需的结果。这是一个最小示例

\documentclass[a4paper,12pt]{report}
\usepackage{graphicx,amsmath,amsthm} %enumerate% directly loaded from texstudio
\usepackage[left=38.00mm, right=13.00mm, top=25.00mm, bottom=13.00mm]{geometry}
\usepackage{lipsum} % for generating dummy texts
\usepackage{tocloft}  %for table of contents and list of figure formatting
\usepackage{fmtcount} % for textual representation of numbers
\usepackage{etoolbox} % for changing internal parameters of latex
\usepackage{fancyhdr}  % for pagestyles
\usepackage{times}    % for times new roman font
%\usepackage[linktoc=all]{hyperref}   % for link to chapter and section
\usepackage{tikz} % for drawing
%\usepackage{titletoc} %for table of content formatting

%%% this code centred, adjust chapters' distance from the title and convert chapter 1 to chapter one
\makeatletter  
\def\@makechapterhead#1{%
    \vspace*{-60\p@}% default: 50pt % vertical distance between chapter and top of page
    {\parindent \z@ \centering \normalfont% default: \raggedright
        \ifnum \c@secnumdepth >\m@ne
        \huge\bfseries \@chapapp\space \NUMBERstring{chapter}%\the
        \par\nobreak
        \vskip 20\p@% default: 20\p@  % vertical distance between chapter and title below it
        \fi
        \interlinepenalty\@M
        \Huge \bfseries #1\par\nobreak
        \vskip 40\p@
}}
\def\@makeschapterhead#1{%
    \vspace*{-70\p@}% default: 50pt % vertical distance between unnumbered chapter and top of page
    {\parindent \z@ \centering% default: \raggedright
        \normalfont
        \interlinepenalty\@M
        \Huge \bfseries  #1\par\nobreak
        \vskip 40\p@
}}
\makeatother

%% This code rename 1 to CHAPTER ONE in table of contents:
\makeatletter  
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
    \refstepcounter{chapter}%
    \typeout{\@chapapp\space\thechapter.}%
    \addcontentsline{toc}{chapter}%
    {\protect \chaptername~\NUMBERstringnum{\thechapter}:~#1}%
    \else
    \addcontentsline{toc}{chapter}{#1}%
    \fi
    \chaptermark{#1}%
    \addtocontents{lof}{\protect\addvspace{10\p@}}%
    \addtocontents{lot}{\protect\addvspace{10\p@}}%
    \if@twocolumn
    \@topnewpage[\@makechapterhead{#2}]%
    \else
    \@makechapterhead{#2}%
    \@afterheading
    \fi}
\makeatother

%%% this code changes the names of contets, list of figures,abstract, chapters and references
\renewcommand{\contentsname}{\large{Table of Contents}}
\renewcommand{\listfigurename}{\large{List of Figures}}
\renewcommand{\abstractname}{\large{Abstract}}
\renewcommand{\bibname}{\large{References}}
\renewcommand{\chaptername}{\large{CHAPTER}}
%\renewcommand{\cftchappresnum}{\chaptername~}

%This code centres table of content
\renewcommand{\cfttoctitlefont}{\hspace*{6cm}\large\bfseries}
%\renewcommand{\cfttoctitlefont}{\hfill\large\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}

%This code centres list of figure
\renewcommand{\cftloftitlefont}{\hfill\large\bfseries}
\renewcommand{\cftafterloftitle}{\hfill}

\linespread{1.8}

% this code for list of tables with figure, cation and page.
\renewcommand{\cftafterloftitle}{\hfill{}
    \\[\baselineskip]\mbox{\textbf{Figure}}\hspace*{6cm}{\textbf{Caption}}\hfill{\normalfont \textbf{Page}}}

%this code removes the references label
\makeatletter
\renewcommand\@biblabel[1]{}
\makeatother


\begin{document}
\titlepage
    \pagenumbering{roman}   

%\phantomsection    
\addcontentsline{toc}{chapter}{\numberline{}Title Page}
\chapter*{Title}
\thispagestyle{empty}
\newpage

%\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}Certification}

    \chapter*{\large{Certification}}

\newpage
%\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}Dedication}

    \chapter*{\large{Dedication}}


\newpage
%\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}Acknowledgements}

\chapter*{\large{Acknowledgements}}

\newpage

%\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}Table of Contents}
\setlength{\cftbeforetoctitleskip}{-2.5cm} %to control vertical spacing
\tableofcontents
\clearpage
%\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}List of Figures}
\setlength{\cftbeforeloftitleskip}{-2.5cm} %to control vertical spacing
\listoffigures 
\clearpage
%\phantomsection
\addcontentsline{toc}{chapter}{\numberline{}Abstract}
\chapter*{\large{Abstract}}

\newpage
%\phantomsection    
\addcontentsline{toc}{chapter}{\numberline{}Nomenclatures}

    \chapter*{\large{Nomenclatures}}

\newpage  
\pagenumbering{arabic}
\addtocontents{toc}{\cftpagenumbersoff{chapter}}
\chapter{\large{INTRODUCTION}}

\fancyhf{}
%\fancyhead[C]{\thepage}
\pagestyle{fancy}
\fancypagestyle{plain} 
\fancyhf{}
\fancyhead[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}
%\pagenumbering{arabic}
\setcounter{page}{1}
\section{Introduction}

\section{Section 1}
\subsection{Subsection 1}
\subsection{Subsection 2}

\section{Section 2} 

\chapter{\large{LITERATURE REVIEW}}
\section{Section 1}

\newpage
%\phantomsection
\addtocontents{toc}{\cftpagenumberson{chapter}}
\addcontentsline{toc}{chapter}{\numberline{} REFERENCES}
\thispagestyle{empty}
\begin{thebibliography}{99}
\bibitem{1} First reference
\bibitem{2} Second reference
\end{thebibliography}

\end{document}

相关内容