8

Before to begin, I know about:

\chapter{My Chapter Title\hfill\includegraphics{image}}

But it's not what I'm searching.

I want the following result:

Perfectly center

Next are all the methods I tried (and their respective results):

\chapter{Devise}\tabto{13cm}\includegraphics[height=2cm]{DEVISE_logo.png}

Too low

\chapter[Devise]{Devise\hfill\parbox[c]{.5\textwidth}{\hfill\includegraphics[height=2cm]{DEVISE_logo.png}}}

Too low

\begin{minipage}[c]{.5\linewidth}
    \chapter{Devise}
\end{minipage}
\begin{minipage}[c]{.5\linewidth}
    \hfill\includegraphics[height=2cm]{DEVISE_logo.png}
\end{minipage}

Too high and offset

\parbox[c]{.5\linewidth}{\chapter{Devise}}\parbox[c]{.5\linewidth}{\hfill\includegraphics[height=2cm]{DEVISE_logo.png}}

To low and offset


PS: I post all the pictures with the same width so you can notice on the two last the chapter title is offset.


Just to add to the solution found by John Kormylo:

Since he found the height of the chapter block (number + space + title) is 2.3cm, to place an image on the center line of this block, just do:

\savebox{\tempbox}{\raisebox{-((X-2.3)/2)cm}[0pt][0pt]{\includegraphics[height=Xcm]{example-image}}}

(Replace X by the height you want for your image)

  • Try the titlesec package. – skpblack Jul 23 '14 at 14:59
  • Ok, thanks, I watch. But if there is a way to do it and also keep the standard LaTeX chapter title, I would prefer. – Matthieu.P Jul 23 '14 at 15:04
  • Is this just for one chapter or shall this be applied to all chapters? – Gonzalo Medina Jul 23 '14 at 15:47
  • A lot depends on the font size used and the size of the gap. Shall we assume standard book definitions, or just play with numbers until it fits? – John Kormylo Jul 23 '14 at 16:09
3

The 22pt was found by trial and error. It should represent the height of the font used for the title plus 1/2 the gap. For some reason \chapter didn't like \raisebox, hence the \savebox.

\documentclass{book}
\usepackage{graphicx}
\usepackage{mwe}

\newsavebox{\tempbox}

\begin{document}
\savebox{\tempbox}{\raisebox{22pt}[\height]{\includegraphics[width=3cm]{example-image}}}
\chapter[My Chapter Title]{My Chapter Title\hfill\usebox{\tempbox}}% corrected

This space left blank.
\end{document}

chapter title

Here is an alternate form to overlap the text. Again, the 2.3cm was found by trial and error.

\savebox{\tempbox}{\raisebox{0pt}[0pt][0pt]{\includegraphics[height=2.3cm]{example-image}}}

overlap

  • You should also use the optional argument, or the TOC and headers will have small problems. – egreg Jul 23 '14 at 16:28
  • Uh, so what should I use for the optional argument? The manual isn't very clear. – John Kormylo Jul 23 '14 at 16:42
  • @JohnKormylo what egreg means is to use the optional argument for \chapter, as in `\chapter[My chapter title]{My Chapter Title\hfill\usebox{\tempbox}}. – Gonzalo Medina Jul 23 '14 at 16:49
  • Huh. I got the impression that the optional argument was supposed to be a counter. – John Kormylo Jul 23 '14 at 16:51
  • Thanks a lot ! I use your answer: by configuring the first parameter of raisebox at -Xcm it do what I want. Now, is there somewhere I can see the height of the block "Chapter N Title" so I can make a calculus to center the image in function of the height ? – Matthieu.P Jul 24 '14 at 7:32
3

Here's a mildly more complicated solution: the picture, with possible options, is given as a trailing optional argument to \chapter.

It would be possible to use the standard \chapter syntax and define a \chapterfigure command that needs to be placed just before \chapter.

\documentclass[a4paper]{book}

\usepackage{xparse,graphicx,xpatch}
\let\latexchapter\chapter

\ExplSyntaxOn

\RenewDocumentCommand{\chapter}{somo}
 {
  \bool_gset_false:N \g_matthieu_chapter_figure_bool
  \IfBooleanTF{#1}
   {\latexchapter*{#3}}
   {
    \IfNoValueTF{#4}
     {
      \IfNoValueTF{#2}
       {\latexchapter{#3}}
       {\latexchapter[#2]{#3}}
     }
     {
      \bool_gset_true:N \g_matthieu_chapter_figure_bool
      \placefigure{#4}
      \IfNoValueTF{#2}
       {\latexchapter{#3}}
       {\latexchapter[#2]{#3}}
     }
   }
 }

\NewDocumentCommand{\placefigure}{m}
 {
  \keys_set:nn { matthieu/placefigure } { #1 }
  \matthieu_placefigure:V \l_matthieu_placefigure_options_tl
 }

\keys_define:nn { matthieu/placefigure }
 {
  options .tl_set:N  = \l_matthieu_placefigure_options_tl,
  file    .tl_set:N  = \l_matthieu_placefigure_file_tl,
  file    .initial:n = {},
 }

\box_new:N \l_matthieu_figure_box
\dim_new:N \l_matthieu_halfheight_dim
\bool_new:N \g_matthieu_chapter_figure_bool

\cs_new_protected:Npn \matthieu_placefigure:n #1
 {
  \hbox_set:Nn \l_matthieu_figure_box
   {
    \includegraphics[#1]{ \l_matthieu_placefigure_file_tl }
   }
  \hbox_set_to_wd:Nnn \l_matthieu_figure_box { \textwidth }
   {
    \hfil
    \box_move_down:nn { \box_ht:N \l_matthieu_figure_box / 2 }
     { \box_use:N \l_matthieu_figure_box }
   }
  \box_set_ht:Nn \l_matthieu_figure_box { 0pt }
  \box_set_dp:Nn \l_matthieu_figure_box { 0pt }
 }

\cs_generate_variant:Nn \matthieu_placefigure:n { V }

\makeatletter
\xpatchcmd{\@makechapterhead}
 { \vskip 20\p@ }
 {
  \vskip 10\p@ \nointerlineskip
  \bool_if:NT \g_matthieu_chapter_figure_bool { \box_use:N \l_matthieu_figure_box }
  \nointerlineskip
  \vskip 10\p@
 }
 {}{}
\makeatother

\ExplSyntaxOff

\begin{document}

\let\cleardoublepage\relax % just to show a few chapters

\chapter{My chapter title}[
  file=example-image,
  options={width=3cm,height=2cm}
]


\chapter{My chapter title}[
  file=example-image,
  options={width=3cm,height=30pt}
]

\chapter{My chapter title}[
  file=example-image,
  options={width=3cm,height=10pt}
]

\end{document}

enter image description here

  • Thanks for your answer. It's hard to understand for a beginner like me but it's good to have some complicate example so I can use them latter to see how to configure deeply LaTeX commands. --> Thanks :) – Matthieu.P Jul 24 '14 at 11:41
  • @Matthieu.P The first part is just to add an optional argument to \chapter. The trick for centering is in the patch to \@makechapterhead, where the \vskip20pt is split into two and in the middle I set a zero height box that contains the image (flush right and centered with respect to its baseline). – egreg Jul 24 '14 at 11:50
  • Oh ok, now I understand ! (good idea) Thanks :) – Matthieu.P Jul 25 '14 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.