1

Set up

The layout of my project is this

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

enter image description here

The table of contents I want is this

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

enter image description here

Goal

How do I combine the two codes?

I.e. How do I put the table of contents, list of figures and list of tables from code 2 into code 1. I tried by copying code 2 into code 1, and then using \include{code 2}, but the formatting for the table came out very badly. (the word "contents" did not line with the text, and all number were in roman numerals)

  • I don't fully understand what you want, but have you tried Ctrl+C, Ctrl+V? – Michael Fraiman Jun 22 '17 at 20:29
  • No I copied the preambles into a separate .tex file and used \include{} to insert it into the main. I removed the packages which were duplicates, but some formatting errors came out. – UniStuffz Jun 23 '17 at 15:41
2

You need to copy the preamble of code 2 into the preamble of code 1 (here I did this right before begin{document}). Then some clean up is required. You must check the packages loaded for potential conflicts. In your case only the packages color (code 1) and xcolor (code 2) are an issue. But this is easy to solve, because xcolor is basically an extended version of color, so only load xcolor.

With other documents you would also have to throw out everything not needed for formatting the table of contents. But (besides blindtext) there is nothing in code 2.

The header for the toc (Contents Page No.) is treated as a normal paragraph and therefore indendented by \parindent. This can be removed with \noindent added to the \addtocontents line, but it must be \protected.

A part of the code 2 preamble centers the headline for \chapter*. This centers Contents but also Appendix and Bibliography. By commenting out this code (I marked it in the source) you get back to left aligned headlines.

Edit:

This code also adds \chapter* headlines to the TOC, if you set \schaptertoctrue after \tableofcontents (set before would also add a line for the TOC to the TOC). You can disable this with \schaptertocfalse before the \chpter* you don`t want in the 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}

Edit: Roman page numbers:

This is due to \frontmatter and \mainmatter in code 2 after \begin{document} (there is also \backmatter). \frontmatter sets lower case roman page numbering. Usually it's followed by abstract, TOC, LOF, LOT, and other stuff, which isn't the main contents of your document. The latter follows after \mainmatter, which sets the page numbering to arabic and resets the page number. So the first page of your document is page i and chapter 1 is on page 1. Without these commands \mainmatter is asumed, the first page of the document is page 1 and chapter 1 is on some higher numbered page. Note: this may require three latex runs, until everything is correct.

  • "The header for the toc (Contents Page No.) is treated as a normal paragraph" Thanks so much, this makes sense, I was trying to figure out what went wrong. Any idea how code 2 got \chapter*{}, \listoftables, and ` \listoffigures` to be included in the TOC using roman numerals? (look at image above) – UniStuffz Jun 23 '17 at 15:53
  • 1
    @UniStuffz: I added the answer to this to my answer (it was a bit to long for a comment. – Mike Jun 23 '17 at 19:58
  • Okay Final question, so I've added the numbering difference to my own code, and it makes sense \fontmatter for numbering before chapter 1, \mainmatter after and \backmatter for the appendix and things. However the appendix, LOF, LOT still aren't listed in the TOC just like code 2, any ideas? – UniStuffz Jun 23 '17 at 20:34
  • 1
    @UniStuffz: and another edit to also cover this. – Mike Jun 23 '17 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.