mscroggs.co.uk
mscroggs.co.uk

subscribe

Blog

 2019-03-26 
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:
a (left) and \hat{a} (right)
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Χ 
\renewcommand{\hat}[1]{
    % We put our new definition here
}

After a fair amount of fiddling with the code, we eventually got it to produce the following result:
a (left) and \hat{a} (right) while using the realhats package
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:
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Χ 
\input{docstrip.tex}
\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Χ 
%\lstinline{realhats} is a package for \LaTeX{} that makes the \lstinline{\hat}
%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.
realhats: gotta put them all in academic papers
                        
(Click on one of these icons to react to this blog post)

You might also enjoy...

Comments

Comments in green were written by me. Comments in blue were not written by me.
I am a pensioner studying maths with OU. Currently doing M248 stats module. My enjoyment of MLEs has been magnified by your wonderful realhats package. It's a good job I'm 99% tee-total or my tutor would be getting a dubious assignment (still leaves a 1% chance of malt-driven mischief though).
Dave
×1      ×1           Reply
 Add a Comment 


I will only use your email address to reply to your comment (if a reply is needed).

Allowed HTML tags: <br> <a> <small> <b> <i> <s> <sup> <sub> <u> <spoiler> <ul> <ol> <li> <logo>
To prove you are not a spam bot, please type "q" then "u" then "o" then "t" then "i" then "e" then "n" then "t" in the box below (case sensitive):

Archive

Show me a random blog post
 2024 

Feb 2024

Zines, pt. 2

Jan 2024

Christmas (2023) is over
 2023 
▼ show ▼
 2022 
▼ show ▼
 2021 
▼ show ▼
 2020 
▼ show ▼
 2019 
▼ show ▼
 2018 
▼ show ▼
 2017 
▼ show ▼
 2016 
▼ show ▼
 2015 
▼ show ▼
 2014 
▼ show ▼
 2013 
▼ show ▼
 2012 
▼ show ▼

Tags

graphs preconditioning manchester science festival talking maths in public cambridge matrix multiplication newcastle crochet raspberry pi arithmetic folding paper logs countdown folding tube maps matt parker approximation nine men's morris radio 4 frobel ucl zines football london signorini conditions go platonic solids pi approximation day live stream light weak imposition london underground finite group matrices exponential growth christmas harriss spiral royal institution correlation sport python dragon curves gather town pythagoras estimation palindromes anscombe's quartet recursion ternary probability inverse matrices reddit news a gamut of games latex php stirling numbers standard deviation sorting computational complexity royal baby the aperiodical books guest posts convergence fence posts speed sobolev spaces advent calendar bempp game of life matrix of minors hexapawn trigonometry martin gardner logic christmas card javascript plastic ratio pascal's triangle phd big internet math-off hyperbolic surfaces hats databet boundary element methods edinburgh dates mathslogicbot numbers numerical analysis pi mathsjam wave scattering dinosaurs weather station data visualisation electromagnetic field golden ratio rhombicuboctahedron map projections tmip world cup manchester video games games asteroids accuracy oeis wool national lottery coins geogebra gaussian elimination polynomials draughts data machine learning stickers menace determinants statistics braiding errors dataset chebyshev graph theory curvature finite element method error bars binary logo pizza cutting reuleaux polygons people maths captain scarlet fractals quadrilaterals puzzles gerry anderson noughts and crosses mathsteroids inline code rugby pac-man programming interpolation runge's phenomenon squares chalkdust magazine cross stitch propositional calculus european cup matrix of cofactors crossnumber realhats simultaneous equations hannah fry sound datasaurus dozen bodmas bubble bobble game show probability tennis youtube golden spiral fonts final fantasy craft flexagons turtles chess 24 hour maths misleading statistics mean geometry triangles

Archive

Show me a random blog post
▼ show ▼
© Matthew Scroggs 2012–2024