3

How to add header (containing chapter, section titles) to toc-entry and delete any headings in body? The purpose of doing this is I have lot of chapters, sections, subsection and paragraphs with lots of diagrams and these headings are occupying a full page and increasing number of pages. My goal is to use the minimum number of pages. Please find below a MWE:

\documentclass{report}
\usepackage{listings}
\usepackage{fancyhdr}
\pagestyle{fancy}

\lhead{\nouppercase{\rightmark} (\nouppercase{\leftmark})}
\chead{}
\rhead{}

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

\begin{document}
\tableofcontents

\part{Humanoid Introduction} 
\chapter{Obstacle Avoidance}
\section{detecting wall}
\chapter{Face Recognition}
\section{Shape Recognition}
\subsection{Face Recognisation}
\subsection{Hand Recognisation}

\end{document}
3
  • Also I don't want to use any new packages please .
    – user80596
    Jun 24, 2015 at 10:59
  • define \chap like this \newcommand\chap[1]{\addcontentsline{toc}{chapter}{#1}}
    – touhami
    Jun 24, 2015 at 11:10
  • Thanks for an immediate reply but still the headings in the body of pdf didn't disappear?In the begin{document} instead of \chapter do i need to write something else? please advise
    – user80596
    Jun 24, 2015 at 11:18

2 Answers 2

3
\documentclass{report}
\newcommand\fakechapter[1]{%
  \clearpage
  \refstepcounter{chapter}%
  \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}%
  \chaptermark{#1}}

\begin{document}
\tableofcontents

\fakechapter{Obstacle Avoidance}
blbla

\fakechapter{Face Recognition}
blblb

\end{document}
4
  • Even with the above example same issue occur's i.e few chapters are overshooting 1page or more. :(
    – user80596
    Jun 24, 2015 at 13:55
  • The example doesn't use hyperref, so it can't have faulty links or bookmarks. Jun 24, 2015 at 13:57
  • I need hyperref package as I am using it for other purposes.Without deleting hyperref package how can i resolve this issue? please advise.
    – user80596
    Jun 24, 2015 at 14:10
  • I have no idea what issue you have as you didn't provide a minimal example that shows your issue. Also my code works fine with hyperref, I even don't need to add \phantomsection. Jun 24, 2015 at 14:16
3

This should need some adjustments, but it basically implements your request. Since I just redefine the sectioning commands, commenting out the \redefinelevel lines will revert to the standard behavior with no other change (apart running LaTeX a couple of times).

\documentclass{report}
\usepackage{fancyhdr}

\usepackage{lipsum}

\pagestyle{fancy}

\fancyhf{}
\fancyhead[L]{\nouppercase{\rightmark} (\nouppercase{\leftmark})}
\fancyfoot[C]{\thepage}

\makeatletter
\newcommand{\redefinelevel}[2][]{%
  \@namedef{#2}{\par % ... in case "\section" and siblings are called while 
                     % still in horizontal mode.
    \@ifstar{\@nameuse{redef@s@#2}}{\@nameuse{redef@#2}}}%
  \@namedef{redef@#2}##1{%
    #1% (In any case, unneeded spaces in macro definition waste memory...)
    \refstepcounter{#2}%
    \@nameuse{#2mark}{##1}%
    \addcontentsline{toc}{#2}{\protect\numberline{\@nameuse{the#2}}##1}%
  }%
  \@namedef{redef@s@#2}##1{#1\@mkboth{##1}{##1}}% this one was missing too
}
\makeatother

\redefinelevel[\cleardoublepage]{chapter}
\redefinelevel{section}
\redefinelevel{subsection}
\redefinelevel{subsubsection}

\begin{document}

\tableofcontents

\part{Humanoid Introduction} 
\chapter{Obstacle Avoidance}
\section{detecting wall}

\lipsum[1-10]

\chapter{Face Recognition}

\section{Shape Recognition}

\lipsum[1-10]

\subsection{Face Recognisation}

\lipsum[1-10]

\subsection{Hand Recognisation}

\lipsum[1-10]

\chapter{Testing the correction}
Some text.
\section{Called in horizontal mode}
This should be a new paragraph.  (Note that, without \verb|\par|, and 
without the appropriate~\verb|%|'s, two more spurious spaces were added.)

\end{document}

enter image description here

8
  • Brilliant.It works Thank you so much.How did you learn this?Can you please refer me to some link that I too can learn.Thanks
    – user80596
    Jun 24, 2015 at 11:43
  • @user80596 About 30 year experience with TeX. ;-)
    – egreg
    Jun 24, 2015 at 11:48
  • wow thats great1 A small issue @egreg. When I click on few chapters in browser pane it is going to the first section instead of going to chapter? This is bug I found just now with the above procedure. Please could you help?
    – user80596
    Jun 24, 2015 at 12:08
  • @user80596: Add \phantomsection to the code (before \addcontentsline). Jun 24, 2015 at 12:13
  • No @UlrikeFischer Same issue. Firrst 3 chapters point correctly to chapters but after that few chapters point to the next page :(
    – user80596
    Jun 24, 2015 at 12:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .