# Insert an image at the beginning of a chapter

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".

-

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}


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.

-

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

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

\newcommand\chapterimage[2][]{%
\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.

-