Let's say I have the following text:

The first program a computer science student sees is usually

program HelloWorld(output);
  WriteLn('Hello, World!');


10 PRINT "Hello World!"

The second program ...

How should I punctuate such a text? Putting a period in front of "The second program ..." looks really ugly. With mathematical equations, it's possible to add punctuation at the end of an equation, but for code samples it's impossible, as it would break the code. For example, the first code snippet must end with the period, while the second one must not.

There are a few other question (like For formal articles, should a displayed equation be followed by a punctuation to conform to the language grammar ?) that deal with a similar problem, but only for mathematical equations, not for code samples, where the situation is different.

  • 6
    I use punctuation in displayed equations, but not in displayed code, where it would be impossible to place it in a sensible way.
    – egreg
    Commented Jan 25, 2014 at 22:50
  • Knuth himself doesn't use punctuation for small code snippets like this.
    – texnokrat
    Commented Jan 25, 2014 at 23:04
  • 4
    I would definitely not add punctuation to code snippets since others reading such code might incorrectly assume that the punctuation was actually part of code.
    – A.Ellett
    Commented Jan 26, 2014 at 1:38
  • 1
    I would see displayed code as a form of image and thus no punctuation. BTW: a few years ago a copy editor from a publishing firm wanted us to add a dot after floating tables. No, we did not add any (same person insisted on spelling Erdős using ö)
    – daleif
    Commented Jan 27, 2014 at 13:06
  • 1
    A math formula is part of the sentence, so it deserves punctuation symbols. I wouldn't consider a listing part of the sentence as it's (generally) too complex. Therefore I wouldn't add any punctuation symbols (at the sentence level). I just list the listing, just like you list a poem.
    – user10274
    Commented Jan 27, 2014 at 13:14

1 Answer 1


Most style guides recommend adding punctuation after inline formulas (and by extension presumably to other displayed material).

I for one never place punctuation after displayed contents (non-inline math, algorithms, ...). It just adds extraneous material to the display, which can make it harder to read (and it could very well change the meaning, in a program '.' is usually meaningful and might be an error; a formula followed by '!' means something completely different most of the time). I just write as though the display is its sentence-ending period, but mostly try to rearrange wording so that the sentence ends before the display (or no comma or such is needed just after it).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .