将图像添加到章节标题页

将图像添加到章节标题页

此代码的灵感来自于哈维·谢泼德

我希望能够更改章节标题页上的图像。在下面给出的代码中,我目前只有奇数页章节标题图像更改。我希望能够每次在每个章节标题页上放置一张新图像。你能帮我完成这个任务吗?谢谢!

以下是代码:(略微超出 MWE 的范围)

\documentclass[a4paper, 10pt, fleqn, openany, twoside]{report}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Basic settings, options processing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage[l2tabu, orthodox]{nag}

\RequirePackage{polyglossia}
\setdefaultlanguage{english}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Needed packages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage[final=true,step=1]{microtype}
\RequirePackage{graphicx}
\RequirePackage{calc}
\RequirePackage[usenames,dvipsnames,svgnames,table]{xcolor}
\RequirePackage{ragged2e}
\RequirePackage{eso-pic}
\RequirePackage{fancyhdr}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Layout
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{changepage}

\patchcmd{\part}{\thispagestyle{plain}}{\thispagestyle{empty}}{}{\errmessage{Cannot patch \string\part}}

\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}
\setlength{\marginparpush}{1.5\baselineskip}

\RequirePackage[
    xetex,
    a4paper,
%   showframe,
    twoside,
    top=27mm,
    bottom=27mm,
    inner=20mm,
    outer=20mm,
    ignorehead,
    ignorefoot,
    includemp,
    marginparwidth=52mm,
    marginparsep=8mm,
    headsep=7mm,
    footskip=14mm,
    headheight=12.2pt,
]{geometry}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%      Colors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definecolor{mainColor}{RGB}{211, 47, 47}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Utilities
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DeclareDocumentCommand{\isOddPage}{mm}{%
    \strictpagechecktrue%
    \checkoddpage%
    \ifoddpage%
    #1%
    \else%
    #2%
    \fi%
}
\DeclareDocumentCommand{\alignLeftOrRight}{O{} O{}}{%
    \strictpagechecktrue%
    \checkoddpage%
    \ifoddpage%
    \begin{FlushRight}%
        #1%
        \else%
        \begin{FlushLeft}%
            #2%
            \fi%
        }
        \DeclareDocumentCommand{\alignLeftOrRightEnd}{O{} O{}}{%
            \strictpagechecktrue%
            \checkoddpage%
            \ifoddpage%
        \end{FlushRight}%
        #1%
        \else%
    \end{FlushLeft}%
    #2%
    \fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Titling
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{titlesec}
\titleformat{\chapter}[block]
{} % format
{ % label
    \backgroundThisPageColor%
    \isOddPage{%
        \begin{tikzpicture}[remember picture, overlay]
        \coordinate[xshift=-\bigVerticalLineWidth/2, yshift=-5.8cm] (numberCenter) at (current page.north east);
        \draw[White] (numberCenter) node{
            \fontsize{6.5cm}{7.8cm}\selectfont
            \color{White}
            \thechapter
        };
        \end{tikzpicture}%
    }{%
    \begin{tikzpicture}[remember picture, overlay]
    \coordinate[xshift=\bigVerticalLineWidth/2, yshift=-5.8cm] (numberCenter) at (current page.north west);
    \draw[White] (numberCenter) node{
        \fontsize{6.5cm}{7.8cm}\selectfont
        \color{White}
        \thechapter
    };
    \end{tikzpicture}%
}
}
{0pt} % sep
{ % code before
    \isOddPage{
        \begin{FlushRight}
            \vspace*{-1.5mm}
    }{
        \begin{FlushLeft}
        \vspace*{-10mm}
    }
    \fontsize{2.5cm}{3cm}\selectfont%
}[ % code after
\alignLeftOrRightEnd
]
\titleformat{name=\chapter, numberless}[block]
{} % format
{\backgroundThisPageColor} % label
{0pt} % sep
{ % code before
    \alignLeftOrRight
    \fontsize{2.5cm}{3cm}\selectfont    
}[ % code after
\alignLeftOrRightEnd
]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       TikZ
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{calc}
\usetikzlibrary{matrix}

\pgfdeclarelayer{bg}    % declare background layer
\pgfsetlayers{bg,main}  % set the order of the layers (main is the standard layer)

\newlength{\bigVerticalLineWidth}
\setlength{\bigVerticalLineWidth}{\evensidemargin + 1in + \hoffset}

\newlength{\bigVerticalLinePartOfMarginParSep}
\setlength{\bigVerticalLinePartOfMarginParSep}{\marginparsep/8}
\setlength{\bigVerticalLinePartOfMarginParSep}{5\bigVerticalLinePartOfMarginParSep}

\addtolength{\bigVerticalLineWidth}{-\bigVerticalLinePartOfMarginParSep}

\newcommand{\bigVerticalLine}[1]{
    \strictpagechecktrue
    \checkoddpage
    \ifoddpage%
    \AddToShipoutPictureBG*{
        \begin{tikzpicture}[remember picture, overlay]
            \fill[fill=#1] (current page.south east) rectangle ++(-\bigVerticalLineWidth, \paperheight);
        \end{tikzpicture}
    }
    \else%

    \AddToShipoutPictureBG*{
        \hspace{-2.6pt}\includegraphics[width=7.7cm,height=\paperheight]{example-image-a}}
    \fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Backgrounding commands
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Vertical lines
\newcommand{\backgroundThisPageGrey}{\bigVerticalLine{bigVerticalLineGrey}}
\newcommand{\backgroundThisPageColor}{\bigVerticalLine{mainColor}}

% Entire page area
\newcounter{background}[page]
\renewcommand{\thebackground}{\arabic{page}-\arabic{background}}

\DeclareDocumentCommand{\startBackgroundPageTop}{O{\thebackground}}{\begin{tikzpicture}[overlay, remember picture]%
    \node at (current page.north west) {\backgroundAnchor{begin-#1}};
\end{tikzpicture}}

\usepackage{lipsum}

\begin{document}

    \chapter{Introduction}

    \lipsum[2]

    \chapter{Content}

    \lipsum[2]
    \lipsum[3]

    \section{First Section}
    \subsection{Subsection}

    \lipsum[2]

\end{document} 

答案1

以下代码定义了两个新命令,\ChapterImages\InsertImage,分别允许您设置逗号分隔的图像名称列表和插入下一张图片。使用这些命令,您可以使用以下命令为章节设置图像

\ChapterImages{
    example-image-a,
    example-image-b,
    example-image-c
}

\includegraphics您可以将当前命令替换为 ,从而将这些图像逐一插入到章节页面中\InsertImage。每次使用该命令时,该\InsertImage命令都会插入列表中的下一张图片,如果没有更多可用图片,则不执行任何操作。

例如,下面是使用以下代码生成的第 3 章的标题页:

在此处输入图片描述

以下是更新后的代码:

\documentclass[a4paper, 10pt, fleqn, openany, twoside]{report}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Chapter images
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \g_chapter_images_seq
\NewDocumentCommand\ChapterImages{m}{
  \seq_gset_from_clist:Nn \g_chapter_images_seq {#1}
}
\NewDocumentCommand\InsertImage{}{
  % print the next image, if we have one
  \seq_if_empty:NF \g_chapter_images_seq {
    \seq_gpop_left:NN \g_chapter_images_seq \l_tmpa_tl
    \hspace{-2.6pt}\includegraphics[width=7.7cm,height=\paperheight]{\tl_use:N \l_tmpa_tl}
  }
}
\ExplSyntaxOff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ChapterImages{
    example-image-a,
    example-image-b,
    example-image-c
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage[l2tabu, orthodox]{nag}

\RequirePackage{polyglossia}
\setdefaultlanguage{english}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Needed packages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage[final=true,step=1]{microtype}
\RequirePackage{graphicx}
\RequirePackage{calc}
\RequirePackage[usenames,dvipsnames,svgnames,table]{xcolor}
\RequirePackage{ragged2e}
\RequirePackage{eso-pic}
\RequirePackage{fancyhdr}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Layout
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{changepage}

\patchcmd{\part}{\thispagestyle{plain}}{\thispagestyle{empty}}{}{\errmessage{Cannot patch \string\part}}

\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}
\setlength{\marginparpush}{1.5\baselineskip}

\RequirePackage[
    xetex,
    a4paper,
%   showframe,
    twoside,
    top=27mm,
    bottom=27mm,
    inner=20mm,
    outer=20mm,
    ignorehead,
    ignorefoot,
    includemp,
    marginparwidth=52mm,
    marginparsep=8mm,
    headsep=7mm,
    footskip=14mm,
    headheight=12.2pt,
]{geometry}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%      Colors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definecolor{mainColor}{RGB}{211, 47, 47}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Utilities
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DeclareDocumentCommand{\isOddPage}{mm}{%
    \strictpagechecktrue%
    \checkoddpage%
    \ifoddpage%
    #1%
    \else%
    #2%
    \fi%
}
\DeclareDocumentCommand{\alignLeftOrRight}{O{} O{}}{%
    \strictpagechecktrue%
    \checkoddpage%
    \ifoddpage%
    \begin{FlushRight}%
        #1%
        \else%
        \begin{FlushLeft}%
            #2%
            \fi%
        }
        \DeclareDocumentCommand{\alignLeftOrRightEnd}{O{} O{}}{%
            \strictpagechecktrue%
            \checkoddpage%
            \ifoddpage%
        \end{FlushRight}%
        #1%
        \else%
    \end{FlushLeft}%
    #2%
    \fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Titling
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{titlesec}
\titleformat{\chapter}[block]
{} % format
{ % label
    \backgroundThisPageColor%
    \isOddPage{%
        \begin{tikzpicture}[remember picture, overlay]
        \coordinate[xshift=-\bigVerticalLineWidth/2, yshift=-5.8cm] (numberCenter) at (current page.north east);
        \draw[White] (numberCenter) node{
            \fontsize{6.5cm}{7.8cm}\selectfont
            \color{White}
            \thechapter
        };
        \end{tikzpicture}%
    }{%
    \begin{tikzpicture}[remember picture, overlay]
    \coordinate[xshift=\bigVerticalLineWidth/2, yshift=-5.8cm] (numberCenter) at (current page.north west);
    \draw[White] (numberCenter) node{
        \fontsize{6.5cm}{7.8cm}\selectfont
        \color{White}
        \thechapter
    };
    \end{tikzpicture}%
}
}
{0pt} % sep
{ % code before
    \isOddPage{
        \begin{FlushRight}
            \vspace*{-1.5mm}
    }{
        \begin{FlushLeft}
        \vspace*{-10mm}
    }
    \fontsize{2.5cm}{3cm}\selectfont%
}[ % code after
\alignLeftOrRightEnd
]
\titleformat{name=\chapter, numberless}[block]
{} % format
{\backgroundThisPageColor} % label
{0pt} % sep
{ % code before
    \alignLeftOrRight
    \fontsize{2.5cm}{3cm}\selectfont
}[ % code after
\alignLeftOrRightEnd
]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       TikZ
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{calc}
\usetikzlibrary{matrix}

\pgfdeclarelayer{bg}    % declare background layer
\pgfsetlayers{bg,main}  % set the order of the layers (main is the standard layer)

\newlength{\bigVerticalLineWidth}
\setlength{\bigVerticalLineWidth}{\evensidemargin + 1in + \hoffset}

\newlength{\bigVerticalLinePartOfMarginParSep}
\setlength{\bigVerticalLinePartOfMarginParSep}{\marginparsep/8}
\setlength{\bigVerticalLinePartOfMarginParSep}{5\bigVerticalLinePartOfMarginParSep}

\addtolength{\bigVerticalLineWidth}{-\bigVerticalLinePartOfMarginParSep}

\newcommand{\bigVerticalLine}[1]{
    \strictpagechecktrue
    \checkoddpage
    \ifoddpage%
    \AddToShipoutPictureBG*{
        \begin{tikzpicture}[remember picture, overlay]
            \fill[fill=#1] (current page.south east) rectangle ++(-\bigVerticalLineWidth, \paperheight);
        \end{tikzpicture}
    }
    \else%
      \AddToShipoutPictureBG*{ \InsertImage }
    \fi%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Backgrounding commands
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Vertical lines
\newcommand{\backgroundThisPageGrey}{\bigVerticalLine{bigVerticalLineGrey}}
\newcommand{\backgroundThisPageColor}{\bigVerticalLine{mainColor}}

% Entire page area
\newcounter{background}[page]
\renewcommand{\thebackground}{\arabic{page}-\arabic{background}}

\DeclareDocumentCommand{\startBackgroundPageTop}{O{\thebackground}}{\begin{tikzpicture}[overlay, remember picture]%
    \node at (current page.north west) {\backgroundAnchor{begin-#1}};
\end{tikzpicture}}

\usepackage{lipsum}

\begin{document}

    \chapter{Introduction}

    \lipsum[2]

    \chapter{Content}

    \lipsum[2]
    \lipsum[3]

    \section{First Section}
    \subsection{Subsection}

    \lipsum
    \lipsum

    \clearpage
    \clearpage
    \chapter{Second}

    \lipsum[2]
    \lipsum[3]

    \section{First Section}
    \subsection{Subsection}

    \lipsum

    \clearpage
    \chapter{Second}

    \lipsum[2]
    \lipsum[3]

    \section{First Section}
    \subsection{Subsection}

    \lipsum

\end{document}

代码本身并不令人兴奋:将逗号分隔的图像列表放入 LaTeX3 序列中。然后从序列的前面弹出这些文件名,并\includegraphics每次\InsertImage使用时将其赋值。

相关内容