1

I want to add a background to the final page of the document. I have two images, one that I have added on the first page of the document works perfectly, but I am not sure how to add the second one.

\documentclass{book}
\usepackage{graphicx}
\usepackage[pages=some]{background}


\backgroundsetup{   %for first-page image
scale=1,
color=black,
opacity=0.5,
angle=0,
contents={%
\includegraphics[width=\paperwidth,height=\paperheight]{example-image-a}
  }%
}  

 \title{TITLE}
 \author{AUTHOR}
 \date{DATE}
 \begin{document}
 \BgThispage  % first-page image

 \maketitle 

 \tableofcontents
 \include{chapter}

  %Here I want to add second-page image.

 \end{document}
1
  • 1
    You can use TikZ [remember picture, ovverlay] at the begining of specific pages. You can use the afterpage package on the previous page. You can use the lastpage package and \AddToHook{shiput/background}{...} to check for the last page. Dec 20, 2022 at 23:09

2 Answers 2

1

You could use the tikz package that allows you to write the following:

\begin{tikzpicture}[overlay,remember picture]
\node at (current page.center) {\includegraphics[width=\paperwidth,height=\paperheight]{example-image-a}};
\end{tikzpicture}

Explanation

Inside the environment tikzpicture is defined a node at the centre of the current page. This node will display anything between its {...}. It just happens that I told it to \includegraphics[width=\paperwidth,height=\paperheight]{example-image-a}.

Now for this to work correctly, you need to tell tikz that this tikzpicture is an overlay and that It will be superposed to whatever is below it on the paper.

The remember picture is used to tell tikz that this tikzpicture goes into an .aux file and will be compiled with other elements. For simplicity, let's say that this allows the tikzpicture to know where the centre of the page is. Try removing this option and you will have the tikzpicture move to the top of the page.

refs:

1
  • There woulld be a problem putting this on the top of the same page as \chapter due to \clearpage (or \cleardoublepage). Dec 21, 2022 at 17:23
3

To demonstrate my comment about lastpage:

\documentclass{book}
\usepackage{graphicx}
\usepackage{transparent}
\usepackage{lastpage}
\usepackage{refcount}

\AddToHook{shipout/background}{%
  \ifnum\value{page}=1\relax
     \put (0pt,-\paperheight) {\transparent{0.5}\includegraphics[width=\paperwidth,height=\paperheight]{example-image-a}}
  \fi
  \ifnum\value{page}=\getpagerefnumber{LastPage}\relax
     \put (0pt,-\paperheight) {\transparent{0.5}\includegraphics[width=\paperwidth,height=\paperheight]{example-image-b}}
  \fi
}

 \title{TITLE}
 \author{AUTHOR}
 \date{DATE}

 \begin{document}
 
 \maketitle 

 \tableofcontents

\chapter{First}

  \pageref{LastPage}

 \end{document}
5
  • This works very well, thanks. But can the last page be completely devoid of headers and page numbers? I used \thispagestyle{empty} just before \pageref{LastPage} but it only removed the header and not the page number. Dec 21, 2022 at 11:34
  • It worked for me. \chapter uses \thispagestyle{plain}, but that shouldn\t stop another \thispagestyle. Dec 21, 2022 at 17:17
  • LaTeX also provides \thetotalpages and \PreviousTotalPages which would make it independent of the page counter (which can be 1 more than once) and a manual \pageref at the last page. → \ifnum\thetotalpages=1 and \ifnum\thetotalpages=\PreviousTotalPages Dec 22, 2022 at 10:32
  • @Qrrbrbirlbel - I take it this is a new feature? Dec 22, 2022 at 16:46
  • @JohnKormylo I just stumbled across it in source2e when answering another question with shipout/background since it is in the same section. Dec 22, 2022 at 19:47

You must log in to answer this question.

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