9

In a book, with the book documentclass, I'm typesetting I want to include a figure above the \chapter title. This however has turned out to be hard. With the following code:

\chapter{Uppväxt}

\begin{figure}[ht]
\includegraphics[width=\textwidth]{bilder/hus.jpg}
\end{figure}

\noindent
\lipsum

Produces the following result: A figure underneath a chapter.

Placing \figure above \chapter results in the image being put on the previous page due to the \clearduble command of \chapter, which is desirable for the rest of the book. Changing the options of \figure to [t] results in the figure being included on the next page. It seems like no other combination of positioning options seems to help either.

Can anyone think up a way of placing the figure above the chapter?

Rovanion
  • 307
  • the answer to this depends on what \documentclass you are using, because they define \chapter in different ways. some classes even go to extra trouble to prevent floats from coming at the top of the first page of a chapter. my inclination is that this picture should be treated not as a float, but inserted via some explicit mechanism within the (altered) \chapter framework. but the particular mechanism that might be successful does depend strongly on the \documentclass. – barbara beeton Nov 12 '14 at 13:20
  • You can use eso-pic or background or textpos packages and overlay the image. –  Nov 12 '14 at 13:29
  • @barbara beeton: I had Book as a tag on the post, but it was removed by lockstep - hence the ambiguity about the documentclass. Added back in the text instead. – Rovanion Nov 12 '14 at 13:31

1 Answers1

13

The image appears to be part of the chapter head design so the most natural place to add it is in \@makechapterhead (or equivalent command in other classes.

enter image description here

This adds an image with name \chappic if that command is non empty.

\documentclass{book}
\usepackage{graphicx}
\makeatletter

\def\@makechapterhead#1{%
  \ifx\chappic\@empty
  \vspace*{50\p@}%
\else
  \vspace*{5\p@}% was 5
    \centerline{\includegraphics{\chappic}}%<<<<
\fi
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \huge\bfseries \@chapapp\space \thechapter
        \par\nobreak
        \vskip 20\p@
      \fi
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}
\makeatother
\newcommand*\chappic{}

\begin{document}

\renewcommand*\chappic{house}
\chapter{house}

text ... text ... text ... text ... text ... text ... text ... text ... 
text ... text ... text ... text ... text ... text ... text ... text ... 
text ... text ... text ... text ... text ... text ... text ... text ... 

\renewcommand*\chappic{}
\chapter{no house}

text ... text ... text ... text ... text ... text ... text ... text ... 
text ... text ... text ... text ... text ... text ... text ... text ... 
text ... text ... text ... text ... text ... text ... text ... text ... 


\end{document}
Rovanion
  • 307
David Carlisle
  • 757,742
  • This will include the image before every chapter which is not the desired effect. The image should only appear in one place in the book. – Rovanion Nov 12 '14 at 13:35
  • @Rovanion well to be exact it will include a figure with the same name as each chapter if it exists. It can be wrapped in a conditional so it only sometimes does that, I'll add a line... – David Carlisle Nov 12 '14 at 13:39
  • @Rovanion updated with chapter 1 with an image and chapter 2 without – David Carlisle Nov 12 '14 at 13:47
  • What does the command \interlinepenalty\@M do in the title? It messed up the rest of the text by inserting spaces between paragraphs. – Rovanion Nov 18 '14 at 20:20
  • @Rovanion it prevents page breaks between lines, oops the first one is not inside a group. Oh just remove it, sorry. – David Carlisle Nov 18 '14 at 20:26
  • It should not be this hard to put an image on a chapter page in LaTeX.... – jvriesem Apr 19 '21 at 02:37
  • @jvriesem I wouldn't say adding one line of code was hard, but if you want to avoid end users having to copy the definition then nothing is stopping you taking the above and maintaining a chappic.sty package on ctan, that is how latex evolves. – David Carlisle Apr 19 '21 at 07:20