Should one enclose variable names in commata?

In scientific writing, when should one enclose variable names in commas?

Take the following example sentences:

1a) "For a set of pixels S, two pixels p and q from S are said to be connected if there exists a path between them."

1b)"For a set of pixels, S, two pixels, p and q from S, are said to be connected if there exists a path between them."

and

2a)"The 4-neighbors N4 are marked in light gray, and the diagonal neighbors ND are marked in dark gray."

2b)"The 4-neighbors, N4, are marked in light gray, and the diagonal neighbors, ND, are marked in dark gray."

Which would you prefer and why? (Or are both bad?) More generally: is there any hard rule for this?

EDIT: My field is computer science.


Solution 1:

The Chicago Manual of Style, sixteenth edition (2010) refers to introductory phrases of the type

For a set of pixels S, ...

as "qualifying clauses." In section 12.22 ("Qualifying clauses for displayed expressions"), Chicago makes clear that it does not endorse adding a comma immediately before the symbol or symbols introduced in the qualifying clause:

The qualifying clause may appear in the text, preceding the displayed main expression:

For all real numbers a and b,

|a + b| ≤ |a| + |b|

Elsewhere Chicago gives examples with qualifying clauses such as

A function f is even if f(–x) = f(x); f is odd if f(–x) = –f(x).

and

If a = b, then for all real numbers x,

a + x = b + x

In each of these instances, Chicago declines to include a comma prior to the first mention of the variable or variables. That is, it implicitly endorses "all real numbers a and b," over "all real numbers, a and b," and "a function f" over "a function, f," and "for all real numbers x," over "for all real numbers, x,".

Webster's Standard American Style Manual (1985) addresses the poster's specific question more directly than Chicago does:

A sign or symbol used as an appositive directly following a descriptive term is not set off by punctuation unless it is necessary to avoid ambiguity.

Two sets A and B are said to be equivalent if there is a one-to-one correspondence between them.

According to Merriam-Webster, "A and B" functions as an appositive relative to "Two sets" and should not be set off with commas or any other punctuation marks.

I have dealt with house styles that differed on this point—but if it were up to me, I would be inclined to follow the advice of Chicago and Webster's and leave the variables comma-less unless complications elsewhere in the sentence made the sense of the construction ambiguous without supporting punctuation.