0

I currently try to set my page number via:

\fancyhf{}%
\fancyfoot[RO]{\begin{picture}(0,0)\put(1.5cm,-0.9cm){\includegraphics[width = 1cm]{page-number}}}\put(1.5cm,-0.9cm){\makebox[0pt]{\pagenumberfont\Large\thepage}}\end{picture}}%
\fancyfoot[LE]{\begin{picture}(0,0)\put(-1.5cm,-0.9cm){\includegraphics[width = 1cm]{page-number}}}\put(-1.5cm,-0.9cm){\makebox[0pt]{\pagenumberfont\Large\thepage}}\end{picture}}%

But I get the error:

! You can't use `\raise' in vertical mode.
\put  ...itsset \@tempdimc {#2}\unitlength \raise 
                                                  \@tempdimc \hb@xt@ \z@ {\@...

l.41 ...makebox[0pt]{\pagenumberfont\Large\thepage}}
                                                  \end{picture}}%
? 

How do I either fix my code or use another way to get the a background right behind the page number that's centered the same way as the page number?

Minimal example:

\documentclass{book}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage[paperwidth=6.125in,paperheight=9.25in,inner=2.0625cm,outer=3.2985cm,top=2.325cm,bottom=3.6846000000000005cm]{geometry}%
%
\usepackage{graphicx}%
\usepackage{fontspec}%
\usepackage{tocloft}%
\usepackage{changepage}%
\usepackage{ebgaramond}%
\newfontfamily{\pagenumberfont}{Pacifico}%
\setmainfont{EB Garamond}%

\usepackage[pdfusetitle,bookmarks=true,colorlinks=true,linkcolor=black,urlcolor=black,citecolor=black]{hyperref}%
\usepackage{fancyhdr}%
\usepackage{etoolbox}%
\pagestyle{fancy}%
\AddToHook{shipout/background}{\put(0pt, -\paperheight) {\ifodd\value{page}\includegraphics[width =\paperwidth, height =\paperheight]{../right-\thechapter}\else\includegraphics[width =\paperwidth, height =\paperheight]{../raw_images/PoeticHypnosisI/left-\thechapter}\fi}}%%
%
\begin{document}%
\normalsize%
\patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%
\fancyhf{}%
\fancyfoot[RO]{\begin{picture}(0,0)\put(1.5cm,-0.9cm){\includegraphics[width = 1cm]{page-number}}}\put(1.5cm,-0.9cm){\makebox[0pt]{\pagenumberfont\Large\thepage}}\end{picture}}%
\fancyfoot[LE]{\begin{picture}(0,0)\put(-1.5cm,-0.9cm){\includegraphics[width = 1cm]{page-number}}}\put(-1.5cm,-0.9cm){\makebox[0pt]{\pagenumberfont\Large\thepage}}\end{picture}}%
\renewcommand{\headrulewidth}{0.0pt}%

\newpage%
\begingroup%
\begin{adjustwidth}{0.75cm}{0.4cm}%
\AddToHookNext{shipout/background}{\put(0pt, -\paperheight) {\includegraphics[width =\paperwidth, height =\paperheight]{../tableofcontents}}}%
\lineskiplimit=-\maxdimen%
\tableofcontents%
\end{adjustwidth}%
\thispagestyle{fancy}%
\endgroup%
\setmainfont{EB Garamond}%
\chapter*{Introduction}%
\addcontentsline{toc}{chapter}{Introduction}%
\vfill%
\paragraph{}%

%
Super Text
\paragraph{}%

%
Super Text

%
\end{document}
4
  • 3
    Please edit your post to provide a complete compilable document. That's much more useful than code snippets.
    – cfr
    Commented Sep 27, 2023 at 12:01
  • @cfr : I tried to cut down to a minimal example and added it.
    – Christian
    Commented Sep 29, 2023 at 13:58
  • 1
    Why are you loading conflicting font packages? If you want to use T1 and lmodern, don't use fontspec. If you want to use fontspec, ditch fontenc, lmodern, textcomp and inputenc. When creating MWEs, try not to depend on things some people don't have unless they're essential to the problem e.g. Pacifico and specific images.
    – cfr
    Commented Sep 30, 2023 at 14:29
  • 1
    your example can't be used for a test. You are loading a local font and local images. Replace them by example images from the mwe package. Commented Sep 30, 2023 at 15:07

1 Answer 1

3
+50

Your example can't be used as it is. I've replaced your images with standard example images. You're also loading conflicting fonts and font packages. Since I don't have the font you're using for page numbers anyway, I've commented out the fontspec stuff and gone with the fontenc/inputenc/lmodern/textcomp stuff. If you want to use fontspec for that font, remove the incompatible packages and uncomment the relevant lines.

Original

The simplest way I know to achieve the effect you want is to put things in boxes and then use them in the footer setup. Here, I've used 'coffins' from xcoffins because they are easy to join and align. This makes it straightforward to put the page number on top of the image and does away with the need to use picture and fiddle with horizontal/vertical adjustments manually.

output with sample images

\documentclass{book}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage[paperwidth=6.125in,paperheight=9.25in,inner=2.0625cm,outer=3.2985cm,top=2.325cm,bottom=3.6846000000000005cm]{geometry}%
%
\usepackage[]{graphicx}%
\usepackage{xcoffins}
% \usepackage{fontspec}% if you want to use this, remove fontenc, inputenc, lmodern, textcomp
\usepackage{tocloft}%
\usepackage{changepage}%
\usepackage{ebgaramond}%
% \newfontfamily{\pagenumberfont}{Pacifico}%
% \setmainfont{EB Garamond}%
\let\pagenumberfont\sffamily
\usepackage{fancyhdr}%
\usepackage{etoolbox}%
\usepackage[pdfusetitle,bookmarks=true,colorlinks=true,linkcolor=black,urlcolor=black,citecolor=black]{hyperref}% load last unless specific package requires later loading, consider bookmarks
\pagestyle{fancy}%
\AddToHook{shipout/background}{\put(0pt, -\paperheight) {\ifodd\value{page}\includegraphics[width =\paperwidth, height =\paperheight]{example-image-a}\else\includegraphics[width =\paperwidth, height =\paperheight]{example-image-b}\fi}}

\NewCoffin \PagenumberCoffin
\NewCoffin \PagenumberbackCoffin

\NewDocumentCommand \MakePagenumberCoffin { O {r} }
{%
  \SetHorizontalCoffin \PagenumberCoffin {\pagenumberfont\Large\thepage}%
  \SetHorizontalCoffin \PagenumberbackCoffin {\includegraphics[width=1cm]{example-image}}%
  \JoinCoffins \PagenumberbackCoffin [hc,vc] \PagenumberCoffin [hc,vc]%
  \TypesetCoffin \PagenumberbackCoffin [B,#1]%
}

\patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%
\fancyhf{}%
\fancyfoot[RO]{\MakePagenumberCoffin}
\fancyfoot[LE]{\MakePagenumberCoffin[l]}%
\renewcommand{\headrulewidth}{0.0pt}%
\begin{document}
\begingroup
  \begin{adjustwidth}{0.75cm}{0.4cm}%
    \AddToHookNext{shipout/background}{\put(0pt, -\paperheight) {\includegraphics[width =\paperwidth, height =\paperheight]{example-image-c}}}%
    \lineskiplimit=-\maxdimen
    \tableofcontents
  \end{adjustwidth}%
  \thispagestyle{fancy}%
\endgroup
% \setmainfont{EB Garamond}%
\chapter*{Introduction}
\addcontentsline{toc}{chapter}{Introduction}%
\vfill

\paragraph{This command is not the way to make paragraphs}%

Super Text
\paragraph{A subsubsubheading} rather than a paragraph.

This is a new paragraph.

Super Text is in another new paragraph.

\end{document}

Edit 1 (Non-experimental)

If you prefer to avoid the experimental xcoffins, here's a non-experimental version.

\documentclass{book}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage[paperwidth=6.125in,paperheight=9.25in,inner=2.0625cm,outer=3.2985cm,top=2.325cm,bottom=3.6846000000000005cm]{geometry}%
%
\usepackage[]{graphicx}%
% \usepackage{fontspec}% if you want to use this, remove fontenc, inputenc, lmodern, textcomp
\usepackage{tocloft}%
\usepackage{changepage}%
\usepackage{ebgaramond}%
% \newfontfamily{\pagenumberfont}{Pacifico}%
% \setmainfont{EB Garamond}%
\let\pagenumberfont\sffamily
\usepackage{fancyhdr}%
\usepackage{etoolbox}%
\usepackage[pdfusetitle,bookmarks=true,colorlinks=true,linkcolor=black,urlcolor=black,citecolor=black]{hyperref}% load last unless specific package requires later loading, consider bookmarks
\pagestyle{fancy}%
\AddToHook{shipout/background}{\put(0pt, -\paperheight) {\ifodd\value{page}\includegraphics[width =\paperwidth, height =\paperheight]{example-image-a}\else\includegraphics[width =\paperwidth, height =\paperheight]{example-image-b}\fi}}

\ExplSyntaxOn
\coffin_new:N \l_christian_pageno_coffin
\coffin_new:N \g_christian_pagenoback_coffin
\coffin_new:N \l_christian_pagenocombo_coffin

\AtBeginDocument
{
  \hcoffin_gset:Nn \g_christian_pagenoback_coffin { \includegraphics[width=1cm]{example-image} }
}

\NewDocumentCommand \MakePagenumberCoffin { O {r} }
{%
  \group_begin:
    \hcoffin_set:Nn \l_christian_pageno_coffin  {\pagenumberfont\Large\thepage}
    \coffin_set_eq:NN \l_christian_pagenocombo_coffin \g_christian_pagenoback_coffin
    \coffin_join:NnnNnnnn \l_christian_pagenocombo_coffin { hc } { vc } \l_christian_pageno_coffin { hc } { vc } { 0pt } { 0pt }
    \coffin_typeset:Nnnnn \l_christian_pagenocombo_coffin { B } { #1 } { 0pt } { 0pt }
  \group_end:
}
\ExplSyntaxOff
\patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%
\fancyhf{}%
\fancyfoot[RO]{\MakePagenumberCoffin}
\fancyfoot[LE]{\MakePagenumberCoffin[l]}%
\renewcommand{\headrulewidth}{0.0pt}%
\begin{document}
\begingroup
  \begin{adjustwidth}{0.75cm}{0.4cm}%
    \AddToHookNext{shipout/background}{\put(0pt, -\paperheight) {\includegraphics[width =\paperwidth, height =\paperheight]{example-image-c}}}%
    \lineskiplimit=-\maxdimen
    \tableofcontents
  \end{adjustwidth}%
  \thispagestyle{fancy}%
\endgroup
% \setmainfont{EB Garamond}%
\chapter*{Introduction}
\addcontentsline{toc}{chapter}{Introduction}%
\vfill

\paragraph{This command is not the way to make paragraphs}%

Super Text
\paragraph{A subsubsubheading} rather than a paragraph.

This is a new paragraph.

Super Text is in another new paragraph.

\end{document}

This has the advantage of typesetting the image into the background coffin only once. After that the result is reused each time. The high-level commands in xcoffins don't let you just copy one coffin to another, but the commands in the programming layer are more full-featured.

Edit 2 (With shift)

We can modify this to shift the page numbers by adding a basic key-value interface. Three keys are provided

  • side should be l or r (default: r)
  • xshift should be a dimension (default: 1.5cm)
  • yshift should be a dimension (default: -0.9cm)

Then the following will shift the left page numbers left and down and the right page numbers down and right.

\documentclass{book}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage[paperwidth=6.125in,paperheight=9.25in,inner=2.0625cm,outer=3.2985cm,top=2.325cm,bottom=3.6846000000000005cm]{geometry}%
%
\usepackage[]{graphicx}%
% \usepackage{fontspec}% if you want to use this, remove fontenc, inputenc, lmodern, textcomp
\usepackage{tocloft}%
\usepackage{changepage}%
\usepackage{ebgaramond}%
% \newfontfamily{\pagenumberfont}{Pacifico}%
% \setmainfont{EB Garamond}%
\let\pagenumberfont\sffamily
\usepackage{fancyhdr}%
\usepackage{etoolbox}%
\usepackage[pdfusetitle,bookmarks=true,colorlinks=true,linkcolor=black,urlcolor=black,citecolor=black]{hyperref}% load last unless specific package requires later loading, consider bookmarks
\pagestyle{fancy}%
\AddToHook{shipout/background}{\put(0pt, -\paperheight) {\ifodd\value{page}\includegraphics[width =\paperwidth, height =\paperheight]{example-image-a}\else\includegraphics[width =\paperwidth, height =\paperheight]{example-image-b}\fi}}

\ExplSyntaxOn
\coffin_new:N \l_christian_pageno_coffin
\coffin_new:N \g_christian_pagenoback_coffin
\coffin_new:N \l_christian_pagenocombo_coffin

\AtBeginDocument
{
  \hcoffin_gset:Nn \g_christian_pagenoback_coffin { \includegraphics[width=1cm]{example-image} }
}

\keys_define:nn { christian / page~no }
{
  side .tl_set:N = \l_christian_side_tl,
  side .initial:n = r,
  xshift .dim_set:N = \l_christian_xshift_dim,
  xshift .initial:n = 1.5cm,
  yshift .dim_set:N = \l_christian_yshift_dim,
  yshift .initial:n = -0.9cm,
}
\cs_generate_variant:Nn \coffin_typeset:Nnnnn { NnVVV }
\NewDocumentCommand \MakePagenumberCoffin { O {side=r,xshift=1.5cm} }
{%
  \group_begin:
    \keys_set:nn { christian / page~no } { #1 }
    \hcoffin_set:Nn \l_christian_pageno_coffin  {\pagenumberfont\Large\thepage}
    \coffin_set_eq:NN \l_christian_pagenocombo_coffin \g_christian_pagenoback_coffin
    \coffin_join:NnnNnnnn \l_christian_pagenocombo_coffin { hc } { vc } \l_christian_pageno_coffin { hc } { vc } { 0pt } { 0pt }
    \coffin_typeset:NnVVV \l_christian_pagenocombo_coffin { B } \l_christian_side_tl \l_christian_xshift_dim \l_christian_yshift_dim
  \group_end:
}
\ExplSyntaxOff
\patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%
\fancyhf{}%
\fancyfoot[RO]{\MakePagenumberCoffin}
\fancyfoot[LE]{\MakePagenumberCoffin[side=l,xshift=-1.5cm]}%
\renewcommand{\headrulewidth}{0.0pt}%
\begin{document}
\begingroup
  \begin{adjustwidth}{0.75cm}{0.4cm}%
    \AddToHookNext{shipout/background}{\put(0pt, -\paperheight) {\includegraphics[width =\paperwidth, height =\paperheight]{example-image-c}}}%
    \lineskiplimit=-\maxdimen
    \tableofcontents
  \end{adjustwidth}%
  \thispagestyle{fancy}%
\endgroup
% \setmainfont{EB Garamond}%
\chapter*{Introduction}
\addcontentsline{toc}{chapter}{Introduction}%
\vfill

\paragraph{This command is not the way to make paragraphs}%

Super Text
\paragraph{A subsubsubheading} rather than a paragraph.

This is a new paragraph.

Super Text is in another new paragraph.

\end{document}

shifted page numbers

Edit 3

If you just want the page numbers offset, then all the fiddling about with shifting things is needless. Instead, you can simply redefine \headwidth as explained in fancyhdr's documentation. This is straightforward. So all you need really is the way to box the page number on its background.

standard case of off-setting covered in fancyhdr docs combined with coffins to 'box' (and a bit) the page number combination

Note that the lines are just from show frame for demonstration purposes. Remove from the commented geometry line for actual use.

\documentclass{book}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage[show frame,paperwidth=6.125in,paperheight=9.25in,inner=2.0625cm,outer=3.2985cm,top=2.325cm,bottom=3.6846000000000005cm]{geometry}% remove "show frame" when happy with layout
%
\usepackage[]{graphicx}%
\usepackage{tocloft}%
\usepackage{ebgaramond}%
\usepackage{changepage}
\let\pagenumberfont\sffamily
\usepackage{fancyhdr}%
\usepackage{etoolbox}%
\usepackage[pdfusetitle,bookmarks=true,colorlinks=true,linkcolor=black,urlcolor=black,citecolor=black]{hyperref}% load last unless specific package requires later loading, consider bookmarks
\pagestyle{fancy}%
\AddToHook{shipout/background}{\put(0pt, -\paperheight) {\ifodd\value{page}\includegraphics[width =\paperwidth, height =\paperheight]{example-image-a}\else\includegraphics[width =\paperwidth, height =\paperheight]{example-image-b}\fi}}

\ExplSyntaxOn
\coffin_new:N \l_christian_pageno_coffin
\coffin_new:N \g_christian_pagenoback_coffin
\coffin_new:N \l_christian_pagenocombo_coffin

\AtBeginDocument
{
  \hcoffin_gset:Nn \g_christian_pagenoback_coffin { \includegraphics[width=1cm]{example-image} }
}

\keys_define:nn { christian / page~no }
{
  side .tl_set:N = \l_christian_side_tl,
  side .initial:n = r,
  yshift .dim_set:N = \l_christian_yshift_dim,
  yshift .initial:n = -0.9cm,
}
\cs_generate_variant:Nn \coffin_typeset:Nnnnn { NnVnV }
\NewDocumentCommand \MakePagenumberCoffin { O {side=r,xshift=1.5cm} }
{% if textblock has constant width, we could do this more efficiently by setting some of this only once for each of the two even/odd pages, but we don't want to assume that
  \group_begin:
    \keys_set:nn { christian / page~no } { #1 }
    \hcoffin_set:Nn \l_christian_pageno_coffin  {\pagenumberfont\Large\thepage}
    \coffin_set_eq:NN \l_christian_pagenocombo_coffin \g_christian_pagenoback_coffin
    \coffin_join:NnnNnnnn \l_christian_pagenocombo_coffin { hc } { vc } \l_christian_pageno_coffin { hc } { vc } { 0pt } { 0pt }
    \coffin_typeset:NnVnV \l_christian_pagenocombo_coffin { B } \l_christian_side_tl { 0pt } \l_christian_yshift_dim
  \group_end:
}
\ExplSyntaxOff
\patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%
\fancyhf{}%
\fancyfoot[RO]{\MakePagenumberCoffin[side=r]}
\fancyfoot[LE]{\MakePagenumberCoffin[side=l]}
\addtolength \headwidth {1.5cm}
\renewcommand{\headrulewidth}{0.0pt}%
\begin{document}
\begingroup
  \begin{adjustwidth}{0.75cm}{0.4cm}%
    \tableofcontents
  \end{adjustwidth}%
  \thispagestyle{fancy}%
\endgroup
\chapter*{Introduction}
\addcontentsline{toc}{chapter}{Introduction}%
\vfill

\paragraph{This command is not the way to make paragraphs}%

Super Text
\paragraph{A subsubsubheading} rather than a paragraph.

This is a new paragraph.

Super Text is in another new paragraph.

\end{document}
10
  • Package xcoffins does not seem to be directly available. Instead, part of l3experimental ?
    – user287367
    Commented Sep 30, 2023 at 19:04
  • @rallg It's distributed as part of TeX Live, so I'm not sure why it wouldn't be 'directly available'.
    – cfr
    Commented Sep 30, 2023 at 19:27
  • 1
    @rallg See edit. I've added a non-experimental version.
    – cfr
    Commented Sep 30, 2023 at 20:14
  • Thanks, it does manage to include the picture the right way. However, the positioning I had of "1.5cm,-0.9cm" is not included and it's not clear how to integrate it into the code given that it is "1.5cm,-0.9cm" on the right and "-1.5cm,-0.9cm" on the left.
    – Christian
    Commented Sep 30, 2023 at 22:11
  • @Christian That is easy to adjust. Where is it 1.5/0.9 cm from exactly? (Note, I don't mean 'that is easy to adjust if this stuff is new to you', but it is easy to adjust if you've done this before.)
    – cfr
    Commented Sep 30, 2023 at 23:35

You must log in to answer this question.

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