[Tex/LaTex] How to add a page number to the right corner of the page

header-footermargins

I used to use \pagestyle{fancy} to make my page number be placed at the right bottom corner, but there is a problem that it only can be placed at the inside of the page margin, I want to move it to the very bottom of the page.
Let me show you:

enter image description here

Best Answer

If you can use package scrlayer-scrpage instead fancyhdr you can define new layers and add them to the predefined page styles scrheadings and plain.

\documentclass[
%  twoside
]{article}
\usepackage{xcolor}

\usepackage{scrlayer-scrpage}
\clearpairofpagestyles
\DeclareNewLayer[
  foreground,
  oddpage,
  align=br,
  hoffset=\paperwidth,
  voffset=\paperheight,
  width=7em,
  height=2\baselineskip,
  contents={{%
  \setlength{\fboxsep}{0pt}%
  \colorbox{red!50!black}{\parbox[c][\layerheight][c]{\layerwidth}{\centering\pagemark}}%
  }}
]{pagenumberouterbottom.odd}
\DeclareNewLayer[
  clone=pagenumberouterbottom.odd,
  evenpage,
  align=bl,
  hoffset=0pt
]{pagenumberouterbottom.even}
\addtokomafont{pagenumber}{\color{white}\bfseries}
\AddLayersToPageStyle{scrheadings}{pagenumberouterbottom.odd,pagenumberouterbottom.even}
\AddLayersToPageStyle{plain}{pagenumberouterbottom.odd,pagenumberouterbottom.even}

\usepackage{blindtext}% only for dummy text
\begin{document}
\blinddocument
\end{document}

enter image description here

Note that in the code is also a layer for even pages. So if the document is twosided the page number will be on the outer margin.

With twoside:

enter image description here

Related Question