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)

4
  • 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
    Commented Aug 22, 2012 at 15:33
  • Just section-level will be fine.
    – BigDawg
    Commented Aug 22, 2012 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
    Commented Aug 22, 2012 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
    Commented Aug 22, 2012 at 16:45

2 Answers 2

47

Something like this:

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

\invisiblesection{Blah}
11
  • Perfect ! I was missing the \sectionmark in what I tried before coming here. Thanks for the help.
    – BigDawg
    Commented Aug 22, 2012 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? Commented Dec 11, 2013 at 6:55
  • 1
    Just use \addcontentsline{toc}{section}{#1}
    – Boris
    Commented Jan 15, 2014 at 19:57
  • 1
    Then use \markboth{#1}{#1}
    – Boris
    Commented Jan 16, 2014 at 19:46
  • 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
    Commented Dec 19, 2019 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):

2
  • Welcome to tex.sx. Commented Dec 18, 2022 at 23:47
  • +1: It would be nice if you could offer a complete code example and also a screenshot of the output. Commented Dec 18, 2022 at 23:54

You must log in to answer this question.

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