12

I wish insert an image at the beginning of a chapter that, like in the figure attached, is seen in the white space at right of the title. Moreover, it must be different for every chapter of the book.

I tried the following MWE:

\documentclass{book}
\usepackage{titlesec}
\titleformat{\chapter}[block]
 {%\rule{1in}{1in}
  \normalfont\huge\bfseries}
  {\chaptertitlename\ \thechapter\\}
  {0pt}
  {\Huge}
\titlespacing*{\chapter}{0pt}{0pt}{40pt}

\begin{document}
 \chapter{Example}
\end{document}

But it creates (or add) an image before the word "Chapter".

Example

3 Answers 3

10

Here's one possibility that works for both numbered and unnumbered chapters, using \parboxes to position the image and the "Chapter #" string for numbered chapters; for unnumbered chapters, just the image is included. Modify the settings according to your needs:

\documentclass[openany]{book}
\usepackage{graphicx}
\usepackage[explicit]{titlesec}

\makeatletter
\let\@chapterimage\relax
\newcommand\chapterimage[1]{%
  \if\relax\detokenize{#1}\relax
  \else
    \gdef\@chapterimage{\smash{\includegraphics[width=4cm,height=5cm,keepaspectratio]{#1}}}
  \fi
}

\titleformat{\chapter}[display]
  {\normalfont\huge\bfseries}
  {}
  {0pt}
  {\parbox[t]{.4\textwidth}{\chaptertitlename\ \thechapter}\hfill
    \parbox[t]{.4\textwidth}{\@chapterimage}\\[20pt]%
    \Huge#1
  }
\titleformat{name=\chapter,numberless}[display]
  {\normalfont\huge\bfseries}
  {}
  {0pt}
  {\parbox[t]{.4\textwidth}{\mbox{}}\hfill
    \parbox[t]{.4\textwidth}{\@chapterimage}\\[20pt]%
    \Huge#1
  }
\titlespacing*{\chapter}{0pt}{0pt}{40pt}

\newcommand\nochapterimage{
  \let\@chapterimage\relax
}
\makeatother

\begin{document}

\chapterimage{dog1}
\chapter*{Test unnumbered chapter}

\nochapterimage
\chapter*{Another test unnumbered chapter}

\chapter{Test numbered chapter}

\chapterimage{dog2}
\chapter{Another test numbered chapter}

\end{document}

enter image description here

Use \chapterimage to declare the image you want to use for a specific chapter. Use \nochapterimage for chapters which shouldn't receive an image (the code shows both commands in action).

I used the class option openany just for the example.

1
  • How do I center the image? I have a slightly larger image and want its width equal to the textwidth. I did set width=\textwidth in your code, but the image is not centered and runs into the margins (I have slightly larger margins). What can be done so that image is in within the textwidth?
    – Damitr
    Apr 7, 2023 at 7:29
5

This is straight-forward with an application of eso-pic when you utilize the starred version of the placement macros:

enter image description here

\documentclass[openany]{book}
\usepackage{graphicx,eso-pic}

\newcommand\chapterimage[2][]{%
  \AddToShipoutPictureBG*{% Add picture to BackGround on this page only
    \AtTextUpperLeft{% Position at upper left of text block
      \hspace*{\textwidth}% Move over to upper right of text block
      \llap{% Ignore horizontal width and overlap to the left
        \smash{% Ignore vertical height
          \raisebox{-\height}{% Lower so top touches baseline
            \includegraphics[#1]{#2}}}}}}}% Include image with options

\begin{document}

\chapter*{Test unnumbered chapter}
\chapterimage[width=4cm]{example-image-a}

\chapter*{Another test unnumbered chapter}

\chapter{Test numbered chapter}

\chapter{Another test numbered chapter}
\chapterimage[height=50pt,angle=45]{example-image-b}

\end{document}

\AddToShipoutPictureBG* adds content in the background of (or under the) page content, while \AddToShipoutPictureFG* places it in the foreground (over top) of the page content.

0

One more option is to insert image using tikz. One advantage of this approach is that you can exactly control the position and size of the image with respect to the chapter heading or page contents. The drawback is that you might have to workout manually with trial and error every image if the image size differs significantly. I had images which had width equal to the textwidth hence I tried this approach and the relative coordinates were the same for me for all the chapter images.

These two packages are used:

\usepackage{tikz}
\usetikzlibrary{calc}

You can place the image with relative coordinates of the page. This code had to be added after \chapter command, otherwise it will not appear on the same page.

 \begin{tikzpicture}[remember picture,overlay,shift=(current page.north west)]
\begin{scope}[x={(current page.north east)},y={(current page.south west)}]
\node [overlay,remember picture] at (0.35,0.16)  % these numbers will have to be worked for your page /chapter headings with trial and error     
{\includegraphics[width=\textwidth]{figures.jpg}};
\end{scope}
\end{tikzpicture}

You must log in to answer this question.

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