8

Is there a way to completely hide the \part and \chapter headings when they are called (but still show them in the toc)? Maybe using titlesec?

Note:

I'm using something like this to capture the chapter headings and reuse them:

\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

% Define \chaphead to be used in section headings
\let\oldchapter\chapter
\newcommand\temphead{}
\newcommand\chaphead{}
\renewcommand\chapter[2][\temphead]{%
    \renewcommand\temphead{#2}%
    \renewcommand\chaphead{#2}%
    \oldchapter[#1]{#2}}

\newcommand*\Hide{%

\titleformat{\chapter}[display]
  {}{}{0pt}{}
%\titleclass{\chapter}{straight}

\titleformat{\section}[display]
  {\large} % format
  {\chaphead, section \thesection} % label
  {10pt} %sep
  {} %before
}

\begin{document}

{
\Hide
\chapter{A first chapter}
\section{A first section}
\lipsum
}

\chapter{A second chapter}
\section{A second section}
\lipsum

\end{document}

When activating \titleclass{\chapter}{straight}, it seems \chaphead is not capture anymore.

raphink
  • 31,894

2 Answers2

5

Indeed, you can use the titlesec package; its explicit option allows you to remove the titles:

\documentclass{book}
\usepackage[explicit]{titlesec}

\newcommand*\Hide{%
\titleformat{\chapter}[display]
  {}{}{0pt}{\Huge}
\titleformat{\part}
  {}{}{0pt}{}
}

\begin{document}
\tableofcontents

{
\Hide
\part{A test part}
\chapter{A test chapter}
\section{Test section}
}

\part{A test part}
\chapter{A test chapter}
\section{Test section}

\end{document}
Gonzalo Medina
  • 505,128
  • Alright, that removes the chapter headings... but it also removes the section headings, and the chapter headings outside of the environment in which I did the call to \titleformat... – raphink Apr 18 '11 at 14:06
  • You can keep the changes local by using the grouping mechanism; I've edited my example to reflect this approach (and defining a command to simplify the work). – Gonzalo Medina Apr 18 '11 at 14:11
  • Hmmm... I'm using an environment already, that should limit the scope, shouldn't it? – raphink Apr 18 '11 at 14:15
  • Without your actual relevant code it's really hard to tell. Why don't you add to your question all the necessary elements (in the sense of a minimal working example) affecting your request? – Gonzalo Medina Apr 18 '11 at 14:22
  • Alright, I found my mistake (forgot to use #1 to indicate the headings when using explicit). Now, it won't show the headings, but it will still make empty pages. Can I get rid of these? – raphink Apr 18 '11 at 14:26
  • Sure, instead of using the \part command, you can add the corresponding ToC entry manually with the help of the \addcontentsline command. – Gonzalo Medina Apr 18 '11 at 14:30
  • ... or change the "class" of \part to straight: \titleclass{\part}{straight}. – Gonzalo Medina Apr 18 '11 at 14:34
  • I meant for the \chapter part. I can't just add the contents, because I'm using the counter and chapter name in other macros, so I need the chapter to be properly registered, but still not show in the text. – raphink Apr 18 '11 at 14:34
  • Using \titleclass{\chapter}{straight} removes the blank pages, but it doesn't register the chapter name (it does register the chapter number though). – raphink Apr 18 '11 at 14:36
  • I added a note about a bit of code I have already which might play a role in this issue... – raphink Apr 18 '11 at 14:40
  • 1
    Again, without knowledge of your actual relevant code, this process of guessing is totally useless and time wasting... as I already suggested, add a minimal working example of your relevant code. – Gonzalo Medina Apr 18 '11 at 14:43
  • I added an example. – raphink Apr 18 '11 at 14:57
1

I extended and changed the example for class srcartcl to the following solution (You find the one for class srcbook below):

\documentclass{scrartcl}
\usepackage{blindtext}
\usepackage[explicit]{titlesec}

% change part format
\titleformat{\part}[drop]{}{}{0pt}{}
\titlespacing*{\part}{0pt}{0pt}{0pt}

\begin{document}
\tableofcontents

\part{One}
\blindtext
\section{Apple}
\blindtext
\section{Peach}
\blindtext

\part{Two}
\blindtext
\section{Red}
\blindtext
\section{Green}
\blindtext

\end{document}

Btw.: It works nicely together with the counter reset from Adendum 2 in this first answer.

\makeatletter
\@addtoreset{section}{part}
\makeatother

Here the code with scrbook:

\documentclass{scrbook}
\usepackage{blindtext}
\usepackage[explicit]{titlesec}

% change part format
\titleformat{\part}[drop]{}{}{0pt}{}
\titlespacing*{\part}{0pt}{0pt}{0pt}
\titleformat{\chapter}[drop]{}{}{0pt}{}
\titlespacing*{\chapter}{0pt}{0pt}{0pt}

\begin{document}
\tableofcontents

\part{One}
\blindtext
\chapter{Fruits}
\blindtext
\section{Apple}
\blindtext
\section{Peach}
\blindtext

\part{Two}
\blindtext
\chapter{Colours}
\blindtext
\section{Red}
\blindtext
\section{Green}
\blindtext

\end{document}
Dirk
  • 2,728