1

I need to include a bunch of PDFs as files after and before other regular chapters. I want each PDF to be a numbered chapter, be included in the toc, and to appear in the header-line (of fancyhdr), but not the actual 'Chapter X. A fancy chapter' to be printed, since that would print the 'Chapter X', then a blank page, then the pdfs.

All of this works fine with a solution I found on another question ( Start a new chapter without heading )

\documentclass{scrbook} %report}
\usepackage{hyperref}

\makeatletter
\newcommand{\unchapter}[1]{%
    \begingroup
    \let\@makechapterhead\@gobble % make \@makechapterhead do nothing
    \chapter{#1}
    \endgroup
}
\makeatother

\pagestyle{headings}

\begin{document}
    \tableofcontents
    
    \chapter{Regular}\label{sec:regular}
    \section{Something}\label{sec:something}
    Text
    
    \unchapter{No chapter head}\label{sec:nohead}
    
    Text\newpage
    text: check the heading
    
    \chapter{This is regular again}\label{sec:head}
    
    References: \\
    Regular: \ref{sec:regular}\\
    Something: \ref{sec:something}\\
    No chapter head:  \ref{sec:nohead}\\
    Regular again: \ref{sec:head}   
\end{document}

However, I face a problem when I want to refer to the chapter, since that then refers to the last (sub-)section. I.e. the example references of the last chapter look like this:

References:
Regular: 1
Something: 1.1
No chapter head: 1.1
Regular again: 3

The links also lead to section 1.1. So I assume makechapterhead is somehow necessary for the references to work, but I couldn't find what it does.

I did find \refstepcounter{chapter}, but that only increments Regular again: 3 to 4, so it seems the counter is correctly incremented by the \unchapter, but the reference just doesn't know there was a change?

I also tried sprinkling some clearpage, new page, chaptermark, all over it to no avail.

1
  • I suspect hyperref patches something in chapter that got deleted. Try using \phantomsection (page 40). Nov 24, 2022 at 19:35

1 Answer 1

1

This code gives the right result. (without the \begingroup and \endgroup)

\documentclass{scrbook} %report}
\usepackage{hyperref}

\makeatletter
\newcommand{\unchapter}[1]{% changed <<<<<<<<<<<<
%\begingroup
    \let\old@makechapterhead\@makechapterhead
    \let\@makechapterhead\@gobble % make \@makechapterhead do nothing
    \chapter{#1}
    \let\@makechapterhead\old@makechapterhead% restore @makechapterhead
%\endgroup
}
\makeatother

\pagestyle{headings}

\begin{document}
    \tableofcontents
    
    \chapter{Regular}\label{sec:regular}
    \section{Something}\label{sec:something}
    Text
    
    \unchapter{No chapter head}\label{sec:nohead}
    
    Text\newpage
    text: check the heading
    
    \chapter{This is regular again}\label{sec:head}
    
    References: 
    
    Regular: \ref{sec:regular}
    
    Something: \ref{sec:something}
    
    No chapter head:  \ref{sec:nohead}
    
    Regular again: \ref{sec:head}   
\end{document}

b

2
  • I checked, after some recompilations it also works beautifully with multiple \unchapter-s. Links point where they should, too. Thanks! Unfortunately I don't have enough points to make a visible vote, but it said its registered anyway.
    – Maahk
    Nov 24, 2022 at 12:59
  • @Maahk I updated the answer. Please use this version. Nov 24, 2022 at 15:50

You must log in to answer this question.

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