[Tex/LaTex] how to import images into Memoir chapter style

chaptersgraphicsmemoir

Another Memoir style I'm trying to create… I grabbed bits of code from here and there and I almost have it working, but the imported image isn't centered over the chapter title… spent many hours on this before coming to you…I'm trying! I would appreciate if someone could fix it and suggest any clean up to any blunders I've made.. Also, I would like to suppress all chapter numbers in the document (but keep them in the toc) is there a good way to do that, or do I just need to use the asterisk.

in brief… what I'm trying to do is get an image over centered text with some numbers in there that I can tweak to adjust the v spacing between the image and title and between the title and the first paragraph.

\makechapterstyle{test1}{%
  \chapterstyle{default}
  \chapterstyle{article}%
  \renewcommand*{\printchapternonum}{%
%    \vphantom{\printchaptername \chapnumfont 1}
    \afterchapternum
   }
 \renewcommand*{\afterchapternum}{%
   \vspace*{\beforechapskip}
   \smash{%
      \raisebox{0cm}{%
       \includegraphics[width=2in]{chantheader.jpg}}}
         \aliaspagestyle{chapter}{headings}
 }%
}

Best Answer

You can try something like this (the code contains some explanatory comments):

\documentclass{memoir}
\usepackage{graphicx}
\usepackage{lipsum}

\makechapterstyle{test1}{%
\renewcommand{\printchaptername}{}% suppress "Chapter" from heading
\renewcommand*{\printchapternum}{}% suppress numbering from heading
\renewcommand*{\chaptitlefont}{\normalfont\bfseries\LARGE\centering}% title formatting
\renewcommand\afterchapternum{%
  \centering\includegraphics[width=2in]{ctanlion}\vskip1em}% add the image and some additional vertical spacing (1em) between the image and the title 
\setlength\beforechapskip{-10pt}% adjust vertical space before the image
\setlength\afterchapskip{30pt}% adjust vertical space after the title
}
\chapterstyle{test1}

\begin{document}

\tableofcontents*
\chapter{Test Chapter}
\lipsum[2]

\end{document}

The ToC:

enter image description here

The page containing the chapter heading:

enter image description here

CTAN lion drawing by Duane Bibby.

In a comment, a redefinition of \chapter has been requested, so as to have an additional mandatory argument to specify the image for each chapter. The code below shows this redefinition with the help of the xparse package (I used xparse to easily handle the two optional arguments for \chapter in memoir); the new syntax is

\chapter[ToC entry][Header text]{image file}{Heading in the document body}

\documentclass[openany]{memoir}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{lipsum}

\newcommand\chapterimage{}
\makechapterstyle{test1}{%
\renewcommand{\printchaptername}{}% suppress "Chapter" from heading
\renewcommand*{\printchapternum}{}% suppress numbering from heading
\renewcommand*{\chaptitlefont}{\normalfont\bfseries\LARGE\centering}% title formatting
\renewcommand\afterchapternum{%
  \centering\chapterimage\vskip1em}% add the image and some additional vertical spacing (1em) between the image and the title 
\setlength\beforechapskip{-10pt}% adjust vertical space before the image
\setlength\afterchapskip{30pt}% adjust vertical space after the title
}
\chapterstyle{test1}

% Redefinition of \chapter; now it has an additional mandatory argument
% for the name of the image file to be used for each chapter
\let\oldchapter\chapter
\RenewDocumentCommand\chapter{O{}O{}mm}{%
\if\detokenize{#3}\relax\relax
  \gdef\chapterimage{}
\else
  \gdef\chapterimage{\includegraphics[width=2in]{#3}}
\fi%
\oldchapter[#1][#2]{#4}}

\begin{document}

\tableofcontents*
\chapter{example-image-a}{Test chapter one}
\lipsum[4]
\chapter{example-image-b}{Test chapter two}
\lipsum[4]

\end{document}

enter image description here

openany was used only for the example.

Related Question