0

This code is inspired by the elegant article class designed by Harvey Sheppard.

I wanted to be able to change the image on the chapter title pages. In the code given below, I currently have just the odd page chapter title image change. I wanted to be able to put a new image each time on each chapter title page. Can you help me to accomplish this task? Thanks!

Here is the code: (A little beyond a 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

The code below defines two new commands, \ChapterImages and \InsertImage, that lets you set a comma separated list of image names and insert next image, respectively. Using these commands you can set the images for your chapters using

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

and you can insert these images on your chapter pages, one by one, by replacing your current \includegraphics command with \InsertImage. Each time it is used, the \InsertImage command inserts the next image in the list, or does nothing if no more images are available.

As an example, here is the title page for chapter 3 produced by the code below:

enter image description here

Here is the updated code:

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

The code itself is not very exciting: the comma separated list of images is put into a LaTeX3 sequence. These filenames are then popped off the front of the sequence and given to \includegraphics each time \InsertImage is used.

| improve this answer | |

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.