Making dotted graph paper in Julia with Luxor.jl

By: Tamás K. Papp

Re-posted from: https://tamaspapp.eu/post/dotted-paper-luxor/

I needed some graph paper with subdivisions at centimeters and millimetres. There are some generators online, but I found their output too heavy, visually cluttered, or simply not ideal (yes, I am very picky about these things).

The obvious solution was to generate a PDF or SVG using a program. Before learning Julia, I would have just started in pgf/tikz, and spent hours searching online to do the thing I want. pgf/tikz are fine tools, but I generally try to avoid programming anything nontrivial in LaTeX, so I always have to spend a lot of time debugging my code.

However, being a Julia user, this was a great opportunity to try Luxor.jl, the “Cairo for tourists!”. This means that I don’t have to immerse myself in the intricacies of its syntax and concepts; I can just dip my toes and get the results.

I was not disappointed: within 15 minutes of Pkg.add("Luxor"), I had arrived at a solution I like. I am very satisfied with Luxor.jl and will probably use it in the future when I need graphics built up from primitives.

download as dotted_paper.jl

using Luxor

#######################
# customize script here
#######################
paper = "A4"                       # paper size
major_unit = 1cm                   # gridlines at each
subdivisions = 10                  # dots on gridlines in between
radius = 0.1mm                     # radius for the tiny dots
margins = (5mm, 5mm)               # minimum margins (may be more, centered)
color = "black"                    # color for the tiny dots
filename = "dotted_paper.pdf"      # output goes here, OVERWRITTEN

#######################################################
# runtime code - you probably don't need to change this
#######################################################
pagesize = paper_sizes[paper]
nx, ny = @. floor(Int, (pagesize - (2 * margins)) / major_unit)
offsetx, offsety = @. margins + (pagesize % major_unit) / 2

major_grid_x = offsetx .+ major_unit * (0:nx)
major_grid_y = offsety .+ major_unit * (0:ny)
minor_grid_x = offsetx .+ (major_unit / subdivisions) * (0:(nx*subdivisions))
minor_grid_y = offsety .+ (major_unit / subdivisions) * (0:(ny*subdivisions))

Drawing(paper, filename)
background("white")
sethue(color)
@. circle(Point(major_grid_x, minor_grid_y'), radius, :fill);
@. circle(Point(minor_grid_x, major_grid_y'), radius, :fill);
finish()
preview()

The final PDF is here.