1

I am trying to find a Memoir chapter style that would allow me to import images into chapters, e.g. using the Thatcher chapter style - but with images centered above each chapter, and retaining the chosen chapter style.

There's an example at how to import images into Memoir chapter style, but on the first example, the selected chapter style got overridden, and I didn't manage to specify a different image for each chapter, on the second example.

The basic document preamble I'm using is:

\documentclass[12pt,msmallroyalvopaper]{memoir}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{lipsum}
\chapterstyle{thatcher}

First example:

\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}

Second example:

[Same preamble as above]

\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}
2
  • Do you mean that I should add "thatcher" in \makechapterstyle{test1}, as in \makechapterstyle{thatcher} If not, where do I find the thatcher style definitions to copy into a \makechapterstyle definition?
    – Claud
    Mar 30, 2016 at 4:31
  • @jon - I think this question might contain the answer that I need: Memoir chapterstyle - add image before chapter num
    – Claud
    Mar 30, 2016 at 4:48

1 Answer 1

1

You could do this. I assumed you wanted to change the image with each chapter.

\documentclass[12pt,msmallroyalvopaper, openany]{memoir}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{lipsum,mwe}

% usage: \setimage{<image file name>}
\providecommand\currentimage{}
\newcommand\setimage[1]{\def\currentimage{#1}}


\makeatletter

\makechapterstyle{thatcherimage}{%
  \chapterstyle{default}
  \renewcommand*{\chapterheadstart}{}% <-- this is the command to redefine, but we delay it so it doesn't print an image on the \tableofcontents page
  \renewcommand*{\printchaptername}{%
    \centerline{\chapnumfont{\@chapapp\ \thechapter}}}
  \renewcommand*{\chapternamenum}{}
  \renewcommand*{\chapnumfont}{\normalfont\scshape\MakeTextLowercase}
  \renewcommand*{\printchapternum}{}
  \renewcommand*{\afterchapternum}{%
    \par\centerline{\parbox{0.5in}{\hrulefill}}\par}
  \renewcommand*{\printchapternonum}{%
    \vphantom{\chapnumfont \@chapapp 1}\par
    \parbox{0.5in}{}\par}
  \renewcommand*{\chaptitlefont}{\normalfont\large}
  \renewcommand*{\printchaptertitle}[1]{%
    \centering \chaptitlefont\MakeTextUppercase{##1}}}

\makeatother

\chapterstyle{thatcherimage}

\begin{document}
\tableofcontents

% To avoid conflicting with \tableofcontents, we delay the image
% commands until here
\renewcommand*{\chapterheadstart}{%
  \par{\centering
  \edef\xxx{\noexpand\includegraphics[width=2cm]{\currentimage}}\xxx
  \par}\bigskip
}
% Note: the lines surrounding the \edef line are the 'formatting'


\setimage{example-image-a}

\chapter*{Starred Chapter}
\lipsum[1]


\setimage{example-image-b}

\chapter[1-OPT]{Chapter Using 1 Optional Argument}
\lipsum[1]


\setimage{example-image-c}

\chapter[2-OPT][2-OPTIONAL]{Chapter Using 2 Optional Arguments}
\lipsum[1]

\end{document}

At the risk of becoming a little clunky, here's a version that lets you optionally specify a \label and \caption for your chapter-images. (Though why such a \caption makes sense in this context is beyond me.)

The key command is \setimage, which works in the following way:

\setimage(<label>)[<image file>][<caption>]

Strictly speaking, all arguments are optional, but the second command is only 'optional' because it seems more convenient to not use three types of braces (viz, not \setimage(){}[]).

\RequirePackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{doody,
  author       = {Doody, Terrence},
  title        = {Hemingway's Style and {Jake's} Narration},
  year         = 1974,
  volume       = 4,
  number       = 3,
  pages        = {212-225},
  journal      = {The Journal of Narrative Technique},
}
\end{filecontents}

\documentclass[msmallroyalvopaper, openany]{memoir}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{lipsum,mwe, capt-of}


\newif\ifnoimage
\newif\ifnolabel
\newif\ifnocaption
\providecommand\currentimage{}
\providecommand\currentlabel{}
\providecommand\currentcaption{}
\newcommand\ImagesOff{\noimagetrue\nolabeltrue\nocaptiontrue}

\NewDocumentCommand{\setimage}{d()oo}
{% usage: \setimage(<label>)[<image file>][<caption>]
  \noimagefalse
  \IfNoValueTF{#1}{\nolabeltrue}{\nolabelfalse\def\currentlabel{#1}}%
  \def\currentimage{#2}%
  \IfNoValueTF{#3}{\nocaptiontrue}{\nocaptionfalse\def\currentcaption{#3}}%
}

\makeatletter

\makechapterstyle{thatcherimage}{%
  \chapterstyle{default}
%  \renewcommand*{\chapterheadstart}{}
  \renewcommand*{\chapterheadstart}{%
    \begin{center}
    \ifnoimage\else
      \edef\xxx{\noexpand\includegraphics[width=2cm]{\currentimage}}%
      \xxx
    \fi
      %
    \ifnocaption  \else
      \edef\yyy{\noexpand\captionof{figure}{\currentcaption}}%
    \yyy
    \fi
    \ifnolabel\else
      {\label{\currentlabel}}%
    \fi
  \end{center}
}
  \renewcommand*{\printchaptername}{%
    \centerline{\chapnumfont{\@chapapp\ \thechapter}}}
  \renewcommand*{\chapternamenum}{}
  \renewcommand*{\chapnumfont}{\normalfont\scshape\MakeTextLowercase}
  \renewcommand*{\printchapternum}{}
  \renewcommand*{\afterchapternum}{%
    \par\centerline{\parbox{0.5in}{\hrulefill}}\par}
  \renewcommand*{\printchapternonum}{%
    \vphantom{\chapnumfont \@chapapp 1}\par
    \parbox{0.5in}{}\par}
  \renewcommand*{\chaptitlefont}{\normalfont\large}
  \renewcommand*{\printchaptertitle}[1]{%
    \centering \chaptitlefont\MakeTextUppercase{##1}}}
\makeatother

\chapterstyle{thatcherimage}

\begin{document}

%\noimagetrue
\ImagesOff

\tableofcontents
% Should only include entries for:
% - example-image
% - example-image-b
\listoffigures



\setimage(fig:no)[example-image][No Letter]% all 3 args present

\chapter*{Image with 3 Arguments}
\lipsum[1]


\setimage(fig:a)[example-image-a]% no \caption; not in LOF

\chapter[1-OPT]{Image with label, no caption}
\lipsum[1]


\setimage()[example-image-b][The Letter B]% no \label; note: 'empty' \label

\chapter[2-OPT][2-OPTIONAL]{Image with cpation, no label }
\lipsum[1]


\setimage[example-image-c]% only the image; not in LOF

\chapter{Only Image}

{\ttfamily
\verb|\ref{fig:no}| (defined)   $\to$ \ref{fig:no}\par
\verb|\ref{fig:a}|  (defined)   $\to$ \ref{fig:a}\par
\verb|\ref{fig:b}|  (undefined) $\to$ \ref{fig:b}\par
\verb|\ref{fig:c}|  (undefined) $\to$ \ref{fig:c}\par
}


\noimagetrue % <-- probably better to use \ImagesOff, but this works too
\bibliographystyle{abbrvnat}
\nocite{*}
\bibliography{\jobname}

\end{document}
5
  • Note that it is much easier to add the image, if the image will be the same every time. Just add the image directly in the redefinition of \chapterheadstart in \makechapterstyle.
    – jon
    Mar 30, 2016 at 5:27
  • 1
    I really appreciate you taking time to help me fix this. I can wait. Re the figures I should have said \caption which is for \listoffigures. I have placed one figure after \frontmatter, \maketitle in the traditional way and the code includes a both \caption{kkkk} and \label{kkk}. I use \caption and the image appears below the book title, subtitle in the first page of the book and it shows up in the list of fifures. Would be nice to have this function, or both, for the images imported into the chapter headings as well, whichever is simpler - \caption is more important though.
    – Claud
    Mar 30, 2016 at 19:27
  • 1
    @Claud -- The updated answer provides \ImagesOff, which tells the chapterstyle to not expect an image. And the \setimage(<label>)[image file][<caption>] allows you to label or not, and caption or not as you see fit.
    – jon
    Mar 30, 2016 at 19:53
  • 1
    Quick question: I haven't tried this yet, but what is the purpose of that bibliography entry at the beginning \RequirePackage{filecontents} \begin{filecontents}{\jobname.bib}? Do I need to replace it with my own bib information and style? As I mentioned before, I was problems with placing my \bibliography{refs} at the end my book, because it would print an image above the bibliography title, so I moved it to the end of this list... \tableofcontents \listoffigures \listoftables \bibliography{refs} so it appears on page V of the book.
    – Claud
    Mar 30, 2016 at 21:22
  • 1
    You should use \ImagesOff. However, if it is followed by a \setimage you have effectively turned 'images back on'. You go \ImagesOff \chapter{Chap with No Image} and then when you want an image, you go \setimage(label)[image][caption] \chapter{New Chapter with Image}. So: images stay 'off' (from \ImagesOff) until you proactively want to set an image by declaring \setimage; to make \chapters not expect an image again, you must reissue \ImagesOff.
    – jon
    Mar 31, 2016 at 4:08

You must log in to answer this question.

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