# Blog

## Archive

Show me a random blog post**2019**

**2018**

**2017**

**2016**

**2015**

**2014**

**2013**

**2012**

## Tags

pac-man books accuracy reuleaux polygons javascript electromagnetic field nine men's morris wool approximation twitter triangles coins sorting pythagoras football braiding dataset london underground plastic ratio mathslogicbot probability tennis rugby dragon curves christmas golden ratio chess fractals draughts national lottery folding tube maps data curvature statistics raspberry pi christmas card frobel oeis python flexagons speed binary rhombicuboctahedron people maths propositional calculus harriss spiral craft go the aperiodical polynomials european cup stickers trigonometry folding paper geometry arithmetic weather station puzzles sound matt parker light manchester science festival php bubble bobble captain scarlet royal baby a gamut of games aperiodical big internet math-off latex news countdown graph theory chebyshev london programming asteroids mathsteroids game of life inline code gerry anderson sport dates golden spiral map projections misleading statistics platonic solids bodmas games ternary palindromes chalkdust magazine logic manchester radio 4 estimation error bars realhats interpolation hats game show probability hexapawn world cup cross stitch martin gardner reddit machine learning pizza cutting final fantasy video games menace noughts and crosses**2019-03-26**

## realhats: Writing a LaTeΧ package

I originally wrote this post for The Aperiodical.

A few months ago, Adam Townsend went to lunch and had a conversation. I wasn't there, but I imagine the conversation went something like this:

Adam: Hello.Smitha: Hello.Adam: How are you?Smitha: Not bad. I've had a funny idea, actually.Adam: Yes?Smitha: You know how the \hat command in LaTeΧ puts a caret above a letter?... Well I was thinking it would be funny if someone made a package that made the \hat command put a picture of an actual hat on the symbol instead?Adam: (After a few hours of laughter.) I'll see what my flatmate is up to this weekend...Jeff: What on Earth are you two talking about?!

As anyone who has been anywhere near maths at a university in the last ∞ years will be able to tell you,
LaTeΧ is a piece of maths typesetting software. It's a bit like a version of Word that runs in terminal and makes PDFs with really
pretty equations.

By default, LaTeΧ can't do very much, but features can easily added by importing packages: importing the graphicsx
package allows you to put images in your PDF; importing geometry allows you to easily change the page margins; and importing
realhats makes the \hat command put real hats above symbols.

### Changing the behaviour of \hat

By default, the LaTeΧ command \hat puts a pointy "hat" above a symbol:

After Adam's conversation, we had a go at redefining the \hat command by putting the following
at the top of our LaTeΧ file.

**LaTeΧ**

% We put our new definition here

}

After a fair amount of fiddling with the code, we eventually got it to produce the following result:

We were now ready to put our code into a package so others could use it.

### How to write a package

A LaTeΧ package is made up of:

- a sty file, containing a collection of commands like the one we wrote above;
- a PDF of documentation showing users how to use your package;
- a README file with a basic description of your package.

It's quite common to make the first two of these by making a
dtx file
and an ins file. And no, we have
no idea either why these are the file extensions used or why this is supposedly simpler than making a sty file and a PDF.

The ins file says which bits of the dtx should be used to make up the sty file.
Our ins file looks like this:

**LaTeΧ**

\keepsilent

\usedir{tex/latex/realhats}

\preamble

*License goes here*

\endpreamble

\askforoverwritefalse

\generate{

\file{realhats.sty}{\from{realhats.dtx}{realhats}}

}

\endbatchfile

The most important command in this file is \generate: this says that that the file
realhats.sty should be made from the file realhats.dtx
taking all the lines that are marked as part of realhats. The following is part of our dtx file:

**LaTeΧ**

%command put real hats on symbols.

%For example, the input \lstinline@\hat{a}=\hat{b}@ will produce the output:

%\[\hat{a}=\hat{b}\]

%To make a vector with a hat, the input \lstinline@\hat{\mathbf{a}}@ produces:

%\[\hat{\mathbf{a}}\]

%

%\iffalse

%<*documentation>

\documentclass{article}

\usepackage{realhats}

\usepackage{doc}

\usepackage{listings}

\title{realhats}

\author{Matthew W.~Scroggs \& Adam K.~Townsend}

\begin{document}

\maketitle

\DocInput{realhats.dtx}

\end{document}

%</documentation>

%\fi

%\iffalse

%<*realhats>

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{realhats}[2019/02/02 realhats]

\RequirePackage{amsmath}

\RequirePackage{graphicx}

\RequirePackage{ifthen}

\renewcommand{\hat}[1]{

% We put our new definition here

}

%</realhats>

%\fi

The lines near the end between <*realhats>
and </realhats> will be included in the sty file, as they are marked at part of
realhats.

The rest of this file will make the PDF documentation when the dtx file is compiled.
The command \DocInput tells LaTeΧ to include the dtx again, but with the
%s that make lines into comments removed. In this way all the comments that describe the functionality will end up
in the PDF. The lines that define the package will not be included in the PDF as they are between \iffalse and
\fi.

Writing both the commands and the documentation in the same file like this means that the resulting file is quite a mess, and really quite
ugly. But this is apparently the standard way of writing LaTeΧ packages, so rest assured that it's not just our code that ugly and
confusing.

### What to do with your package

Once you've written a package, you'll want to get it out there for other people to use. After all, what's the point of being able to
put real hats on top of symbols if the whole world can't do the same?

First, we put the source code of our package on GitHub, so that Adam and I had an
easy way to both work on the same code. This also allows other LaTeΧ lovers to see the source and contribute to it, although none have
chosen to add anything yet.

Next, we submitted our package to CTAN, the Comprehensive TeΧ Archive Network.
CTAN is an archive of thousands of LaTeΧ packages, and putting realhats there gives LaTeΧ users
everywhere easy access to real hats. Within days of being added to CTAN, realhats was added (with no work by us)
to MikTeX and TeX Live
to allow anyone using these LaTeΧ distributions to seemlessly install it as soon as it is needed.

We figured that the packaged needed a website too, so we made one. We also figured that the website
should look as horrid as possible.

### How to use realhats

So if you want to end fake hats and put real hats on top of your symbols, you can simply write \usepackage{realhats}
at the top of your LaTeΧ file.

### Similar posts

Runge's Phenomenon | Mathsteroids | How OEISbot works | Logic bot |

### Comments

Comments in green were written by me. Comments in blue were not written by me.

**2018-09-13**

## Runge's Phenomenon

This is a post I wrote for round 2 of The Aperiodical's Big Internet Math-Off 2018. As I went out in round 1 of the Big Math-Off, you got to read about the real projective plane instead of this.

Polynomials are very nice functions: they're easy to integrate and differentiate, it's quick to calculate their value at points, and they're generally friendly to deal with. Because of this, it can often be useful to find a polynomial that closely approximates a more complicated function.

Imagine a function defined for \(x\) between -1 and 1. Pick \(n-1\) points that lie on the function. There is a unique degree \(n\) polynomial (a polynomial whose highest power of \(x\) is \(x^n\)) that passes through these points. This polynomial is called an

*interpolating polynomial*, and it sounds like it ought to be a pretty good approximation of the function.So let's try taking points on a function at equally spaces values of \(x\), and try to approximate the function:

$$f(x)=\frac1{1+25x^2}$$
I'm sure you'll agree that these approximations are pretty terrible, and they get worse as more points are added. The high error towards 1 and -1 is called Runge's phenomenon, and was discovered in 1901 by Carl David Tolmé Runge.

All hope of finding a good polynomial approximation is not lost, however: by choosing the points more carefully, it's possible to avoid Runge's phenomenon. Chebyshev points (named after Pafnuty Chebyshev) are defined by taking the \(x\) co-ordinate of equally spaced points on a circle.

The following GIF shows interpolating polynomials of the same function as before using Chebyshev points.

Nice, we've found a polynomial that closely approximates the function... But I guess you're now wondering how well the Chebyshev interpolation will approximate other functions. To find out, let's try it out on the votes over time of my first round Big Internet Math-Off match.

The graphs below show the results of the match over time interpolated using 16 uniform points (left) and 16 Chebyshev points (right). You can see that the uniform interpolation is all over the place, but the Chebyshev interpolation is very close the the actual results.

But maybe you still want to see how good Chebyshev interpolation is for a function of your choice... To help you find out, I've written @RungeBot, a Twitter bot that can compare interpolations with equispaced and Chebyshev points. Just tweet it a function, and it'll show you how bad Runge's phenomenon is for that function, and how much better Chebysheb points are.

For example, if you were to tweet @RungeBot f(x)=abs(x), then RungeBot would reply: Here's your function interpolated using 17 equally spaced points (blue) and 17 Chebyshev points (red). For your function, Runge's phenomenon is terrible.

A list of constants and functions that RungeBot understands can be found here.

### Similar posts

Mathsteroids | realhats | Harriss and other spirals | Christmas (2018) is over |

### Comments

Comments in green were written by me. Comments in blue were not written by me.

**Add a Comment**

**2018-07-06**

## Mathsteroids

This is a post I wrote for round 1 of The Aperiodical's Big Internet Math-Off 2018, where Mathsteroids lost to MENACE.

A map projection is a way of representing the surface of a sphere, such as the Earth, on a flat surface. There is no way to represent all the features of a sphere on a flat surface, so if you want a map that shows a certain feature of the world, then you map will have to lose some other feature of the world in return.

To show you what different map projections do to a sphere, I have created a version of the game

*Asteroids*on a sphere. I call it*Mathsteroids*. You can play it here, or follow the links below to play on specific projections.### Mercator projection

The most commonly used map projection is the Mercator projection, invented by Gerardus Mercator in 1569. The Mercator projection preserves angles: if two straight lines meet at an angle \(\theta\) on a sphere, then they will also meet at an angle \(\theta\) on the map. Keeping the angles the same, however, will cause the straight lines to no longer appear straight on the map, and the size of the same object in two different place to be very different.

The angle preserving property means that lines on a constant bearing (eg 030° from North) will appear straight on the map. These constant bearing lines are not actually straight lines on the sphere, but when your ship is already being buffeted about by the wind, the waves, and the whims of drunken sailors, a reasonably straight line is the best you can expect.

The picture below shows what three ships travelling in straight lines on the sphere look like on a Mercator projection.

To fully experience the Mercator projection, you can play

*Mathsteroids*in Mercator projection mode here. Try flying North to see your spaceship become huge and distorted.### Gall–Peters projection

The Gall–Peters projection (named after James Gall and Arno Peters) is an area-preserving projection: objects on the map will have the same area as objects on the sphere, although the shape of the object on the projection can be very different to its shape on the sphere.

The picture below shows what three spaceships travelling in straight lines on a sphere look like on the Gall–Peters projection.

You can play

*Mathsteroids*in Gall–Peters projection mode here. I find this one much harder to play than the Mercator projection, as the direction you're travelling changes in a strange way as you move.### Azimuthal projection

An azimuthal projection makes a map on which the directions from the centre point to other points on the map are the same as the directions on the sphere. A map like this could be useful if, for example, you're a radio operator and need to quickly see which direction you should point your aerial to communicate with other points on the map.

The azimuthal projection I've used in

*Mathsteroids*also preserves distances: the distance from the centre to the another points on the map is proportional to the actual distance on the sphere. This projection is used as the emblem of the UN.The picture below shows three straight lines on this projection. You can play

*Mathsteroids*in azimuthal mode here.A retroazimuthal projection makes a map on which the directions to the centre point from other points on the map are the same as the directions on the sphere. If you're thinking that this is the same as the azimuthal projection, then you're too used to doing geometry on flat surfaces: on a sphere, the sum of the angles in a triangle depends on the size of the triangle, so the directions from A to B and from B to A aren't as closely related as you would expect.

The Craig retroazimuthal projection was invented by James Ireland Craig in 1909. He used Mecca as his centre point to make a map that shows muslims across the world which direction they should face to pray.

The picture below shows what three spaceships travelling in a straight lines on a sphere looks like on this projection.

You can play

*Mathsteroids*in Craig retroazimuthal mode here to explore the projection yourself. This is perhaps the hardest of all to play, as (a) two different parts of the sphere overlap on the map, and (b) the map is actually infinitely tall, so quite a bit of it is off the edge of the visible game area.### Stereographic projection

The final projection I'd like to show you is the stereographic projection.

Imagine that a sphere is sitting on a 2D plane. Take a point on the sphere. Imagine a straight line through this point and the point at the top of the sphere. The point where this line meets the 2D plane is stereographic projection of the point on the sphere.

This projection (backwards) can be used to represent the every complex number as a point on a sphere: this is called the Riemann sphere.

To make

*Mathseteroids*playable after this projection, I split the sphere into 2 hemisphere and projected each seperately to give two circles. You can play*Mathsteroids*in stereographic projection mode here. Three spaceships travelling in straight lines on this projection are shown below.... and if you still don't like map projections, you can still enjoy playing ~~head over to The Aperiodical and vote~~ (voting now closed).

*Mathsteroids*on an old fashioned torus. Or on a Klein bottle or the real projective plane. Don't forget to take a short break from playing to### Similar posts

Video game surfaces | Runge's Phenomenon | Optimal Pac-Man | Harriss and other spirals |

### Comments

Comments in green were written by me. Comments in blue were not written by me.

**Add a Comment**

**
**

**© Matthew Scroggs 2019**

Add a Comment