# What are expl3 quarks? When, where, why and how should I use them?

I feel particularly bombarded by sub-atomic particles right now (and it's mostly Will's fault :)). First, in Will's answer to this question today, he referred to expl3's \prg_case_str:nnn command. Digging into the package, I noticed the command involves an interestingly named macro called \quark_if_recursion_tail_stop_do:nn. All I can gather from the expl3 documentation about things with the word (particle;)) quark in their name is that:

l3quark A ‘quark’ is a command that is defined to expand to itself! Therefore they must never be expanded as this will generate infinite recursion; they do however have many uses, e.g. as special markers and delimiters within code.

Hmmm.

Coincidentally, Will and Kevin's new hardwrap package was released on CTAN today (many congrats guys!). Digging into the documented code, I notice:

\hw@scanstop

This is a ‘quark’ from expl3 designed to delimit scanning; it will never be executed, else an infinite loop results.

\protected\def\hw@scanstop{\hw@scanstop}

Reading the remainder of hardwrap's code, yes, \hw@scanstop is used in exactly the way described in the definition of l3quark, above.

Still, I'm perplexed. What's so special about self-referencing tokens as opposed to any other token assigned a special-purpose name, i.e., any other vanilla signifier, whether a command sequence or not? Are they self-referencing only for error-catching purposes?

If LaTeX quarks do have interesting properties beyond just being specially defined and used signifiers, where, when, how and why should I best employ them?

Are they really expl3 specific, or is it more a convention that has been adopted into the LaTeX3 programming patterns set?

Finally, out of interest (in the spirit of Yiannis's recent question about Lisp car and cdr "relics" in TeX's lexicon), what's the story about how the word quark found its way into LaTeX3 vocabulary? (Please tell me that LaTeX3 quarks aren't envisioned to possess color, flavor, etc. properties!)

• In hardwarp I'm afraid Will is not quite right: as he's \protected the definition this is not a quark. I know that because I tried something similar and found it does not work. As Hendrik explains, the key is \ifx testing, and there \protected status is important. If you store a quark inside a variable, then the \ifx will only be true if the quark is not protected (variables are not protected). – Joseph Wright Nov 11 '10 at 15:35
• @Joseph Wright, thanks for the great info and your comment below, but the +1 is for writing hardwarp apropos the question's quantum theme! :) – Geoffrey Jones Nov 12 '10 at 8:40
• @JosephW yes, you're right these aren't really quarks—at one stage they weren't \protected but the code documentation stuck. Oh well :) – Will Robertson Nov 13 '10 at 13:10

So many questions. I'm only going to address "What's so special about self-referencing tokens?", and I'm giving a plain TeX answer. As I learned from TH (at the end of a rather lengthy comment-discussion here), they can be useful for \ifx tests. If I know that #1 is one token, and I do the test

\ifx#1\my@test@token


to find out if #1 is \my@test@token, then it is good to have the definition

\def\my@test@token{\my@test@token}


For \ifx, two control sequences are the same if they have the same definition (including every detail such as parameters and other stuff), but they won't be expanded for the comparison. (That shouldn't happen as you explained above.) For an example see TH's very nice answer that I linked to above.

Be careful: The \ifx test also works if \my@test@token is undefined, but then it turns out true whenever #1 is an undefined control sequence.

• This is a key reason for the definition as-is. It makes it possible to do fast tests for a variable containing only a quark, which means that the quark can then be used as a marker of 'no value' or such like. The key point here is performance on single-token tests: this is necessary as these tests come up an awful lot. – Joseph Wright Nov 11 '10 at 15:32
• Great explanation, Hendrik. So, summing up, LaTeX3 quarks are basically conventional sentinels which, for efficiency reasons, to guarantee they will never collide with or match any token except themselves (including undefined), and maybe to make any errors with using them pretty obvious (stack overflow), they're defined first-order as themselves. Reading between the lines, LaTeX3 quarks are conventionally (always?) used as sentinals, but rather than calling them 'sentinels', LaTeX3 (but not necessarily TeX/LaTeX2) idiom names them 'quarks' (à la TeX's 'whatsits' :)) ). – Geoffrey Jones Nov 12 '10 at 8:30
• @GeoffreyJ Right, and I have no idea where the name ‘quark’ came from. (I like the name a lot, though.) One other advantage of them is that you can expand a quark once and end up with the same thing, which might also be occasionally useful. – Will Robertson Nov 13 '10 at 13:13
• @Will: From that viewpoint, a quark is like a quine, and those two words are rather similar. – Villemoes Mar 3 '11 at 23:19
• @GeoffreyJones The main drawback of quarks is that because TeX optimizes tail calls, expanding a quark leads to an infinite loop rather than a stack overflow. Still something detectable (TeX hangs), but not nearly as nice, since it takes time to realize. – Bruno Le Floch Sep 5 '12 at 9:40