1

I'm using the memoir class to typeset a book in which I want to start chapters with a picture on the left page and then on the right page "Chapter 1: Hello World" followed by text. So I'm playing around with this test document and I can't figure out how to get the extra parameters to show up where I want them. Once I get this figured out, I suppose I could substitute in \includegraphics{blah.png} for the "Foo Bar Baz" stuff.

\documentclass[openleft]{memoir}
\usepackage{lipsum}
\parindent=0pt

\title{Test}
\author{Joe Blow}

\makeatletter
\renewcommand{\@makechapterhead}[3]{%
  Foo: #1\\
  Bar: #2\\
  Baz: #3
  \newpage
  {
  \chapterheadstart
    \printchaptername \chapternamenum \printchapternum\\
    \printchaptertitle{\Huge #1}\\
    \afterchaptertitle
  }
}
\makeatother

\begin{document}
\maketitle
\tableofcontents

\chapter[hello world]{Hello\
World}{Tree}
\lipsum[1-5]

\chapter[hello plants]{Hello\
Plants}{Flowerpot}
\lipsum[6-10]
\end{document}

Instead, I get this on the left side (first chapter):

Foo: Hello World
Bar:
Baz:

Then on the right, I get the chapter header as expected. The chapter name also shows up in the table of contents as expected. But then in the bodies of the text, the first word is the second parameter in curly brackets. For this example, I'd see "Tree" for the first word of the first chapter and "Flowerpot" for the first word of the second chapter. Why did that parameter appear there? How can I make that second parameter appear next to "Bar:" on the previous page?

Adding this MWE as requested by @peter-wilson

\documentclass[12pt]{memoir}
\usepackage{graphicx}
\usepackage{libertine}
\usepackage{lipsum}
\usepackage{hyperxmp}
\usepackage[pdfusetitle,colorlinks]{hyperref}

\parindent=0pt

\title{Test}
\author{Joe Blow}

\makeatletter 
\hypersetup{%
  pdftitle={Test},
  pdfauthor={Joe Blow},
  bookmarksnumbered,
  pdfstartview={FitH},
  allcolors=black,
  pdfcopyright={Copyright 2020 by Joe Blow.  All rights deserved.},
  pdfinfo={
    {Copyright 2020 by Joe Blow.  All rights deserved.}
  }
}%
\makeatother

\makeatletter
\newcommand\chapimage[2]{
  \cleartoverso
  \thispagestyle{empty}
  \begin{center}
  \vspace*{\fill}
  \includegraphics[keepaspectratio=true,height=0.85\textheight]{#1}\par%
  \huge #2
  \vspace*{\fill}
  \end{center}
%  \clearpage
}
\makeatother

\makeatletter
\def\@makechapterhead#1{%
  {
  \chapterheadstart
    \printchaptername \chapternamenum \printchapternum\\
    \printchaptertitle{\Huge #1}\\
    \afterchaptertitle
  }
}
\makeatother

\begin{document}

\maketitle
\tableofcontents

\chapimage{tree}{The Tree}
\chapter[hello world]{Hello\
World}
\lipsum[1-15]

\chapimage{flowerpot}{The Flowerpot}
\chapter[hello plants]{Hello\
Plants}
\lipsum[16-20]
\end{document}

When built as a PDF with pdflatex, this is fine for print. When presented as a PDF with hyperlinks, clicking on a chapter in the table of contents takes me not to the leading picture, but where "Chapter 1", "Chapter 2", and so on appear. When I make it into an epub with tex4ebook -xs -f epub -c test.cfg test.tex, this effect is worse: the caption appears on a page by itself after the image and before the beginning of the chapter.

Here is a test.cfg file that's identical to what I've been using so far:

\Preamble{xhtml}
\Css{div.caption{text-align:center;}}
\Configure{float}{\csname par\endcsname\ShowPar}
{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="float">}}
{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar}
\ConfigureEnv{figure}
{\IgnorePar\EndP\HCode{<div class="figure">\Hnewline}%
\bgroup \Configure{float}{\ShowPar}{}{}%
}
{\egroup\IgnorePar\EndP\HCode{</div>}\par}
{}{}

\Css{div.float, div.figure{margin-left:auto;margin-right:auto;}}

\Configure{writetoc}{\let\\\space}
\Configure{UniqueIdentifier}{Foo Bar Press}
\DeclareGraphicsExtensions{.png,.jpg,.gif}

\begin{document}
\EndPreamble

Now looking at the files within the epub, I see this at the bottom of testli1.html, which is where the table of contents is found:

<div class="center" 
>
<!--l. 57--><p class="noindent" >
</p><!--l. 57--><p class="noindent" ><img 
src="tree.png" alt="PIC"  
width="265" height="466"  />
</p><!--l. 57--><p class="noindent" ><span 
class="rm-lmr-17x-x-143">The Tree</span> </p></div>
<!--l. 58--><div class="crosslinks"><p class="noindent"></p></div>

</body></html>

That inclusion of tree.png should be near the beginning of testch1.html. In testch1.html I see this at the bottom:

<div class="center" 
>
<!--l. 62--><p class="noindent" >
</p><!--l. 62--><p class="noindent" ><img 
src="flowerpot.png" alt="PIC"  
width="360" height="466"  />
</p><!--l. 62--><p class="noindent" ><span 
class="rm-lmr-17x-x-143">The Flowerpot</span> </p></div>
<!--l. 63--><div class="crosslinks"><p class="noindent"></p></div> 

That should appear instead near the beginning of testch2.html.

Now here's the top of testch1.html:

<head><title>1 Hello World</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)" /> 
<meta name="originator" content="TeX4ht (http://www.tug.org/tex4ht/)" /> 
<!-- charset=utf-8,epub,uni-html4,html,xhtml --> 
<meta name="src" content="test.tex" /> 
<link rel="stylesheet" type="text/css" href="test.css" /> 
</head><body 
>
<!--l. 59--><div class="crosslinks"><p class="noindent"></p></div>
<h2 class="chapterHead"><span class="titlemark">Chapter 1</span><br /><a 
 id="x3-20001"></a>Hello World</h2>

The image and caption should appear somewhere just before the <h2></h2>.

  • 1
    \chapter only takes one argument in {} why are you giving it two? On the other hand in memoir \chapter can take two optional arguments in []'s – daleif Jan 21 at 13:16
  • More to the point, \@makechapterhead in memoir only has one argument, which is passed directly by \chapter. While it is possible to pass arguments indirectly to another macro, the second macro has to be the very last thing in the first macro (or next to last using \expandafter). – John Kormylo Jan 21 at 14:44
1

The problem is that tex4ebook break document into standalone HTML files at beginning of each chapter, so the \chapterimage is placed at the end of the previous chapter. I would redefine this command to just save the image address and caption. This information can be reused in the \chapter command. This ensures that the image is placed in the right file and displayed together with the chapter title.

Try the following configuration:

\Preamble{xhtml}
\makeatletter
\renewcommand\chapimage[2]{\def\chapimagesrc{#1}\def\chapimagedesc{#2}}
\Configure{chapter}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="chapter-header">}% insert container element that will be used for 
% linking from TOC
\ifdefined\chapimagesrc%
  \begin{center}%
  \includegraphics[keepaspectratio=true,height=0.85\textheight]{\chapimagesrc}\par%
  \HCode{<span class="chapimagedesc">}\chapimagedesc\HCode{</span>}% 
  \end{center}%
\global\let\chapimagesrc\@undefined%
\global\let\chapimagedesc\@undefined%
\fi%
}{}
   {\IgnorePar\EndP\HCode{<h2 class="chapterHead"\csname a:LRdir\endcsname>}%
    \if@mainmatter \TitleMark{\HCode{<br />}}\fi }
   {\HCode{</h2></div>}\IgnoreIndent\par}
\Css{.chapimagedesc{font-size:2em;}}
\Css{h2.chapterHead{page-break-before:always;}}
\makeatother
\Configure{UniqueIdentifier}{Foo Bar Press}
\Css{div.caption{text-align:center;}}
\Configure{float}{\csname par\endcsname\ShowPar}
{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="float">}}
{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar}
\ConfigureEnv{figure}
{\IgnorePar\EndP\HCode{<div class="figure">\Hnewline}%
\bgroup \Configure{float}{\ShowPar}{}{}%
}
{\egroup\IgnorePar\EndP\HCode{</div>}\par}
{}{}

\Css{div.float, div.figure{margin-left:auto;margin-right:auto;}}

\Configure{writetoc}{\let\\\space}
\Configure{UniqueIdentifier}{Foo Bar Press}
\DeclareGraphicsExtensions{.png,.jpg,.gif}

\begin{document}
\EndPreamble

The important part is this:

\renewcommand\chapimage[2]{\def\chapimagesrc{#1}\def\chapimagedesc{#2}}
\Configure{chapter}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="chapter-header">}% insert container element that will be used for 
% linking from TOC
\ifdefined\chapimagesrc%
  \begin{center}%
  \includegraphics[keepaspectratio=true,height=0.85\textheight]{\chapimagesrc}\par%
  \HCode{<span class="chapimagedesc">}\chapimagedesc\HCode{</span>}% 
  \end{center}%
\global\let\chapimagesrc\@undefined%
\global\let\chapimagedesc\@undefined%
\fi%
}{}
   {\IgnorePar\EndP\HCode{<h2 class="chapterHead"\csname a:LRdir\endcsname>}%
    \if@mainmatter \TitleMark{\HCode{<br />}}\fi }
   {\HCode{</h2></div>}\IgnoreIndent\par}
\Css{.chapimagedesc{font-size:2em;}}
    \renewcommand\chapimage[2]{\def\chapimagesrc{#1}\def\chapimagedesc{#2}}
\Configure{chapter}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="chapter-header">}% insert container element that will be used for 
% linking from TOC
\ifdefined\chapimagesrc%
  \begin{center}%
  \includegraphics[keepaspectratio=true,height=0.85\textheight]{\chapimagesrc}\par%
  \HCode{<span class="chapimagedesc">}\chapimagedesc\HCode{</span>}% 
  \end{center}%
\global\let\chapimagesrc\@undefined%
\global\let\chapimagedesc\@undefined%
\fi%
}{}
   {\IgnorePar\EndP\HCode{<h2 class="chapterHead"\csname a:LRdir\endcsname>}%
    \if@mainmatter \TitleMark{\HCode{<br />}}\fi }
   {\HCode{</h2></div>}\IgnoreIndent\par}
\Css{.chapimagedesc{font-size:2em;}}
\Css{h2.chapterHead{page-break-before:always;}}

The \chapterimage command defines two macros, \chapimagesrc and \chapimagedesc that contains the current image filename and caption. This information is included at the beginning of the chapter using \Configure{chapter}.

The chapter image will be placed on separate page thanks to \Css{h2.chapterHead{page-break-before:always;}}.

Because TOC links directly to the chapter title, it is necessary to move the link destination before the chapter image. This can be achieved using the following build file:

local domfilter = require "make4ht-domfilter"

local process = domfilter { function(dom)
  -- process headers
  for _,  header in ipairs(dom:query_selector "div.chapter-header") do
    -- find chapter toc id
    for _, a in ipairs(header:query_selector "h2 a") do
      local id = a:get_attribute("id") 
      if id then
        -- move the 
        a:set_attribute("id", nil)
        header:set_attribute("id", id)
      end
    end
  end
  return dom
end}

Make:match("html$", process)

This is the result:

enter image description here

  • At least with Calibre, it looks like this configuration places the image and its caption on the same page with "Chapter 1\nHello World". It's not exactly what I was looking for, but it won't look bad. Thanks! Is there any sort of magic I can do to have the first page of the ebook have just the image and caption and the next page begin with the chapter header stuff? Can you comment on making hyperlinks in PDF go to the image instead of the chapter header? – Frotz Feb 12 at 3:50
  • @Frotz I've updated my answer – michal.h21 Feb 12 at 13:12
0

Much simpler to create a macro for the left picture page and leave \chapter as is, then you don't have to worry about the organisation of 6 arguments --- 4 required and 2 optional.

% prechapprob.tex  SE 525098 Picture before chapter
\documentclass{memoir}
\usepackage{lipsum}

\newcommand{\prechap}[3]{%  % put on the next even page
  \cleartoevenpage
  \thispagestyle{empty}
  Foo: #1 \\
  Bar: #2 \\
  Baz: #3}

\begin{document}
\tableofcontents

\prechap{Hello}{World}{Tree}
\chapter{Tree}
\lipsum[1-5]

\prechap{Hello}{Earth}{Plants}
\chapter{Flowerpot}
\lipsum[6-10]

\end{document}
  • I already did this, but it means that the \prechap stuff appears in the previous chapter. That causes problems with hypertext and tex4ebook. If you go directly to a chapter, the preceding image isn't shown. For tex4ebook, there is often an extra blank page between the image and the chapter header. – Frotz Jan 21 at 18:47
  • @Frotz Then you should provide an MWE that shows your specific problem. As far as I am concerned I provided a possible solution to your stated problem.--- GOM – Peter Wilson Jan 22 at 20:23
  • I've added the MWE showing my problems and describing what I want in greater detail. – Frotz Jan 29 at 7:40
  • @Frotz I appreciate all the extra information you have provided but I don't feel competent to address all your issues. Perhaps ask another question that somebody else might pick up on. Apologies. – Peter Wilson Jan 30 at 18:26

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.