Use typewriter font for code inside prose

There’s a useful tradition of using a typewriter font, or more generally some monospaced font, for bits of code sprinkled in prose. The practice is analogous to using italic to mark, for example, a French mot dropped into an English paragraph. In HTML, the code tag marks content as software code, which a browser typically will render in a typewriter font.

Here’s a sentence from a new article on Python at Netflix that could benefit a few code tags.

These features (and more) have led to increasingly pervasive use of Python in everything from small tools using boto to talk to AWS, to storing information with python-memcached and pycassa, managing processes with Envoy, polling restful APIs to large applications with requests, providing web interfaces with CherryPy and Bottle, and crunching data with scipy.

Here’s the same sentence with some code tags.

These features (and more) have led to increasingly pervasive use of Python in everything from small tools using boto to talk to AWS, to storing information with python-memcached and pycassa, managing processes with Envoy, polling restful APIs to large applications with requests, providing web interfaces with CherryPy and Bottle, and crunching data with scipy.

It’s especially helpful to let the reader know that packages like requests are indeed packages. It helps to clarify, for example, whether Wes McKinney has been stress testing pandas or pandas. That way we know whether to inform animal protection authorities or to download a new version of a library.

Tagged with: ,
Posted in Typography
8 comments on “Use typewriter font for code inside prose
  1. Matt Williams says:

    And in LaTeX, {tt pandas}.

  2. Canageek says:

    That is a very old and not generally approved of way to do things in LaTeX Matt.
    A far supriour way is to use the Listings package: http://stackoverflow.com/questions/586572/make-code-in-latex-look-nice has some very good examples, and Wikibooks has more: http://en.wikibooks.org/wiki/LaTeX/Packages/Listings

  3. Abdulsattar says:

    And if you’re using a monospaced font, make sure you use the family. I’ve seen many websites built on Mac that use Monaco as their monospaced font but fail to specify the fallback. This renders whole code blocks in a default font.

    A simple font-family: Monaco,monospace would solve the issue. I’m not talking about news websites but about programmer focused sites like Socket.io and slim-lang.com (These sites had only Monaco until recently).

  4. Interesting, I just started looking at Python, might try to learn it.

  5. Yawar says:

    Agree about code listings in general. But it’s not clear that program names, when used outside of a code context, or outside the context of showing how to run the program, should be monospaced. Do we monospace LaTeX, e.g.? Or Microsoft Word? etc.

  6. John says:

    Yawar: I agree that’s fuzzy. I would set libraries in monospace but not programs. I think that’s conventional. And it makes sense: program names are generally capitalized and more familiar.

    I’d set command line utility names like sed in monospace because they are not capitalized and because they’re conceptually more like libraries for the shell than like programs. Also, some of them are common words like top and nice.

  7. John says:

    Martin and Dmitry: Good points.

    Allowing zeros does make things more difficult, maybe impossible. Certainly impossible to brute force by computer. Maybe there are theoretical ways to limit, for example, the number of primes of the form 1000….0003, but that’s a much harder question.

  8. Matt Doar says:

    I find the O’Reilly Style guide works well for text with code in it. More info at http://oreilly.com/oreilly/author/stylesheet.html

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>