TransWikia.com

Make tikz rectangle fill whole paperwidth - including margin

TeX - LaTeX Asked by Dr.Kameleon on April 2, 2021

I’m drawing a rectangle that is supposed to cover from the leftmost to the rightmost edge of each page with some centered text in it.

Here is the relevant code:

begin{tikzpicture}
     draw[fill=darkgray] (0,0) rectangle (paperwidth,3cm) node[midway] 
     {color{lightgray}{huge{Animals}}};
end{tikzpicture}

However the result is not what I expect:

enter image description here

How do I solve this?

3 Answers

enter image description here

This should help you

MWE

documentclass{article}
pagestyle{empty} % Suppress page numbers
usepackage{tikz}
begin{document}
begin{tikzpicture}[font=sffamily,remember picture,overlay]
path (current page.north west) node[below right,fill=darkgray,minimum 
width=paperwidth,minimum height=3cm] (box){};

path (box.west) node[right=5mm,align=left] %<distance can be changed to suit
{{fontsize{45pt}{65pt}color{white}textbf{JS Bibra}}[2mm]
{fontsize{30pt}{20pt}color{cyan}Advisor to TEX}[2mm]
{fontsize{10pt}{10pt}color{white}LaTeX development}};

end{tikzpicture}
end{document}

EDIT

For centered text change the 5mm to any other distance from the left edge of the page for example 10cm

path (box.west) node[right=10cm,align=left] 

This would give the following text alignment

enter image description here

EDIT2

The difference lies in the addition of the option

[font=sffamily,remember picture,overlay]

at the beginning of the tikzpicture

After adding the above option to the code of OP the box falls in place

enter image description here

MWE of the OP after amending option at the begining

documentclass{article}
pagestyle{empty} % Suppress page numbers
usepackage{tikz}
begin{document}
begin{tikzpicture}
[font=sffamily,remember picture,overlay]
draw (current page.north west) node[below right,fill=darkgray,minimum 
width=paperwidth,minimum height=3cm] (box){color{green}{dxczcd}};
end{tikzpicture}
end{document}

Correct answer by js bibra on April 2, 2021

try noindent before begin{tikzpicture}

Answered by jlettvin on April 2, 2021

tcolorbox could be another option:

documentclass{article}
usepackage[most]{tcolorbox}
usepackage{lipsum}

begin{document}
lipsum[1]
begin{tcolorbox}[enhanced, spread sidewards, halign=center, valign=center, sharp corners, colframe=darkgray, colback=darkgray, colupper=lightgray, fontupper=hugesffamily, height=3cm ]
Animals
end{tcolorbox}
lipsum[2]
end{document}

enter image description here

Answered by Ignasi on April 2, 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