2

I have the following setup which generate lined-paper background in a4paper (modified from texblog and answer from this site)

I would like to make this code more papersize independent.

The value "28" on for-loop works for this papersize (A4), but for other papersize or orientation (Landscape/Potrait), it need to be adjusted.

I'm guessing I can put \paperheight somewhere in the for-loop, but I don't know how to convert that value (which is in inch or cm) into (coordinate-unit) after \draw.

Or maybe doing arithmetic with page.north west and page.south west, if that even possible.

Yes I know, I could just modified them manually (changing value 28 to 38 in for-loop when working in a3paper), but I'm interested on more automatic solution.

In short: I have those many \usepackage{geometry} commented in code, and would want to make a single \mygraphpaper definition that works on them all.

Layout document that I want to achieve: No left/right margin, the same x margin from top and bottom

\documentclass{article} 
\usepackage{tikz}
%\usepackage[landscape,a3paper]{geometry}
\usepackage[a4paper]{geometry}
%\usepackage[landscape,a5paper]{geometry}
%\usepackage[a5paper]{geometry}
%\usepackage[landscape,paperheight=8.17in,paperwidth=6.32in,top=1in,bottom=1in,right=1in,left=1in]{geometry}
\usetikzlibrary{calc}
\def\mygraphpaper{%
  \begin{tikzpicture}
  \foreach \i in {4,5,...,28}{
  \draw[gray] ($(current page.north west)+(0,-\i)$) -- ($(current page.north east)+(0,-\i)$);}
  \end{tikzpicture}%
}
\usepackage{background}
\backgroundsetup{
  angle=0,
  contents=\mygraphpaper,
  color=black,
  scale=1,
}

\begin{document}
\pagenumbering{gobble}
$\,$
\end{document}
3
  • You can just use length with units in a coordinate. You don't have to use the XYZ coordinate system (and you can change that, too). So do you want a horizontal line every 1cm but not for the whole page but only for the text area? Commented Dec 21, 2022 at 8:43
  • hi, how would the for loop syntax works on that? I want horizontal lines every 1cm starting from the most left of the page to the most right, but give blank 'x' cm from top and x cm from bottom.
    – Ajat
    Commented Dec 21, 2022 at 8:52
  • I think most of my problem would be, involving the arithmetic using pre-defined length in Tex. Like I wonder if I can just do something like replacing 28 on the loop to \i*\paperheight-\j, or use while-loop , etc.
    – Ajat
    Commented Dec 21, 2022 at 8:59

2 Answers 2

3

For two parameters where #1 stands for the distance between the first horizontal line to the top of the page and #2 for the minimum distance to the bottom you can use define \mygraphpaper as such

\newcommand*\mygraphpaper[2]{%
  \begin{tikzpicture}[remember picture,overlay]
  \foreach[parse=true] \i in {0,...,(\paperheight-(#1)-(#2))/1cm}{
  \draw[gray] ([yshift={-\i cm-(#1)}]current page.north west) -- ++(right:\paperwidth);}
  \end{tikzpicture}%
}

Note that I've used rememer picture and overlay so that the special current page node gets recognized correctly and we don't have to deal with whereever background decides to place this content.

Note however, that the everypage package that is used by everypage is deprecated.


With a LaTeX version of the past two years, you can (should) use the shipout/background hook which acts as a picture that has its origin at the top left corner of the page. Since you're only drawing horizontal lines, you can just draw it that way without having to use PGF/TikZ

The same math as before gets us the needed number of iterations for \multiput.

Code (TikZ + background/everypage)

\documentclass{article} 
\usepackage{tikz}
%\usepackage[landscape,a3paper]{geometry}
\usepackage[a4paper]{geometry}
%\usepackage[landscape,a5paper]{geometry}
%\usepackage[a5paper]{geometry}
%\usepackage[landscape,paperheight=8.17in,paperwidth=6.32in,top=1in,bottom=1in,right=1in,left=1in]{geometry}
\usetikzlibrary{calc}
\newcommand*\mygraphpaper[2]{%
  \begin{tikzpicture}[remember picture,overlay]
  \foreach[parse=true] \i in {0,...,(\paperheight-(#1)-(#2))/1cm}{
  \draw[gray] ([yshift={-\i cm-(#1)}]current page.north west) -- ++(right:\paperwidth);}
  \end{tikzpicture}%
}
\usepackage{background}
\backgroundsetup{
  angle=0,
  contents=\mygraphpaper{3.5cm}{5cm},
  color=black,
  scale=1,
}

\begin{document}
\pagenumbering{gobble}
$\,$
\end{document}

Code (shipout/background + picture)

\documentclass{article}
%\usepackage[landscape,a3paper]{geometry}
\usepackage[a4paper]{geometry}
%\usepackage[landscape,a5paper]{geometry}
%\usepackage[a5paper]{geometry}
%\usepackage[landscape,paperheight=8.17in,paperwidth=6.32in,top=1in,bottom=1in,right=1in,left=1in]{geometry}
\usepackage{xcolor}
\newcommand*\mygraphpaper[2]{%
  \color{gray}
  \setlength\unitlength{1cm}
  \linethickness{.4pt}
  \multiput(0,-#1)(0,-1){\fpeval{1+trunc((\paperheight-#1cm-#2cm)/1cm,0)}}{%
    \line(1,0){\paperwidth}
  }
}
\AddToHook{shipout/background}{\mygraphpaper{3.5}{5}}
\begin{document}
\pagenumbering{gobble}
$\,$
\end{document}
5
  • A combination of TikZ (or better PGF) and the shipout/background is possible, of course, but I haven't put much thought into it since picture is all that is needed for horizontal lines. Commented Dec 21, 2022 at 10:23
  • The first code works without 'remember picture,overlay' , with them the lines are not properly located (I'm not sure why). I think I get how the first code works. Thanks!
    – Ajat
    Commented Dec 21, 2022 at 10:23
  • The first code works every twice compile, it's possibly needed the aux files
    – Ajat
    Commented Dec 21, 2022 at 10:25
  • Yes, remember picture uses the aux file. If you don't use it, the lines are offset to the bottom (one inch because that's how that hook worked. You probably ran the second code with the aux file from the first code which doesn't recognize the PGF/TikZ macros in the aux file anymore. One could implement a TikZ solution without remember picture (since we know where it will be placed). Commented Dec 21, 2022 at 10:34
  • @Ajat Since background uses TikZ in the background I worry about interference and since it uses deprecated packages I wouldn't pursue a solution with it anyway. If you insist on a TikZ solution, I could take a closer look into using TikZ with shipout/background. Commented Dec 21, 2022 at 10:35
3

You could use the grid operation from TikZ (the negative (or zero) value for xstep will remove the vertical lines, so you just get your horizontal rules):

\documentclass{article} 
\usepackage{tikz}
%\usepackage[landscape,a3paper]{geometry}
\usepackage[a4paper]{geometry}
%\usepackage[landscape,a5paper]{geometry}
%\usepackage[a5paper]{geometry}
%\usepackage[landscape,paperheight=8.17in,paperwidth=6.32in,top=1in,bottom=1in,right=1in,left=1in]{geometry}
\usetikzlibrary{calc}

\def\mygraphpaper{%
  \begin{tikzpicture}[remember picture]
    \draw[ystep=1cm, xstep=-1cm, gray] ([yshift=-2cm]current page.north west) grid ([yshift=2cm]current page.south east);
  \end{tikzpicture}%
}
\usepackage{background}
\backgroundsetup{
  angle=0,
  contents=\mygraphpaper,
  color=black,
  scale=1,
}

\begin{document}
\pagenumbering{gobble}
$\,$
\end{document}

enter image description here

4
  • 1
    I always forget about the built-in loop of grid … By the way, a step value of 0(pt) already triggers the non-drawing. (It checks for >0.01pt.) Commented Dec 21, 2022 at 11:02
  • @Qrrbrbirlbel updated, thanks for your comment! Commented Dec 21, 2022 at 11:04
  • thank you for the neat answer! this fit nicely since I also want to make a squared graphpaper.
    – Ajat
    Commented Dec 21, 2022 at 14:39
  • @Ajat You're welcome! Commented Dec 21, 2022 at 14:40

You must log in to answer this question.

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