如何用整页图像替换章节样板?

如何用整页图像替换章节样板?

假设我正在制作一本书(使用\documentclass{book}),其中没有以文本形式显示章节名称,而是插入了包含章节文本的图像。我怎样才能开始新的章节并设置章节名称(用于标题和目录)而不实际显示它是新的章节?

我现在的代码是:

\chapter{My Chapter Title}
\AddToShipoutPicture*{\centering \includegraphics[width=\paperwidth]{chapter-1.png}}
\phantom{asdf}

当然chapter-1.png是我的整页图像,并且显然是正常工作\phantom{asdf}所必需的。\AddToShipoutPicture

基于 ”如何设置章节标题的格式“我在序言中尝试了这两种方法,但都没有奏效:

% Generated "LaTeX error: Missing \begin{document}"
\renewcommand{\@makechapterhead}[1]{
    \vfill
}

% Had no effect
\def\@makeschapterhead#1{
    \vfill
}

我还能做些什么来更改 \chapter 模板,甚至更改它可以设置的内部变量吗?我对自定义 TeX 布局还很陌生,以前只使用过论文等的预定义样式,所以我有点迷茫。

答案1

\picturechapter我将定义一个可以完成所有任务的新命令。

预先解决步骤仅用于演示

为了得到一个完整的最小工作示例,我们首先制作一个包含所有章节页面图片的 PDF。在现实世界中,您不需要这样做,因为您已经有了章节页面图片!

% This is only an example file to generate the pictures to be used
% at the following MWE. If you already have picture files (either
% one PDF with one picture per page or one file per picture) you
% won't need this file!!!
\documentclass{article}
\usepackage{graphicx,xcolor,eso-pic}
\begin{document}
  \pagestyle{empty}
  \centering
  \AddToShipoutPicture{%
    \AtPageLowerLeft{%
      \color{green}\rule{\paperwidth}{\paperheight}%
    }%
  }%
  \vspace*{\fill}\rotatebox{45}{\Huge Chapter page 1}\vspace*{\fill}\newpage
  \vspace*{\fill}\rotatebox{45}{\Huge Chapter page 2}\vspace*{\fill}\newpage
  \vspace*{\fill}\rotatebox{45}{\Huge Chapter page 3}\vspace*{\fill}\newpage
  \vspace*{\fill}\rotatebox{45}{\Huge Chapter page 4}\vspace*{\fill}\newpage
\end{document}

用名称保存此内容chapterpages.tex并执行pdflatex chapterpages制作chapterpages.pdf。此步骤仅用于为以下建议的解决方案提供测试基础!

解决建议

现在让我们定义一个新命令\picturechapter,加载一页chapterpages.pdf作为我们的章节图片页,但像真实页面一样设置目录条目和运行标题行\chapter

\documentclass{book}
\usepackage{pdfpages}

\usepackage[english]{babel}
\usepackage{blindtext}% For demo only (see below)

\usepackage{hyperref}% to show, that even hyperlinks work

% New switch to decide, if the chapter pictures are chapter-1,
% chapter-2 ... chapter-A, chapter-B ... or the pictures are
% the pages of one file chapterpages.pdf. If you have e.g. an
% appendix you have to use \singlepicturestrue
\newif\ifsinglepagepictures

% At this example we use one file chapterpags.pdf:
\singlepagepicturesfalse

\newcommand*{\picturechapter}[1]{%
  \cleardoublepage
  \refstepcounter{chapter}%
  \ifsinglepagepictures
    \includepdf[page=1,
      pagecommand={%
        \thispagestyle{empty}%
        \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}%
        \chaptermark{#1}%
      }%
    ]{chapter-\thechapter}%
  \else
    \includepdf[page=\arabic{chapter},
      pagecommand={%
        \thispagestyle{empty}%
        \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}%
        \chaptermark{#1}%
      }%
    ]{chapterpages}%
  \fi
}

\begin{document}
\tableofcontents
\picturechapter{This is my fist picture chapter}
\blindtext

\let\chapter\picturechapter% for demo purpose only to make \blinddocument use
                           % \picturechapter
\blinddocument
\blinddocument
\blinddocument
\end{document}

使用 pdflatex 处理此文件后,您将获得一个示例 pdf,其中包含绿色章节开始的图片页,这些图片页显示在目录中,标题为“1 这是我的第一张图片页”、“2 0 级标题(章节)”、“2 0 级标题(章节)”、“3 0 级标题(章节)”。甚至从目录到绿色页面的超链接都可以正常工作。绿色章节页面后面的页面的页眉也已设置。

建议的解决方案是使用和bookscrbookaKOMA 脚本类)。但 也应该与reportscrreprt或 几乎每个带有 的类一起使用\chapter。要将其与 article 类一起使用,您必须替换chapterbysection\chaptermarkby \sectionmark

所示建议的替代方案 1(每张图片一个文件)

如果您不想使用chapterpages.pdf包含所有图片的单个文件,而是使用单个文件chapter-1.pngchapter-2.jpgchapter-3.pdf,则只需将其替换\singlepicturesfalse\singlepicturestrue。在这种情况下,它甚至可以与带有数字的附录一起使用A、等BC相应的文件名将是chapter-A.pngchapter-B.png等。

方案 2(独立于章节计数器) 注意:如果您\thechapter使用重新定义\singlepicturesturetrue,您也必须重新定义章节图片。为避免这种情况,您可以使用自己的计数器:

\newcounter{chapterpicture}
\newcommand*{\picturechapter}[1]{%
  \cleardoublepage
  \refstepcounter{chapter}%
  \stepcounter{chapterpicture}% use next chapter picture file
  \ifsinglepagepictures
    \includepdf[page=1,
      pagecommand={%
        \thispagestyle{empty}%
        \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}%
        \chaptermark{#1}%
      }%
    ]{chapter-\thechapterpicture}%
  \else
    \includepdf[page=\thechapterpicture,
      pagecommand={%
        \thispagestyle{empty}%
        \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}%
        \chaptermark{#1}%
      }%
    ]{chapterpages}%
  \fi
}

在这种情况下,附录中的图片将是名称chapter-9.pngchapter-10.pdf(如果附录之前的最后一章是 8)。

方案3:(每个图片文件都有自己的名称,与章节编号无关)

另外,您可以定义一个带有第二个参数的命令:章节图片的名称(您无法使用上面的例子来测试这一点,因为\blinddocument不会设置第二个参数):

\newcommand*{\picturechapter}[2]{%
  \cleardoublepage
  \refstepcounter{chapter}%
  \includepdf[page=1,
    pagecommand={%
      \thispagestyle{empty}%
      \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}%
      \chaptermark{#1}%
    }%
  ]{#2}%
}

用法如下:

\picturechapter{My chapter heading}{filename}

此解决方案的优点是,如果您移动、删除或插入章节到文档中,则无需重命名章节图片文件。

答案2

我建议自定义默认标题命令,而不是禁用标题命令并在正文中放置自己的标题命令。使用\makeatletter\makeatother\begin{document},否则您会因为@宏名称中的字符而收到上述错误“缺失”。

例如:

\makeatletter
\renewcommand*{\@makechapterhead}[1]{
  \AddToShipoutPicture*{\centering\includegraphics[width=\paperwidth]{\chapterimage}}
}
\makeatother

同样,使用\@makeschapterhead时产生标题,并可以重新定义。\chapter*

进一步看看titlesec包,可用于自定义标题格式和间距,这样您就不必像上面那样重新定义内部类宏。

相关内容