1

OBJECTIVE:

to have an ability to place different background images on every page of the book TOC.

WHAT HAS BEEN TRIED ALREADY:

1

\AddToShipoutPicture*{%
  \AtPageLowerLeft{%
   \put(.5\textwidth,.5\textheight) {\includegraphics[width=50mm,height=50mm,keepaspectratio]{example-image}}%
  }
}

works but places only one image on the first page of the TOC

2

\AddToHook{shipout/background}{%
\put(0mm,-240mm){\ifnum\value{page}=0 \includegraphics[width=200mm,height=250mm,keepaspectratio]{example-image}\fi}
}

doesn't work because TOC's pages are gobbled, meaning not numbered, so I can't hook the image to the page number.

3

 \backgroundsetup{
scale=1,
color=black,
opacity=1,
angle=0,
position={47.27mm,-7mm}, 
contents={%
  \includegraphics[height=15ex,keepaspectratio]{example-image}
  }%
}  
\BgThispage

works but also places only one image on the first page of the TOC

4

also found this post but it seems to place one image over all pages of the TOC. In addition, I wouldn't want to use background package since I read that it's outdated and gives me Package everypage Warning, thus must be replaced.
So how do I actually add different background images to any/some/all pages of the TOC, having in mind that they are gobbled, and I'd rather avoid using background package?

Please find the following MWE useful. Don't mind its clumsiness, it was compiled solely for the sake of the example.

% !TEX TS-program = LuaLaTeX
\documentclass[11pt,twoside,openany]{book}
\pagestyle{plain}

\usepackage[english, russian]{babel}
\usepackage{fontspec}
\setmainfont{EB Garamond}[
]

\usepackage{microtype}
\usepackage[shortcuts]{extdash} 
\usepackage[pagewise]{lineno}
\linenumbers
\usepackage[
  paperwidth=6.125in,
  paperheight=9.25in,
  inner=0.625in,
  top=15mm,
  outer=20mm,
  bottom=24mm, 
  heightrounded 
]{geometry} 

\setlength{\parskip}{0ex}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}

\usepackage{eso-pic}

\usepackage{fancyhdr}
\fancyhf{} 
\renewcommand{\headrulewidth}{0ex} 
\fancyfoot[LE,RO]{\thepage} 
\pagestyle{fancy}
\fancypagestyle{plain}{%
  \fancyhf{}%
  \renewcommand{\headrulewidth}{0ex}%
  \fancyhf[lef,rof]{\thepage}%
}

\usepackage{titlesec}
\titleclass{\part}{top}
\titleformat{\part}{\vfil\penalty500\vfilneg\centering\normalfont\LARGE}{\thepart.}{8ex}{\LARGE\centering}
\titlespacing*{\part}{0ex}{0ex plus 4ex minus 1ex}{-4ex} 
\titleclass{\chapter}{straight}
\titleformat{\chapter}{\vfil\penalty500\vfilneg\centering\normalfont\large}{\thechapter.}{8ex}{\large\centering}
\titlespacing*{\chapter} {0ex}{4ex plus 4ex minus 1ex}{-2ex} 

\usepackage{tocloft}
\addto\captionsrussian{%
  \renewcommand{\contentsname}{\normalfont\Large\textcolor{Black}{TOC}}%
}
\addtocontents{toc}{\cftpagenumbersoff{part}} 
\setlength{\cftbeforetoctitleskip}{0ex}
\setlength{\cftaftertoctitleskip}{17ex}
\renewcommand{\cfttoctitlefont}{\hfill\Large\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}
\renewcommand{\cftpartfont}{\large}
\renewcommand{\cftchapfont}{\normalsize}
\renewcommand{\cftpartpagefont}{\normalfont}
\renewcommand{\cftchappagefont}{\normalfont}
\renewcommand\cftchapleader{\cftdotfill{1}} 
\cftsetindents{part}{5.5em}{0em} \cftsetindents{chapter}{1em}{2em}
\renewcommand{\cftchapdotsep}{\cftdotsep}

\begin{document}

\frontmatter
\pagestyle{empty}
\setcounter{page}{0}

 
\begin{titlepage}
  \centering
  \fontsize{20ex}{20ex}\selectfont\bfseries\textcolor{Black} {TITLE PAGE}\par
\end{titlepage}
\clearpage

\baselineskip=12.5pt
\mainmatter

\pagestyle{plain}
\part[PART 1.]{\textcolor{Black}{PART 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 

\part[PART 2.]{\textcolor{Black}{PART 2}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\clearpage
 \AddToShipoutPicture*{%
  \AtPageLowerLeft{%
   \put(.5\textwidth,.5\textheight) {\includegraphics[width=50mm,height=50mm,keepaspectratio]{example-image}}%
  }
}

\pagenumbering{gobble}   
  \tableofcontents

  \end{document}
0

2 Answers 2

1

The package zref provides an enhanced cross-referencing-mechanism for LaTeX.

You can use the package zref for storing and retrieving from a zref-label the value of the so-called "absolute page number" of the page where the table of contents begins.
In the example below that zref-label is called "toc-start".

Additionally you can define a macro \CodeForBackgroundOfAbsolutePage where you can specify an absolute page number and some code so that directives are appended to the hook "shipout/background" for carrying out the code only inside the background-picture-environment of the page whose absolute page number has the specified value.

In the example below the absolute page number can be specified by having TeX do calculations which involve accessing a zref-label. Namely the zref-label "toc-start". Therefore in the example below \CodeForBackgroundOfAbsolutePage does not take only two arguments (absolute page number and code) but takes three arguments, one of them denoting a comma-separated list of zref-labels that must be defined for the specification of the absolute page number to work out. If one of those zref-labels is not defined, as in any case is the case in the first compilation, the code won't be carried out, either.

So syntax of \CodeForBackgroundOfAbsolutePage is:

\CodeForBackgroundOfAbsolutePage
  {<comma-list of zref-labels that must exist for the code to be carried out>%
    % Here you specify both those zref-labels that are needed for the
    % <expression for computing the absolute page number of the page where code
    % shall be carried out> to work out and probably also those zref-labels that
    % are needed for the <code to execute in the page's background-picture-
    % environment> to work out.
  }%
  {<expression for computing the absolute page number of the page where code 
    shall be carried out>}%
  {<code to carry out within the specified page's background-picture-
    environment>}%

The command \CodeForBackgroundOfAbsolutePage only has effect in case the page to be typeset has not yet been typeset when TeX encounters that command. Therefore I recommend using it before \begin{document}.
Other things might add to the hook "shipout/background", too. The order in which things are added is relevant because this affects how things that are not drawn above/below/left/right but atop each other overlap.

With the example below zref is used for placing some sort of cross referencing label.
With the example below this is done via adding to the .toc-file, which holds the data of the table of contents, the directive to place a zref-label "toc-start".
In the first latex-run the .toc-file holding data of the table of contents does not yet exist.
The command \tableofcontents is at the end of the document. So in the second run the .toc file is processed at the end of the LaTeX-run, and thus directives in that file to create cross-referencing-labels/to create zref-labels are carried out at the end of the second latex-run.
Which in turn implies that those cross-referencing-labels/zref-labels are undefined/not available until the end of the second latex-run.
Which in turn implies that the document needs to be compiled at least three times for having everything match out correctly.
In any case obey the messages and warnings regarding the need of re-running LaTeX that occur on the console and in the .log-file.

Things may be more exact and you may reduce to needing to compile the document only twice instead of three times by not adding the directive for placing the zref-label "toc-start" to the .toc-file but instead patching the command \tableofcontents to carry out that directive right after the first word or line of the table of contents has been typeset—this may be the headline of the table of contents.

% !TEX TS-program = LuaLaTeX
%===============================================================================
\makeatletter
\RequirePackage[abspage]{zref}
\ExplSyntaxOn
%...............................................................................
% Save the  value of abspage-counter to a zref-label:
%...............................................................................
\cs_new_protected:Npn \saveabspage #1 {\zref@labelbyprops{#1}{abspage}}
%...............................................................................
% Obtain the saved abspage-counter-value from zref-label if possible, otherwise
% deliver -1:
%...............................................................................
\cs_new_protected:Npn \getabspage #1 {\zref@extractdefault{#1}{abspage}{-1}}
%...............................................................................
% Map over comma-list #1 denoting zref-labels and execute code #2 in case all of
% them are defined, otherwise execute code #3; this is for doing calculations 
% with zref-labels which can only be done in case all of them are defined:
%...............................................................................
\cs_new:Npn \CheckWhetherAllZlabelsDefined #1 
  {
    \exp:w
      \clist_map_function:nN {#1} \mystuff_checkzlabel:nwnn
      \exp_after:wN\exp_end:\use_i:nn
  }
\cs_new:Npn \mystuff_checkzlabel:nwnn #1#2 \exp_after:wN\exp_end:\use_i:nn #3#4
  {
    \int_compare:nNnTF {\getabspage{#1}}={-1}
      {#2 \exp_after:wN\exp_end:\use_i:nn {#4}}
      {#2 \exp_after:wN\exp_end:\use_i:nn {#3}}{#4}%
  }
%...............................................................................
% Map over comma-list denoting zref-labels and generate warning for each 
% undefined zref-label:
%...............................................................................
\cs_new_protected:Npn \WarnForUndefinedZlabels #1
  {
    \clist_map_function:nN {#1}\zref@refused
  }
%...............................................................................
% Provide code to execute on background-picture-environment of page whose
% absolute page number is to be specified:
%...............................................................................
\cs_new_protected:Npn \CodeForBackgroundOfAbsolutePage#1#2#3{ 
  % #1 = comma-list of zref-labels that must exist for the code (#3) to be 
  %      carried out.
  % #2 = expression for computing the absolute page number of the page within
  %      whose background-picture-environment code (#3) shall be carried out:
  % #3 = code to carry out within the specified page's background-picture-
  %      environment
  \AddToHook{shipout/background}{
    \WarnForUndefinedZlabels{#1}
    \int_compare:nNnT {\CheckWhetherAllZlabelsDefined{#1}{#2}{-1}}
                      =
                      {\value{abspage}}
                      {#3}
  }
}
\ExplSyntaxOff
%...............................................................................
% Add a zref-label denoting the absolute page number of the first page of the 
% table of contents:
%   Depending on preface delivered by the \tableofcontents-command the following
%   might not be exact so that instead of adding to the .toc-file you might
%   need to patch the command \tableofcontents to place the zref-label
%   "toc-start" right after the heading of the table of contents.
%...............................................................................
\AtBeginDocument{\addtocontents{toc}{\saveabspage{toc-start}}}%
\makeatother
%===============================================================================

\documentclass[11pt,twoside,openany]{book}
\pagestyle{plain}

\usepackage[english, russian]{babel}
\usepackage{fontspec}
\setmainfont{EB Garamond}[]

\usepackage{microtype}
\usepackage[shortcuts]{extdash} 
\usepackage[pagewise]{lineno}
\linenumbers
\usepackage[
  paperwidth=6.125in,
  paperheight=9.25in,
  inner=0.625in,
  top=15mm,
  outer=20mm,
  bottom=24mm, 
  heightrounded 
]{geometry} 

\setlength{\parskip}{0ex} % Better use a package for this kind of things which
                          % takes subtleties into account.
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}

\usepackage{fancyhdr}
\fancyhf{} 
\renewcommand{\headrulewidth}{0ex} 
\fancyfoot[LE,RO]{\thepage} 
\pagestyle{fancy}
\fancypagestyle{plain}{%
  \fancyhf{}%
  \renewcommand{\headrulewidth}{0ex}%
  \fancyhf[lef,rof]{\thepage}%
}

\usepackage{titlesec}
\titleclass{\part}{top}
\titleformat{\part}{\vfil\penalty500\vfilneg\centering\normalfont\LARGE}%
            {\thepart.}{8ex}{\LARGE\centering}
\titlespacing*{\part}{0ex}{0ex plus 4ex minus 1ex}{-4ex} 
\titleclass{\chapter}{straight}
\titleformat{\chapter}{\vfil\penalty500\vfilneg\centering\normalfont\large}%
            {\thechapter.}{8ex}{\large\centering}
\titlespacing*{\chapter} {0ex}{4ex plus 4ex minus 1ex}{-2ex} 

\usepackage{tocloft}
%...............................................................................
% Probably it is nice to be informed about changes in auxiliary files:
\usepackage[starttoc]{rerunfilecheck}
%...............................................................................
\addto\captionsrussian{%
  \renewcommand{\contentsname}{\normalfont\Large\textcolor{Black}{TOC}}%
}
\addtocontents{toc}{\cftpagenumbersoff{part}} 
\setlength{\cftbeforetoctitleskip}{0ex}
\setlength{\cftaftertoctitleskip}{17ex}
\renewcommand{\cfttoctitlefont}{\hfill\Large\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}
\renewcommand{\cftpartfont}{\large}
\renewcommand{\cftchapfont}{\normalsize}
\renewcommand{\cftpartpagefont}{\normalfont}
\renewcommand{\cftchappagefont}{\normalfont}
\renewcommand\cftchapleader{\cftdotfill{1}} 
\cftsetindents{part}{5.5em}{0em} \cftsetindents{chapter}{1em}{2em}
\renewcommand{\cftchapdotsep}{\cftdotsep}

%...............................................................................
% Add code for the first page of the table of contents; in the argument for
% computing the number of the absolute page where code shall be carried out use 
% \getabspage{toc-start} for obtaining the value of the absolute page-number of
% the first page of the table of contents:
%...............................................................................
\CodeForBackgroundOfAbsolutePage{%
  % The zref-label "toc-start" must exist for the expression for computing the
  % number of the absolute page where code shall be carried out to work out, so
  % it is the only element of the comma-list of zref-labels that must exist for
  % the cod to be carried out.:
  toc-start
}{%
  % Expression for computing the number of the absolute page where code shall 
  % be carried out.
  \getabspage{toc-start}%
}{% Code to carry out inside the background-picture-environment of the 1st page
  % of the table of contents.
  % Be aware that the picture's origin (0,0) is the top left corner of the page
  % and that the first argument of `\put` denotes how far to go to the right and
  % _upwards_ in relation to the origin.
  % So upwards-direction needs to be specified _negative_ as otherwise things
  % end up above the topmargin of the page.
  \put(0, 0) {%
    \vtop to \paperheight{% \paperheight is how far the \vtop goes downwards
                          % from the baseline of the topmost horizontal box/item 
                          % inside the \vtop. The baseline of the topmost 
                          % horizontal box/item inside the \vtop is also the
                          % baseline of the \vtop-box itself.
                          % The baseline of the \vtop-box is vertically aligned
                          % with the coordinate (0,0). 
      \hsize=\paperwidth % \hsize is the width of lines in case TeX switches to
                         % horizontal mode for doing line breaking
                         % automatically.
                         % In so-called *restricted* horizontal mode, e.g., 
                         % when carrying out a \hbox-command, TeX does not do
                         % line-breaking.
      %.........................................................................
      % Use a \hrule of no height and no depth as topmost horizontal item of the
      % \vtop to make sure the \vtop's height is 0 - the \hrule will be the
      % baseline of the \vtop-box. The baseline of the \vtop-box is vertically
      % aligned with the coordinate (0,0). 
      % That coordinate denotes the top left corner of the page :
      %.........................................................................
      \hrule height 0pt width 0pt
      %.........................................................................
      \vfill
      \hbox to \paperwidth{%
        \hfill
        \hbox{%
          \includegraphics[width=.5\paperwidth]{example-image-a}%
        }%
        \hfill\null
      }%
      \vfill
    }%
  }%
}%
%...............................................................................
% Add code for the third page of the table of contents:
%...............................................................................
\CodeForBackgroundOfAbsolutePage{%
  % The zref-label "toc-start" must exist for the expression for computing the
  % number of the absolute page where code shall be carried out to work out, so
  % it is the only element of the comma-list of zref-labels that must exist for
  % the cod to be carried out.:
  toc-start
}{%
  % Expression for computing the number of the absolute page where code shall 
  % be carried out.
  \getabspage{toc-start}+2%
}{% Code to carry out inside the background-picture-environment of the 3rd page
  % of the table of contents.
  % Be aware that the picture's origin (0,0) is the top left corner of the page
  % and that the first argument of `\put` denotes how far to go to the right and
  % _upwards_ in relation to the origin.
  % So upwards-direction needs to be specified _negative_ as otherwise things
  % end up above the topmargin of the page.
  \put(0,-\paperheight) {%
    \vbox to \paperheight{% \paperheight is how far the \vbox goes upwards from
                          % the baseline of the last horizontal box/item at the
                          % bottom inside the \vbox. The baseline of the last
                          % horizontal box/item inside the \vbox at the bottom
                          % of the \vbox is also the baseline of the \vbox
                          % itself.
                          % The baseline of the \vbox-box is vertically aligned
                          % with the coordinate (0,-\paperheight). 
      \hsize=\paperwidth % \hsize is the width of lines in case TeX switches to
                         % horizontal mode for doing line breaking
                         % automatically.
                         % In so-called *restricted* horizontal mode, e.g., 
                         % when carrying out a \hbox-command, TeX does not do
                         % line-breaking.
      \vfill
      \hbox to \paperwidth{%
        \hfill
        \hbox{%
          \includegraphics[width=.5\paperwidth, height=.5\paperwidth, origin=c, 
                           angle=45]{example-image-b}%
        }%
        \hfill\null
      }%
      \vfill
      %.........................................................................
      % Use a \hrule of no height and no depth as the last horizontal item at
      % the bottom the \vbox to make sure the \vbox's depth is 0 - that \hrule
      % will be the baseline of the \vbox. The baseline of the \vbox is
      % vertically aligned with the coordinate (0,-\textheight). 
      % That coordinate denotes the bottom left corner of the page :
      %.........................................................................
      \hrule height 0pt width 0pt
    }%
  }%
}%
%...............................................................................

\begin{document}

\frontmatter

\pagestyle{empty}
%%%%%%%\setcounter{page}{0}

\begin{titlepage}
  \centering
  \fontsize{20ex}{20ex}\selectfont\bfseries\textcolor{Black} {TITLE PAGE}\par
\end{titlepage}

\clearpage

\baselineskip=12.5pt % <- Better use a package for this kind of things which 
                     %    takes subtleties into account.

\mainmatter
\pagestyle{plain}

% In a loop create sections so that the table of contents gets enough pages:

\def\scratch{0}%
\loop\ifnum\scratch<168 %
  \edef\scratch{\the\numexpr\scratch+1\relax}%
  \section{section \scratch} text
\repeat

\newpage
\pagenumbering{gobble}   
\tableofcontents

\end{document}

enter image description here

2
  • Your solution gets the job done, despite the complexity of the code, which I'm unlikely to be able to decipher and modify in the future, in case the need arises. The previous answer also solves my problem, albeit in a much simpler way. Is it possible to accept both answers as helpful?
    – tryingout
    Commented Nov 30, 2022 at 14:44
  • @tryingout "Accepting" more than one answer is not possible. Just accept the answer which is most helpful to you. I decided for a zref-based approach so in any case you can move the \tableofcontents-command around in your document as you like. Commented Nov 30, 2022 at 14:58
2

Using \pagestyle{gobble} is not a problem here, because it still increases counter page, only \thepage will result in always the same empty string:

% !TEX TS-program = LuaLaTeX
\documentclass[11pt,twoside,openany]{book}
\pagestyle{plain}

\usepackage[english, russian]{babel}
\usepackage{fontspec}
\setmainfont{EB Garamond}[]

\usepackage{microtype}
\usepackage[shortcuts]{extdash} 
\usepackage[pagewise]{lineno}
\linenumbers
\usepackage[
  paperwidth=6.125in,
  paperheight=9.25in,
  inner=0.625in,
  top=15mm,
  outer=20mm,
  bottom=24mm, 
  heightrounded 
]{geometry} 

\setlength{\parskip}{0ex}% Not the best idea, better use package parskip.
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}

\usepackage{eso-pic}

\usepackage{fancyhdr}
\fancyhf{} 
\renewcommand{\headrulewidth}{0ex} 
\fancyfoot[LE,RO]{\thepage} 
\pagestyle{fancy}
\fancypagestyle{plain}{%
  \fancyhf{}%
  \renewcommand{\headrulewidth}{0ex}%
  \fancyhf[lef,rof]{\thepage}%
}

\usepackage{titlesec}
\titleclass{\part}{top}
\titleformat{\part}{\vfil\penalty500\vfilneg\centering\normalfont\LARGE}{\thepart.}{8ex}{\LARGE\centering}
\titlespacing*{\part}{0ex}{0ex plus 4ex minus 1ex}{-4ex} 
\titleclass{\chapter}{straight}
\titleformat{\chapter}{\vfil\penalty500\vfilneg\centering\normalfont\large}{\thechapter.}{8ex}{\large\centering}
\titlespacing*{\chapter} {0ex}{4ex plus 4ex minus 1ex}{-2ex} 

\usepackage{tocloft}
\addto\captionsrussian{%
  \renewcommand{\contentsname}{\normalfont\Large\textcolor{Black}{TOC}}%
}
\addtocontents{toc}{\cftpagenumbersoff{part}} 
\setlength{\cftbeforetoctitleskip}{0ex}
\setlength{\cftaftertoctitleskip}{17ex}
\renewcommand{\cfttoctitlefont}{\hfill\Large\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}
\renewcommand{\cftpartfont}{\large}
\renewcommand{\cftchapfont}{\normalsize}
\renewcommand{\cftpartpagefont}{\normalfont}
\renewcommand{\cftchappagefont}{\normalfont}
\renewcommand\cftchapleader{\cftdotfill{1}} 
\cftsetindents{part}{5.5em}{0em} \cftsetindents{chapter}{1em}{2em}
\renewcommand{\cftchapdotsep}{\cftdotsep}

\begin{document}

\frontmatter
\pagestyle{empty}
\setcounter{page}{0}

 
\begin{titlepage}
  \centering
  \fontsize{20ex}{20ex}\selectfont\bfseries\textcolor{Black} {TITLE PAGE}\par
\end{titlepage}
\clearpage

\baselineskip=12.5pt
\mainmatter

\pagestyle{plain}
\part[PART 1.]{\textcolor{Black}{PART 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 

\part[PART 2.]{\textcolor{Black}{PART 2}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\clearpage
 \AddToShipoutPicture{%
  \AtTextLowerLeft{%
   \includegraphics[page=\value{page},width=\textwidth,height=\textheight,keepaspectratio]{example-image-a4-numbered}%
  }
}

\pagenumbering{gobble}   
  \tableofcontents
\clearpage
\ClearShipoutPictureBG% If more pages without images follow, it makes sense, to remove the background shipout code.

  \end{document}

example with page selection via option page

You can alternatively do something like \ifnum \value{page}=…. Following example uses it, to have different files at different positions depending on the page number:

% !TEX TS-program = LuaLaTeX
\documentclass[11pt,twoside,openany]{book}
\pagestyle{plain}

\usepackage[english, russian]{babel}
\usepackage{fontspec}
\setmainfont{EB Garamond}[]

\usepackage{microtype}
\usepackage[shortcuts]{extdash} 
\usepackage[pagewise]{lineno}
\linenumbers
\usepackage[
  paperwidth=6.125in,
  paperheight=9.25in,
  inner=0.625in,
  top=15mm,
  outer=20mm,
  bottom=24mm, 
  heightrounded 
]{geometry} 

\setlength{\parskip}{0ex}% Not the best idea, better use package parskip.
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}

\usepackage{eso-pic}

\usepackage{fancyhdr}
\fancyhf{} 
\renewcommand{\headrulewidth}{0ex} 
\fancyfoot[LE,RO]{\thepage} 
\pagestyle{fancy}
\fancypagestyle{plain}{%
  \fancyhf{}%
  \renewcommand{\headrulewidth}{0ex}%
  \fancyhf[lef,rof]{\thepage}%
}

\usepackage{titlesec}
\titleclass{\part}{top}
\titleformat{\part}{\vfil\penalty500\vfilneg\centering\normalfont\LARGE}{\thepart.}{8ex}{\LARGE\centering}
\titlespacing*{\part}{0ex}{0ex plus 4ex minus 1ex}{-4ex} 
\titleclass{\chapter}{straight}
\titleformat{\chapter}{\vfil\penalty500\vfilneg\centering\normalfont\large}{\thechapter.}{8ex}{\large\centering}
\titlespacing*{\chapter} {0ex}{4ex plus 4ex minus 1ex}{-2ex} 

\usepackage{tocloft}
\addto\captionsrussian{%
  \renewcommand{\contentsname}{\normalfont\Large\textcolor{Black}{TOC}}%
}
\addtocontents{toc}{\cftpagenumbersoff{part}} 
\setlength{\cftbeforetoctitleskip}{0ex}
\setlength{\cftaftertoctitleskip}{17ex}
\renewcommand{\cfttoctitlefont}{\hfill\Large\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}
\renewcommand{\cftpartfont}{\large}
\renewcommand{\cftchapfont}{\normalsize}
\renewcommand{\cftpartpagefont}{\normalfont}
\renewcommand{\cftchappagefont}{\normalfont}
\renewcommand\cftchapleader{\cftdotfill{1}} 
\cftsetindents{part}{5.5em}{0em} \cftsetindents{chapter}{1em}{2em}
\renewcommand{\cftchapdotsep}{\cftdotsep}

\begin{document}

\frontmatter
\pagestyle{empty}
\setcounter{page}{0}

 
\begin{titlepage}
  \centering
  \fontsize{20ex}{20ex}\selectfont\bfseries\textcolor{Black} {TITLE PAGE}\par
\end{titlepage}
\clearpage

\baselineskip=12.5pt
\mainmatter

\pagestyle{plain}
\part[PART 1.]{\textcolor{Black}{PART 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 

\part[PART 2.]{\textcolor{Black}{PART 2}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\chapter[CHAPTER 1.]{\textcolor{Black}{CHAPTER 1}} 
\clearpage
\AddToShipoutPicture{%
  \ifnum \value{page}=1  
    \AtTextLowerLeft{%
      \includegraphics[page=1,width=.5\textwidth,height=.5\textheight,keepaspectratio]{example-image-a}%
    }%
  \fi
  \ifnum \value{page}=2  
    \AtTextCenter{%
      \includegraphics[page=1,width=.5\textwidth,height=.5\textheight,keepaspectratio]{example-image-b}%
    }%
  \fi
  \ifnum \value{page}=3  
    \AtTextUpperLeft{%
      \makebox(0,0)[lt]{\includegraphics[page=1,width=.5\textwidth,height=.5\textheight,keepaspectratio]{example-image-c}}%
    }%
  \fi
}

\pagenumbering{gobble}   
\tableofcontents
\clearpage
\ClearShipoutPictureBG% If more pages without images follow, it makes sense, to remove the background shipout code.

\end{document}

example with different files on different page location

Similar you can load files with names depending on the page number, e.g., using example-image-\alph{page} (but do not use \thepage!) or similar. You can test this, replacing the three filenames example-image-a, example-image-b and example-image-c in the previous example by example-image-\alph{page}.

Note: If you have \setcounter{page}{0} on the current page, then this page has \value{page}=0 not \value{page}=1.

An absolutely trivial suggestion would be, to write corresponding \AddToShipoutPicture* commands to the toc file. For this, you just have to look into the table of contents, see which chapter or section or subsection is on which page of the table of contents, and add \addtocontents{toc}{…} code at the corresponding position in the document, e.g.:

\documentclass[11pt,openany]{book}
\pagestyle{plain}
\usepackage{eso-pic}
\usepackage{mwe}

\begin{document}

\addtocontents{toc}{% inside the TOC
  \protect\AddToShipoutPicture*{% add it only to the next current page
    \protect\put(5cm,8cm){%
      \protect\includegraphics[page=1,width=.5\textwidth,height=.5\textheight,keepaspectratio]{example-image-a}%
    }%
  }%
}%    
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\addtocontents{toc}{% inside the TOC
  \protect\AddToShipoutPicture*{% add it only to the next current page
    \protect\put(5cm,12cm){%
      \protect\includegraphics[page=1,width=.5\textwidth,height=.5\textheight,keepaspectratio,angle=45]{example-image-b}%
    }%
  }%
}%    
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\addtocontents{toc}{% inside the TOC
  \protect\AddToShipoutPicture*{% add it only to the next current page
    \protect\put(8cm,24cm){%
      \protect\includegraphics[page=1,width=.5\textwidth,height=.5\textheight,keepaspectratio,angle=180]{example-image-c}%
    }%
  }%
}%    
\blinddocument

\pagenumbering{gobble}   
\tableofcontents

\end{document}

to get the following table of contents:

result of the most trivial suggestion

However, this makes sense only, when the table of contents is final. If you add or remove chapters, sections or subsections, you may have to move the \addtocontents commands, to add the background images to the correct page.

As you can see, there are a lot of possibilities to either add more or less the same code to load different image into the background of different pages or to add individual code to some pages. And gobbling the page number is not a disqualifier, because the page counter is still there and still increased at every page.

5
  • Just read the eso-pic package manual and I can't seem to find the answer about how you managed to achieve the result. How does one \AddToShipoutPicture code take care of all three pictures? How does page=\value{page} code refer to a specific page in TOC? As soon as I change example-image-a4-numbered to the real file name or even example-image, I get a fatal error. Tried replacing page=\value{page} with \ifnum \value{page}=… - didn't work. And I don't know how to load images with just example-image-\alph{page}. Should I not use page=\value{page} in this case?
    – tryingout
    Commented Nov 27, 2022 at 16:11
  • @tryingout \value{page} is the page counter of the main document and is increased by every page output. Option page selects a page page from an external file. So if the current page number of your document is, e.g,, 10 the \includegraphics[page=\value{page},…]{foo} will include page 10 of file file. Starting with document page number 1 in my example, page=1, page=2 and page=3 are used and so these pages from example-image-a4-numbered are included. There is absolutely nothing magic in this. If you get an fatal error, maybe your external file has not the corresponding page.
    – cabohah
    Commented Nov 29, 2022 at 11:15
  • The problem is that I need to have control over position of every single background image I place in TOC (assuming that some pages might not include background images at all). Say, first image on the first page of the TOC might be in completely different location and of different opacity than the second image on the second page of the TOC. That's why I don't get the idea how to control those images. How does example-image-a4-numbered places 3 different images on every page of the TOC? Please provide more details in your answer code or direct me to the right document to read.
    – tryingout
    Commented Nov 29, 2022 at 12:39
  • @tryingout No problem at all. See my second example. It is really not a big mystery.
    – cabohah
    Commented Nov 29, 2022 at 14:31
  • Thank you for your time and effort to solve my problem. Your answer does the job. Though mostly you perfected the code I've previously tried. And I really appreciate that. The second answer, on the contrary, does not just modify my code, but offers a completely new approach to solving the problem and is described in detail, every line of the code. Assuming that this community will greatly benefit from expanding knowledge and solutions not previously offered on this site, I choose to accept that answer. In turn, as a thank you, I also note the usefulness of your answer by voting for it.
    – tryingout
    Commented Nov 30, 2022 at 15:29

You must log in to answer this question.

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