简体   繁体   English

如何使章节*、节*和小节*出现在目录中

[英]How to make chapter*, section* and subsection* appear in the table of contents

I need to produce a PDF document in which I need some "chapters" (along with its sections and subsections) to be non-numbered but still included in the ToC.我需要生成一个 PDF 文档,其中我需要一些“章节”(连同其部分和小节)没有编号但仍包含在 ToC 中。

This is for my master thesis.这是我的硕士论文。 I'm using the book document class, because I don't like memoir defaults.我正在使用 book 文档类,因为我不喜欢 memoir 默认值。

If I use \\chapter* , then LaTeX remove the chapter from the ToC.如果我使用\\chapter* ,则 LaTeX 从 ToC 中删除该章节。 But I'm required to have those as well in the ToC.但是我也必须在 ToC 中包含这些内容。 Furthermore, the headings (fancy) are not changed with \\chapter* .此外,标题(花式)不会随\\chapter*改变。

The overall structure of the thesis is:论文的总体结构是:

\maketitle %% A custom one
\frontmatter
\tableofcontents
\listoftables
\listoffigures

\chapter*{Abstract}
\chapter*{Introduction} %% This "chapter" presents the whole thesis

\mainmatter

%% Here the real chapters are written

\appendix
%% Appendixes here

%% bibliography

How can I make \\chapter* , \\section* , and \\subsection* to appear in the ToC and to modify the headers?如何使\\chapter*\\section*\\subsection*出现在 ToC 中并修改标题?

I think I may be using some packages that interfere with the way headers and footers are generated.我想我可能正在使用一些干扰页眉和页脚生成方式的包。 The answer of smilingthax gets the first part of the question responded: I have now my \\chapter*s on the TOC. smilethax的答案得到了问题的第一部分的回应:我现在在 TOC 上有我的 \\chapter*s。

This is my full preamble:这是我的完整序言:

\usepackage[sort&compress,round,semicolon]{natbib}
\usepackage{babel}
\usepackage{setspace}
%% inputenc so we can write in spanish
\usepackage[utf8]{inputenc}

\usepackage{fixltx2e} % LaTeX patches, \textsubscript
\usepackage{cmap} % fix search and cut-and-paste in PDF
\usepackage{ifthen}
%% \usepackage{float} % float configuration
%% \floatplacement{figure}{TH} % place figures here definitely

%% fontenc so we can use TrueType fonts
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{garamond}
\usepackage{graphicx}
\usepackage{titlesec}

\usepackage[table]{xcolor}
%% Custom colors
\definecolor{blue}{rgb}{0.2,0.2,0.95}
\definecolor{green}{rgb}{0.2,0.95,0.2}
\definecolor{red}{rgb}{0.95,0.2,0.2}
\definecolor{cyan}{rgb}{0,0,0.95}
\definecolor{ligthred}{rgb}{1, 0, 0}
\definecolor{black}{rgb}{0, 0, 0}

\definecolor{shade}{HTML}{D4D7FE} %light blue shade

% Margins
\usepackage[left=0.9in,top=1in,right=0.7in,bottom=1in]{geometry}


\usepackage[pdftex, colorlinks=true, citecolor=ligthred,
  urlcolor=blue]{hyperref}

\widowpenalty9000
\clubpenalty7000

\usepackage{titlesec}
\newcommand{\bigrule}{\titlerule[0.5mm]}

\renewcommand{\rmdefault}{bch} 

\titleformat{\chapter}[display]
{\bfseries\Huge}
{\garamond
% DESCOMENTAR PARA SUBIR LOS CAPITULOS
\vspace{-1.125in} \titlerule \filleft
\Large\chaptertitlename\ \Large\thechapter}{0mm}
{\filleft}[\vspace{0.5mm} \bigrule]

\let\cite=\citep

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}  %% Clears all headers

% admonition (specially marked topic)
\providecommand{\DUadmonition}[2][class-arg]{%
  % try \DUadmonition#1{#2}:
  \ifcsname DUadmonition#1\endcsname%
    \csname DUadmonition#1\endcsname{#2}%
  \else
    \begin{center}
      \fbox{\parbox{0.9\textwidth}{#2}}
    \end{center}
  \fi
}

% title for topics, admonitions and sidebar
\providecommand*{\DUtitle}[2][class-arg]{%
  % call \DUtitle#1{#2} if it exists:
  \ifcsname DUtitle#1\endcsname%
    \csname DUtitle#1\endcsname{#2}%
  \else
    \smallskip\noindent\textbf{#2}\smallskip%
  \fi
}

% error admonition title
\providecommand*{\DUtitleerror}[1]{\DUtitle{\color{red}#1}}

% fieldlist environment
\ifthenelse{\isundefined{\DUfieldlist}}{
  \newenvironment{DUfieldlist}%
    {\quote\description}
    {\enddescription\endquote}
}{}

% legend
\ifthenelse{\isundefined{\DUlegend}}{
  \newenvironment{DUlegend}{\small}{}
}{}

%%% Fallback definitions for Docutils-specific commands
% numeric or symbol footnotes with hyperlinks
\providecommand*{\DUfootnotemark}[3]{%
  \hyperlink{#2}{\textsuperscript{#3}}\raisebox{1em}{\label{#1}}%
}

\providecommand{\DUfootnotetext}[4]{%
  \begingroup%
  \renewcommand{\thefootnote}{%
    \protect\hyperlink{#2}{#3}}%
  \protect\raisebox{1em}{\protect\label{#1}}%
  \footnotetext{#4}%
  \endgroup%
}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{longtable}
\newlength{\DUtablewidth} % internal use in tables


\usepackage{tikz}
\usepackage{bbding}

\usetikzlibrary{arrows,fit}
\usepackage{amsmath,bm,times}
\newcommand{\mx}[1]{\mathbf{\bm{#1}}} % Matrix command
\newcommand{\vc}[1]{\mathbf{\bm{#1}}} % Vector command

I don't think an specialized command exists for that. 我认为不存在专门的命令。 But you can use 但你可以使用

  \addcontentsline{toc}{chapter}{#1}

to add it to the TOC. 将其添加到TOC。 BTW, I didn't have problems with \\chapter* and fancy, so I used: 顺便说一句,我没有问题\\章*和花哨,所以我使用:

\newcommand\chap[1]{%
  \chapter*{#1}%
  \addcontentsline{toc}{chapter}{#1}}

Variations on this question have been asked and answered several times on the TeX-specific sister site : TeX特定的姐妹网站上已经多次询问和回答了这个问题的变化:

I'm going to copy over Werner's answer to the last of those, because it demonstrates a different technique from any of the existing answers to this question: redefine \\section so that the only effect of \\section* is to skip printing the section numbers. 我要将Werner的答案复制到最后一个,因为它演示了与这个问题的任何现有答案不同的技术:重新定义\\section以便\\section*唯一效果是跳过打印段号。 This will work even when sectioning commands are being issued from the guts of packages you don't control. 即使从您无法控制的软件包的内容发出切片命令,这也会起作用。


[...] Redefine \\section to capture and condition on when the starred-version is used. [...]重新定义\\section以捕获和调整使用星号版本的时间。 Upon finding \\section* , issue it just like you would \\section , but remove the number-printing mechanism through an appropriate setting of the counter secnumdepth . 找到\\section* ,发出它就像你的\\section ,但是通过计数器secnumdepth的适当设置删除数字打印机制。

xparse provides an easy interface for (re)defining commands that may have a s tarred version, as well as an o ptional argument. xparse提供可以具有(重新)定义命令一个简单的界面s柏油版本,以及一个o ptional参数。

\usepackage{xparse}

\let\oldsection\section
\makeatletter
\newcounter{@secnumdepth}
\RenewDocumentCommand{\section}{s o m}{%
  \IfBooleanTF{#1}
    {\setcounter{@secnumdepth}{\value{secnumdepth}}% Store secnumdepth
     \setcounter{secnumdepth}{0}% Print only up to \chapter numbers
     \oldsection{#3}% \section*
     \setcounter{secnumdepth}{\value{@secnumdepth}}}% Restore secnumdepth
    {\IfValueTF{#2}% \section
       {\oldsection[#2]{#3}}% \section[.]{..}
       {\oldsection{#3}}}% \section{..}
}
\makeatother

(To do the same thing to \\chapter , \\subsection , etc., search-and-replace section appropriately, and adjust the temporary value used for secnumdepth .) (对\\chapter\\subsection等做同样的事情,适当地搜索和替换section ,并调整用于secnumdepth的临时值。)

\\setcounter{secnumdepth}{-1}

worked for me as a solution if you used the un-asterisk version of \\chapter{} etc for the book class. 如果您使用图书类的非星号版本的\\ chapter {}等,我可以为我工作。 Hyperref works as well if you wanted a linkable ToC along with my solution. 如果您想要一个可链接的ToC以及我的解决方案,Hyperref也可以工作。 Make sure to compile it twice to get it to work on both the ToC and inline. 确保将其编译两次以使其在ToC和inline上都能正常工作。

Unfortunately however, your preamble killed my TexStudio attempt to check to see if it worked for your exact code, but I have faith. 不幸的是,你的序言杀了我的TexStudio试图查看它是否适用于你的确切代码,但我有信心。

Used TeXstudio 2.6.2 (SVN 4110M) Using Qt Version 4.8.5, compiled with Qt 4.8.5 R On Windows 7 64-bit 使用TeXstudio 2.6.2(SVN 4110M)使用Qt版本4.8.5,使用Qt 4.8.5 R编译在Windows 7 64位上

I have solved it using the following:我已经使用以下方法解决了它:

\chapter*{Introduction} 
\addcontentsline{toc}{chapter}{Introduction}

I have just added this line \\addcontentsline{toc}{chapter}{Introduction} after the \\chapter {Introduction}*.我刚刚在\\chapter {Introduction}* 之后添加了这一行\\addcontentsline{toc}{chapter}{Introduction} Then I have written my writings of this chapter.然后我写了这一章的文字。 Now it appears in the table of contents perfectly.现在它完美地出现在目录中。

I had the same problem and solved it with smilingthax' answer and comments. 我有同样的问题,并用smilingthax的回答和评论解决了。 As it didn't work to use only \\leftmark or \\rightmark , here is a command that you can use to (i) make a chapter non-numbered (ii) add it to the TOC and (iii) have a correct page header. 因为它不能仅使用\\leftmark\\rightmark ,所以这是一个命令,您可以使用它来(i)制作一个非编号的章节(ii)将其添加到TOC和(iii)具有正确的页面标题。

\newcommand\chap[1]{
    \chapter*{#1}
    \addcontentsline{toc}{chapter}{#1}
    \markboth{#1}{#1}}

The same works for sections (and subsections respectively): 对于部分(和子部分)也是如此:

 \newcommand\secTOC[1]{
    \section*{#1}
    \addcontentsline{toc}{section}{#1}
    \markboth{#1}{#1}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM