# You're reading: Posts Tagged: LaTeX

### realhats: Writing a $\LaTeX$ Package

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:

Smitha: Hello.
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Χ (or $\LaTeX$) 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.

\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:

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;

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:

\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:

%\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.

### LaTeX/TikZ to draw a star graph $K_{1,n}$

For a diagram for a class this week, I’ve written a LaTeX command to draw star graphs using TikZ. A star graph $K_{1,n}$ is a graph with a single central node, $n$ radial nodes, and $n$ edges connecting the central node to each radial node. I am sharing this here in case it is useful to anyone else.

### LaTeX for typesetting a multi-pile Nim game

I am preparing to teach our new final year module ‘Game Theory and Recreational Mathematics’. So I’m thinking about game typesetting in LaTeX (texlive-games is useful in this regard). I was looking for an easy way to display multi-pile Nim games. Usually, I find searching “latex thing” finds numerous options for typesetting “thing” in LaTeX, but here I was struggling.

Nim objects could be anything, of course, but conventionally sticks or stones are used. There are various types of dot in LaTeX that might look like stones, but somehow a line of dots didn’t seem satisfactory. There are various ways to draw a line (not least simply typing ‘|’), including some tally markers (e.g. in hhcount). My problem with these (call me picky) is that they are all identical lines, and a ‘heap’ of them just looks very organised. Really, I want a set of lines that looks like someone just threw them into heaps (though probably without crossings for the avoidance of ambiguity). So I wrote my own.

### You can finally use TeX for maths in Microsoft Office… just about

As of next month, you’ll be able to type TeX maths into Office 365 apps and it’ll work.

See the announcement on the Microsoft Developer blog for more details. Warning: it’s a bit complicated.

### ShareLaTeX and Overleaf are merging

Once upon a time (2011), there launched an online LaTeX editor called ShareLaTeX. The very next year, there launched an online LaTeX editor called writeLaTeX. In 2015, writeLaTeX rebranded as Overleaf. Both Overleaf and ShareLaTeX offer browser-based LaTeX editing. Think of it like Google Docs for LaTeX. Both operate under a freemium model. If you use one of them, know that the other is fairly similar. If (like me) you were vaguely aware that there was an online LaTeX editor out there without using it, it was probably one of these or the other (or, I’m pretty sure, both at different times). Though note that these are not the only browser-based LaTeX editors – a native operating system ‘B’ means browser-based in this Wikipedia list of TeX editors and there are currently ten Bs in the list.

Recently, Overleaf fully acquired ShareLaTeX (Scribtex Limited) and plans to integrate the two products into one. The announcement says everyone from both teams will continue to be involved. The announcement contains more detail, a FAQ list and the following explanation.

What does this mean for you as an Overleaf or ShareLaTeX user? No worries! You won’t see any big change in the near future. Both services you know and love will continue to serve you as you have come to expect and be supported by the combined Overleaf and ShareLaTeX team. Over the coming months, we will be working on merging Overleaf and ShareLaTeX together into a single service. We aim to make the transition as smooth as possible. As we develop the combined service, we are actively seeking your feedback and input, starting with this survey. Ideally the only differences you will notice are the improvements to the editor you are currently using.

Exciting News — ShareLaTeX is joining Overleaf on the Overleaf Blog.

Also: ShareLaTeX Joins Overleaf on the ShareLaTeX Blog, which appears to be the same text.

### Photomath can read my writing

I remember when OCR of mathematics was such a difficult problem that there was no good solution. I remember hints some years ago that the then-current version of InftyReader could do a reasonable job of taking a PDF document and converting it into LaTeX code, but it was far from perfect.

Today my phone told me that the app Photomath has an update and now supports handwriting recognition. This means I can write something like this:

and Photomath does this with it:

### Fresh Turing preprints!

A couple of papers by Alan Turing have appeared on the arXiv.

No, that’s right – The Applications of Probability to Cryptography and The Statistics of Repetitions are two papers Turing wrote during the Second World War, and they’re now available on the arXiv, transcribed into modern LaTeX by Ian Taylor.