TransWikia.com

Exact size of makebox

TeX - LaTeX Asked on August 5, 2021

I am trying to create a page layout, where the page number is shown on the right margin in a white textcolor on a black background.

documentclass[parskip=full]{scrreprt}
usepackage[left=2cm,right=2cm]{geometry}
usepackage[headwidth=paper]{scrlayer-scrpage}
usepackage{xcolor}

automark[chapter]{chapter}

setkomafont{pagenumber}{normalfonttextcolor{white}}

chead{}
ohead{leftmarkquadcolorbox{black}{makebox[2cm][l]{pagemark}}}

usepackage{lipsum}

begin{document}

chapter{First Chapter}
colorbox{black}{makebox[2cm]{textcolor{white}{1-2-3}}}
lipsum[1-11]

end{document}

That works already — at least almost. However, I have two questions.

  1. The size of the makebox field does not seem to be exact. The left border of the black background seems to be a little bit more to the left than the right text border. When loading the generated PDF in Affinity Photo, the size of both makeboxes is about 2.2 cm instead of 2.0 cm.

  2. Is it possible to move the command for the black background into the setkomafont{pagenumber} expression or to a renewcommand*{pagemark}... command? This would facilitate defining different headers using the same black box page number.

2 Answers

You could use makebox[dimexpr2cm-2fboxseprelax]{...} to adjust the width of the box:

documentclass[parskip=full]{scrreprt}
usepackage[left=2cm,right=2cm,
  %showframe% show the page layout
]{geometry}
usepackage[headwidth=paper]{scrlayer-scrpage}
usepackage{xcolor}

automark[chapter]{chapter}

addtokomafont{pagenumber}{color{white}}
clearpairofpagestyles
ohead[pagemark]{leftmarkquadpagemark}

renewcommand*pagemark{%
  colorbox{black}{%
    makebox[dimexpr2cm-2fboxseprelax][l]{%
      usekomafont{pagenumber}{thepage}%
}}}

usepackage{lipsum}

begin{document}
chapter{First Chapter}
colorbox{black}{makebox[dimexpr2cm-2fboxseprelax]{textcolor{white}{1-2-3}}}
lipsum[1-11]
end{document}

Result with geometry option showframe:

enter image description here

enter image description here

Or you could use makebox[0pt][l]{...} to move the page number box in the margin:

documentclass[parskip=full]{scrreprt}
usepackage[left=2cm,right=2cm,
  %showframe% show the page layout
]{geometry}
usepackage{scrlayer-scrpage}% <- changed
usepackage{xcolor}

automark[chapter]{chapter}

addtokomafont{pagenumber}{color{white}}
clearpairofpagestyles
ohead[{makebox[0pt][l]{pagemark}}]{leftmarkquadmakebox[0pt][l]{pagemark}}

renewcommand*pagemark{%
  colorbox{black}{%
    makebox[2cm][l]{%
      usekomafont{pagenumber}{thepage}%
}}}

usepackage{lipsum}

begin{document}
chapter{First Chapter}
colorbox{black}{makebox[dimexpr2cm-2fboxseprelax]{textcolor{white}{1-2-3}}}
lipsum[1-11]
end{document}

Or you could declare a new page style layer and add it to the layer page styles:

documentclass[parskip=full]{scrreprt}
usepackage[left=2cm,right=2cm,
  showframe% show the page layout
]{geometry}
usepackage{scrlayer-scrpage}% <- changed
usepackage{xcolor}

automark[chapter]{chapter}

addtokomafont{pagenumber}{color{white}}
clearpairofpagestyles
ohead{leftmarkquad}

DeclareNewLayer[
  background,
  head,
  addhoffset=textwidth,
  oddpage,
  contents=colorbox{black}{makebox[5cm][l]{pagemark}}
]{pagenumber}
AddLayersToPageStyle{scrheadings}{pagenumber}
AddLayersToPageStyle{plain.scrheadings}{pagenumber}

usepackage{lipsum}
begin{document}
chapter{First Chapter}
colorbox{black}{makebox[dimexpr2cm-2fboxsep]{textcolor{white}{1-2-3}}}
lipsum[1-11]
end{document}

Correct answer by esdd on August 5, 2021

When setting the colorbox padding to 0pt, LaTeX also removes the padding above and below the text. I think that the best or at least the easiest way is eyeballing the padding and correct it accordingly.

I also found a solution to my 2nd question.

This is the revised script:

documentclass[parskip=full]{scrreprt}
usepackage[left=2cm,right=2cm]{geometry}
usepackage[headwidth=paper]{scrlayer-scrpage}
usepackage{xcolor}

automark[chapter]{chapter}

renewcommand*{pagemark}{colorbox{black}{makebox[1.85cm][l]{normalfonttextcolor{white}thepage}}}

chead{}
ohead{leftmarkquadpagemark}
cfoot[]{}

usepackage{lipsum}

begin{document}

chapter{First Chapter}
lipsum[1-11]

end{document}

The white page number on the black background can now be easily reused with the simple pagemark argument.

I also added cfoot[]{} to remove the previously invisible page number (because it was white) from the footer.

Answered by Chris on August 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP