如何合并两个代码(将目录插入到项目布局中)

如何合并两个代码(将目录插入到项目布局中)

设置

我的项目布局是这样的

代码1:

\documentclass[12pt, oneside]{book}

\usepackage{sectsty,fancyhdr}
\sectionfont{\LARGE\bfseries}
\usepackage{geometry}

\usepackage{lipsum}%remove 





% *******************************Margin Settings*******************************

\geometry{% margin settings, has to go first before \pagestyle{}
paper=a4paper, 
inner=2.5cm, % Inner margin
outer=2.5cm, % Outer margin
bindingoffset=0.0cm, % Binding offset
top=2.5cm, % Top margin
bottom=2.5cm, % Bottom margin
headsep=4.5mm,% controles header  
footskip=7.4mm,% controles footer
%showframe,% show how the type block is set on the page
}

% ***************************Header/Footer Settings****************************

 \sectionfont{\LARGE\bfseries}%this will keep the formattings of sections
\pagestyle{fancy}
\fancyhf{} % sets all head and foot elements empty.
\rhead{} % right side of upper footer 
\lhead{Project title} %left side 
\renewcommand{\headrulewidth}{2.0pt} % Width of the top line
\renewcommand{\footrulewidth}{1.0pt} % Bottom line 
\setlength{\headheight}{14.5pt}%used to remove the \fancyhf{} \headheight error
\rfoot{Page \thepage} % vice versa with the page number 
\lfoot{\leftmark} % Should show the name of section 

\makeatletter
\renewcommand{\sectionmark}[1]{%this avoids the \makecaptical of footers
    \markboth{\ifnum \c@secnumdepth>\z@ %this allowes symbols in foorters
  \thesection\hskip 1em\relax
\fi #1}{}}
\makeatother

% ***************************title Settings****************************


\newcommand{\mytitle}{

\begin{center}
{\Huge{\textbf{Project of...}}} 
\end{center}

\begin{center}
 \textsc{\Large My name}\\[0.5cm] 
\end{center}

\begin{center}
{\large My ID number}\\[0.5cm] 
\end{center}

}


% ********************************NEW CHAPTER PAGE SETTING*********************
\fancypagestyle{chapterstart}{% 1st page of the chapters, i.e. no new page for chapter
}
\makeatletter

\def\chapter{
\pagestyle{chapterstart}
\secdef\@chapter\@schapter}


 \renewcommand\thesection{\@arabic\c@section} 


 \makeatother
% ********************************CHAPTER STYLE SETTING***********************
\usepackage{geometry}
\usepackage[T1]{fontenc}
\usepackage{graphicx}%**change
\usepackage{titlesec, color} %remove blindtext after
\definecolor{gray75}{gray}{0.75} %1=white, 0=black
\newcommand{\hsp}{\hspace{20pt}} %spacing between line and text
\titleformat{\chapter}[hang]{\vspace*{-2em}\Huge\bfseries}{\scalebox{1}    {\thechapter}\hsp\textcolor{gray75}{|}\hsp}{0pt}{} %spacing across entire page     %CHANGES** \scalebox{2}{}**3pt, **\vspace{+ down - up}
    \titlespacing{\chapter}{0pt}{50pt}{30pt} %change{right}{shift down}{tile and text}

 %------------------------------------------------------------------

 % Begin Project

 %------------------------------------------------------------------

 \begin{document}

 %-------------------------------Title-------------------------------
 \mytitle

 %------------------------------Begin--------------------------------

 \chapter{Minor introduction}
 \section{Introduction}
 \subsection{Super introduction}
 \lipsum[4-11]%remove

 \newpage 
 \chapter{Something more}
 \section{ssection 1}
 \subsection{subection 1}
 \lipsum

 \newpage 
 \chapter*{Appendix}
 \section*{section}
 \lipsum

 \newpage 
 \chapter*{Bibliography}
 \lipsum




  %------------------------------------------------------------------

  % end Project

  %------------------------------------------------------------------ 


  \end{document}

在此处输入图片描述

我想要的目录是这样的

代码2:

\documentclass[oneside]{book}

\renewcommand\contentsname{CONTENTS}
\renewcommand\listtablename{LIST OF TABLES}
\renewcommand\listfigurename{LIST OF FIGURES}
\addtocontents{toc}{{\bfseries Contents\hfill Page No.\bigskip\par}}
\addtocontents{lot}{{\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}}
\addtocontents{lof}{{\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}}

\usepackage[titles]{tocloft}
\usepackage{xcolor,xpatch}
\renewcommand\cftbeforechapskip{0ex}
\renewcommand\cftchapfont{\mdseries}
\renewcommand\cftchappagefont{\mdseries}
\renewcommand\cftchappresnum{Chapter~}
\renewcommand\cftchapaftersnum{: }
\newlength\tocindent
\settowidth\tocindent{\cftchapfont\cftchappresnum9\cftchapaftersnum}
\edef\cftchapnumwidth{\the\tocindent}
\edef\cftsecindent{\the\tocindent}
\advance\tocindent2.3em
\edef\cftsubsecindent{\the\tocindent}
\advance\tocindent3.2em
\edef\cftsubsubsecindent{\the\tocindent}
\renewcommand\cftsecdotsep{\cftnodots}
\renewcommand\cftsubsecdotsep{\cftnodots}
\renewcommand\cftsubsubsecdotsep{\cftnodots}
\newcommand\tocmainmatter
  {\renewcommand\cftchappagefont{\color{white}}%
  }
    \xapptocmd\mainmatter{\addtocontents{toc}{\protect\tocmainmatter}}{}{}


\newif\ifschaptertoc
\makeatletter
\renewcommand\@makeschapterhead[1]%
 {{\parindent \z@ \centering
    \normalfont
    \interlinepenalty\@M
    \Large \bfseries  #1\par\nobreak
    \vskip 20\p@
  }%
  \@mkboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}%
  \ifschaptertoc
    \addcontentsline{toc}{chapter}{#1}%
  \fi
 }
\makeatother

\usepackage{blindtext}

%dots
\renewcommand{\cftpartleader}{\cftdotfill{\cftdotsep}} % for parts
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} % for chapters
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}} % for sections
\renewcommand{\cftsubsecleader}{\cftdotfill{\cftdotsep}} % for subsection




%--------------------------------------------------------------------------
\begin{document}
\frontmatter

\tableofcontents
\schaptertoctrue

%------------------------------------
\chapter*{ACKNOWLEDGEMENTS}
\blindtext
%------------------------------------
\chapter*{ABSTRACT}
\blindtext
%------------------------------------
\listoftables

\listoffigures
%------------------------------------
\chapter*{ABBREVIATIONS}
\Blindtext
%------------------------------------
\chapter*{NOTATIONS}
\Blindtext

\mainmatter
%------------------------------------
\chapter{INTRODUCTION}
\blindtext
%************************************
\section{General Background}
\blindtext

  \section{Objective}
\blindtext
\subsection{Objective 1}
\blindtext
\subsection{Objective 2}
\blindtext
\subsection{Objective 3}
\blindtext

\section{Scheme}
\blindtext
\end{document}

在此处输入图片描述

目标

我该如何将这两个代码结合起来?

即如何将代码 2 中的目录、图片列表和表格列表放入代码 1 中。我尝试将代码 2 复制到代码 1 中,然后使用 \include{code 2},但表格的格式非常糟糕。(“内容”一词与文本不一致,所有数字都是罗马数字)

答案1

您需要将代码 2 的序言复制到代码 1 的序言中(我在这里就在 之前完成了这项工作begin{document})。然后需要进行一些清理。您必须检查已加载的软件包是否存在潜在冲突。在您的例子中,只有软件包color(代码 1)和xcolor(代码 2)存在问题。但这很容易解决,因为xcolor基本上是 的扩展版本color,所以只需加载xcolor

对于其他文档,您还必须删除格式化目录不需要的所有内容。但(除此之外blindtext)代码 2 中没有任何内容。

toc(目录页码)的页眉被视为普通段落,因此以 缩进\parindent。这可以删除并\noindent添加到\addtocontents行中,但必须\protect编辑。

代码 2 序言的一部分将标题置于\chapter*。这将内容但是也附录参考书目通过注释掉此代码(我在源代码中标记了它),您可以回到左对齐标题。

编辑:

\chapter*如果您将标题设置\schaptertoctrue为 after \tableofcontents(将标题设置为 before 也会将 TOC 的一行添加到 TOC),此代码还会将标题添加到 TOC。您可以使用\schaptertocfalsebefore禁用此功能,因为\chpter*您不需要将其添加到 TOC 中。

\documentclass[12pt, oneside]{book}

\usepackage{sectsty,fancyhdr}
\sectionfont{\LARGE\bfseries}
\usepackage{geometry}

\usepackage{lipsum}%remove 

% *******************************Margin Settings*******************************

\geometry{% margin settings, has to go first before \pagestyle{}
paper=a4paper, 
inner=2.5cm, % Inner margin
outer=2.5cm, % Outer margin
bindingoffset=0.0cm, % Binding offset
top=2.5cm, % Top margin
bottom=2.5cm, % Bottom margin
headsep=4.5mm,% controles header  
footskip=7.4mm,% controles footer
%showframe,% show how the type block is set on the page
}

% ***************************Header/Footer Settings****************************

 \sectionfont{\LARGE\bfseries}%this will keep the formattings of sections
\pagestyle{fancy}
\fancyhf{} % sets all head and foot elements empty.
\rhead{} % right side of upper footer 
\lhead{Project title} %left side 
\renewcommand{\headrulewidth}{2.0pt} % Width of the top line
\renewcommand{\footrulewidth}{1.0pt} % Bottom line 
\setlength{\headheight}{14.5pt}%used to remove the \fancyhf{} \headheight error
\rfoot{Page \thepage} % vice versa with the page number 
\lfoot{\leftmark} % Should show the name of section 

\makeatletter
\renewcommand{\sectionmark}[1]{%this avoids the \makecaptical of footers
    \markboth{\ifnum \c@secnumdepth>\z@ %this allowes symbols in foorters
  \thesection\hskip 1em\relax
\fi #1}{}}
\makeatother

% ***************************title Settings****************************

\newcommand{\mytitle}{

\begin{center}
{\Huge{\textbf{Project of...}}} 
\end{center}

\begin{center}
 \textsc{\Large My name}\\[0.5cm] 
\end{center}

\begin{center}
{\large My ID number}\\[0.5cm] 
\end{center}

}

% ********************************NEW CHAPTER PAGE SETTING*********************
\fancypagestyle{chapterstart}{% 1st page of the chapters, i.e. no new page for chapter
}
\makeatletter

\def\chapter{
\pagestyle{chapterstart}
\secdef\@chapter\@schapter}

 \renewcommand\thesection{\@arabic\c@section} 

 \makeatother
% ********************************CHAPTER STYLE SETTING***********************
\usepackage{geometry}
\usepackage[T1]{fontenc}
\usepackage{graphicx}%**change
% replace color by xcolor (both are for color support, but xcolor is used in code 2 preamble)
\usepackage{titlesec,xcolor} %remove blindtext after
\definecolor{gray75}{gray}{0.75} %1=white, 0=black
\newcommand{\hsp}{\hspace{20pt}} %spacing between line and text
\titleformat{\chapter}[hang]{\vspace*{-2em}\Huge\bfseries}{\scalebox{1}    {\thechapter}\hsp\textcolor{gray75}{|}\hsp}{0pt}{} %spacing across entire page     %CHANGES** \scalebox{2}{}**3pt, **\vspace{+ down - up}
    \titlespacing{\chapter}{0pt}{50pt}{30pt} %change{right}{shift down}{tile and text}

 %------------------------------------------------------------------

 %- preamble from code 2 -------------------------------------------

\renewcommand\contentsname{CONTENTS}
\renewcommand\listtablename{LIST OF TABLES}
\renewcommand\listfigurename{LIST OF FIGURES}
% added \protect\noindent to remove \parindent from table header
\addtocontents{toc}{{\protect\noindent\bfseries Contents\hfill Page No.\bigskip\par}}
\addtocontents{lot}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}}
\addtocontents{lof}{{\protect\noindent\bfseries No.\hfill Title\hfill\hfill Page No.\bigskip\par}}

\usepackage[titles]{tocloft}
% xcolor removed, because it's loaded above
\usepackage{xpatch}
\renewcommand\cftbeforechapskip{0ex}
\renewcommand\cftchapfont{\mdseries}
\renewcommand\cftchappagefont{\mdseries}
\renewcommand\cftchappresnum{Chapter~}
\renewcommand\cftchapaftersnum{: }
\newlength\tocindent
\settowidth\tocindent{\cftchapfont\cftchappresnum9\cftchapaftersnum}
\edef\cftchapnumwidth{\the\tocindent}
\edef\cftsecindent{\the\tocindent}
\advance\tocindent2.3em
\edef\cftsubsecindent{\the\tocindent}
\advance\tocindent3.2em
\edef\cftsubsubsecindent{\the\tocindent}
\renewcommand\cftsecdotsep{\cftnodots}
\renewcommand\cftsubsecdotsep{\cftnodots}
\renewcommand\cftsubsubsecdotsep{\cftnodots}
\newcommand\tocmainmatter
  {\renewcommand\cftchappagefont{\color{white}}%
  }
    \xapptocmd\mainmatter{\addtocontents{toc}{\protect\tocmainmatter}}{}{}

% centers \chapter* headers
% if unwanted comment out ...
\newif\ifschaptertoc
\makeatletter
\renewcommand\@makeschapterhead[1]%
 {{\parindent \z@ \centering
    \normalfont
    \interlinepenalty\@M
    \Large \bfseries  #1\par\nobreak
    \vskip 20\p@
  }%
  \@mkboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}%
  \ifschaptertoc
    \addcontentsline{toc}{chapter}{#1}%
  \fi
 }
\makeatother
% ... to here

%\usepackage{blindtext} thrown out, one dummy text package is enough

%dots
\renewcommand{\cftpartleader}{\cftdotfill{\cftdotsep}} % for parts
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} % for chapters
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}} % for sections
\renewcommand{\cftsubsecleader}{\cftdotfill{\cftdotsep}} % for subsection

 %------------------------------------------------------------------

 % Begin Project

 %------------------------------------------------------------------

 \begin{document}
% \mainmatter

 \tableofcontents
 \schaptertoctrue % enable adding \chapter* headlines to the TOC

 %-------------------------------Title-------------------------------
 \mytitle

 %------------------------------Begin--------------------------------

 \chapter{Minor introduction}
 \section{Introduction}
 \subsection{Super introduction}
 \lipsum[4-11]%remove

 \newpage 
 \chapter{Something more}
 \section{ssection 1}
 \subsection{subection 1}
 \lipsum

 %\schaptertocfalse % to disable adding \chapter* headline to the TOC
 \newpage 
 \chapter*{Appendix}
 \section*{section}
 \lipsum

 \newpage 
 \chapter*{Bibliography}
 \lipsum




  %------------------------------------------------------------------

  % end Project

  %------------------------------------------------------------------ 


  \end{document}

编辑:罗马页码:

这是因为代码 2 中的\frontmatter和之后(还有)。设置小写罗马页码。通常它后面跟着摘要、目录、LOF、LOT 和其他内容,这些都不是文档的主要内容。后者跟在 之后,将页码设置为阿拉伯语并重置页码。因此,文档的第一页是第 i 页,第 1 章在第 1 页。如果没有这些命令,则假定文档的第一页是第 1 页,第 1 章在某个编号较高的页面上。注意:这可能需要运行三次 latex,直到一切正确。\mainmatter\begin{document}\backmatter\frontmatter\mainmatter\mainmatter

相关内容