1. Write a good narrative
and then
2. Write good code
Which is to say that a mathematics text is an instance of literate programming.