19

How do I remove a section/subsection title, but keep the section/subsection name in the headings and in the table of contents?

For example, I type in my document somewhere,

\section{My Section}

and I get

  1. "(section number) MY SECTION" in the headings,

  2. "(section number) My Section.........(page number)" in the table of contents,

  3. "(section number) My Section" before the first paragraph.

How do I prevent the third item from occurring?

I have been searching for hours and it seems that this should have a simple one line solution, but I haven't found it. I have thought about using

\addcontentsline{toc}{subsection}{My Section}

but then I lose "(section number) MY SECTION" in the headings and I lose the (section number) in the table of contents. So an equivalent set of question,

How do I add numbered sections to the toc and to the headings?

Working example?

\documentclass[12pt]{book}
\usepackage{lipsum}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\section{Section 1}
\subsection{Subsection 1}
Here is lovely paragraph. And notice there is a ``subsection title'' right above me. How do I get rid of that title thingy???? 
\subsection{Subsection 2}
More stuff..... $$1+2+3+\cdots+\infty=-\frac{1}{12}$$
\lipsum[1-10]
\section{Section 2}
\lipsum[1-10]
\Section{Section 3}
\lipsum[1-10]
\chapter{Chapter 2}
\section{Section 1}
Again, above me a section name, how do I get rid of that guy??? \\
\lipsum[1-10]
\section{Section 2}
\lipsum[1-10]
\end{document}
7
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Aug 25, 2013 at 5:40
  • Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Aug 25, 2013 at 5:50
  • 1
    Add also \markboth{Name 1}{Name 2}
    – Eddy_Em
    Aug 25, 2013 at 6:04
  • Are you saying that you don't want neither the number nor the title to appear in the document, or only the number? Aug 25, 2013 at 6:16
  • I would like to suppress item number three. So, I don't want the number and I don't want the title. Aug 25, 2013 at 6:20

3 Answers 3

24

You can define a \fakesection that does all the things the regular \section does except print the actual heading:

\newcommand{\fakesection}[1]{%
  \par\refstepcounter{section}% Increase section counter
  \sectionmark{#1}% Add section mark (header)
  \addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}% Add section to ToC
  % Add more content here, if needed.
}

A similar macro for \fakesubsection would be

\newcommand{\fakesubsection}[1]{%
  \par\refstepcounter{subsection}% Increase subsection counter
  \subsectionmark{#1}% Add subsection mark (header)
  \addcontentsline{toc}{subsection}{\protect\numberline{\thesubsection}#1}% Add subsection to ToC
  % Add more content here, if needed.
}

The uses would be \fakesection{some section} and \fakesubsection{some subsection}.

5
  • I will try that. But one question? If I create a \label and then \ref that label, will it also still behave like a regular section? Aug 25, 2013 at 6:51
  • 1
    @BobbyOcean: Yes, since I've used \refstepcounter. That is, you'll receive the regular reference number.
    – Werner
    Aug 25, 2013 at 6:55
  • 2
    It should be \protect\numberline. Also the \par should be the first item in the definition. There's still the possibility that the page reference is wrong, because the subsequent paragraph might cause a page break; so probably \@afterheading is needed at the end.
    – egreg
    Aug 25, 2013 at 9:35
  • Hi Werner, why my fake section (included another supplemental tex with figures) in bookmark jump to the first page when the figure in the supplemental tex is long? When the figure is short, the fake section worked well.
    – Zuooo
    Nov 17, 2019 at 7:36
  • @Zuooo: You'd have to provide some code to replicate your behaviour. This minimal example works as expected.
    – Werner
    Nov 17, 2019 at 21:42
7

I can't see any application for this setting: without any visual clue in the text, a table of contents containing inexistent titles doesn't make sense.

However, one can do it with titlesec, ensuring that the references will be correct, both in the table of contents and with the \label-\ref mechanism.

\documentclass{book}

\usepackage{lipsum}

\usepackage{titlesec}

\makeatletter
\titleformat{\section}[runin]{}{}{0pt}{\@gobble}
\titleformat{\subsection}[runin]{}{}{0pt}{\@gobble}
\makeatother
\titlespacing{\section}{\parindent}{0pt}{0pt}
\titlespacing{\subsection}{\parindent}{0pt}{0pt}

\begin{document}
\tableofcontents

\chapter{Chapter 1}

\section{Section 1}

\subsection{Subsection 1}

Here is lovely paragraph. And notice there is a ``subsection title'' 
right above me. How do I get rid of that title thingy?

\subsection{Subsection 2}

More stuff:
\[
1+2+3+\cdots+\infty=-\frac{1}{12}
\]
\lipsum[1-10]

\section{Section 2}
\lipsum[1-10]

\end{document}

Table of contents page

enter image description here

First chapter page

enter image description here

1

everybody!

I was tring to do the same and found a simple solution if it's helpfull.

Write:

\lhead{Whatever you want}
\rhead{nothing if you want to avoid the number and title of sections}
\begin{document}

It worked for me.

Good luck!

You must log in to answer this question.

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