48

Simple question here: how to make section headings invisible, but still get the correct list of sections in the TOC and headers ? e.g. on page 10, if I do a \section{New section}, I must not see the text "X. New section", but I still want the section to be in the TOC and in the \rightmark of the following pages, until a new section of course.

I'm using a minimalistic document (class article, with no package that are used to adjust section headings style)

Mico
  • 506,678
BigDawg
  • 1,387
  • Welcome to TeX.SE! Which document class do you use: book, report, ...? Also, do you already use a package (such as titlesec and sectsty) that may be used affect the appearance of sectioning headers. Finally, do you want to suppress the appearance of all sectioning headers (including subsections, subsubsections, etc) or "just" section-level headers? – Mico Aug 22 '12 at 15:33
  • Just section-level will be fine. – BigDawg Aug 22 '12 at 15:40
  • Will you use some sort of separator, other than the actual section name, to mark where one section ends and the next one begins? – Ricardo Aug 22 '12 at 16:19
  • No. The thing is that new sections will necessary be at the beginning of new pages, I just want to remove the "X. Section name", as it will already be written in the header (via fancyhdr) – BigDawg Aug 22 '12 at 16:45

2 Answers2

47

Something like this:

\newcommand\invisiblesection[1]{%
  \refstepcounter{section}%
  \addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}%
  \sectionmark{#1}}
...

\invisiblesection{Blah}
Boris
  • 38,129
  • Perfect ! I was missing the \sectionmark in what I tried before coming here. Thanks for the help. – BigDawg Aug 22 '12 at 17:18
  • 2
    Don't you have an issue that several \invisiblesection{} in row are linked to the same (wrong) pages in the TOC or don't appear there at all? – Anton Tarasenko Dec 11 '13 at 06:55
  • Can you post an MWE? – Boris Dec 11 '13 at 16:28
  • any chance of getting it to work like \sectio*{} so that the counter number does not appear in the toc – user43528 Jan 15 '14 at 14:46
  • Just do not \refstepcounter, and do not add \numberline to the TOC – Boris Jan 15 '14 at 16:53
  • yes that works nearly perfectly, but then it carries over the section number from the previous section (if you know what I mean). I have 4 regular sections and the i insert several invisible sections, so then my toc shows 4"name of invisible section". any chance to fix that? – user43528 Jan 15 '14 at 19:00
  • 1
    Just use \addcontentsline{toc}{section}{#1} – Boris Jan 15 '14 at 19:57
  • thanks, that works too, but i need the section to exist as I want to print the section name in the footer (i'm using \leftmark for that) – user43528 Jan 16 '14 at 09:16
  • 1
    Then use \markboth{#1}{#1} – Boris Jan 16 '14 at 19:46
  • Don't mean to bump old content, but if you use tools that rely on \section being the literal command used in source, you can still get this result by using \def: \def\invisible\section#1{...}. To get support for other sectioning commands, though, you'll have to generalize (i.e., you won't be able to define an additional \def\invisible\subsection#1{...}, I don't think). – Sean Allred Apr 29 '18 at 14:15
  • 3
    I got a separate subsection command to work simply by replacing section with subsection, but I am open to more elegant solutions. Here's the command: \newcommand\invisiblesubsection[1]{\refstepcounter{subsection} \addcontentsline{toc}{subsection}{\protect\numberline{\thesubsection}#1} \sectionmark{#1}} – Nagel Dec 19 '19 at 17:59
2

Boris' solution works well, but a \nameref to the section will not work correctly. Here's another that's built up on their solution.

Before \begin{document}

\makeatletter
\def\invisiblesection#1{%
\refstepcounter{section}%
\addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}%
\sectionmark{#1}}
\protected@edef\@currentlabelname{#1} % Set correct name
...}
\makeatother

\invisiblesection{Blah} \label{blah} ... \nameref{blah}

Minimum working example (MWE):

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=3,
 breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
 {hyperref}
\usepackage[demo]{graphicx}

\newcommand\invisiblesectionwithoutname[1]{% \refstepcounter{section}% \addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}% \sectionmark{#1}\phantom{} }

\makeatletter \def\invisiblesection#1{% \refstepcounter{section}% \addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}% \sectionmark{#1}\phantom{} \protected@edef@currentlabelname{#1} % Set correct name } \makeatother

\begin{document} \tableofcontents{}\clearpage{}

\invisiblesectionwithoutname{One} \label{one} \begin{figure} \caption{\protect\includegraphics{logo}} \end{figure}

\clearpage{} \invisiblesection{Two} \label{two} \begin{figure} \caption{\protect\includegraphics{logo}} \end{figure}

\ Without setting correct label: \nameref{one}

With setting correct label: \nameref{two} \end{document}

Output (\nameref):

Toon Tran
  • 121