5

While writing a CWEB program I wanted to write a comment like this:

@ If the option \.{-a} has been specified, the output files need to be opened in
append-only mode. Our program should also create the output file, thus the flags with
which we open the output file are either |O_RDONLY|O_CREAT|O_APPEND| or
|O_RDONLY|O_CREAT| as explained above.

It seems that CWEB does not like mentions of the bitwise-or operator | inside inner C contexts. Is there any way to mention a bitwise-or operator in a TeX section without manually reconstructing the output of CWEAVE?

  • Try using @t$\vert$ as a replacement for |. That is, ...are either @t$\vert$O_RDONLY@t$\vert$O_CREAT@t$\vert$O_APPEND@t$\vert$ or @t$\vert$O_RDONLY@t$\vert$O_CREAT| as... – Werner Nov 18 '14 at 20:00
  • @Werner CWEAVE uses the macro \OR which is defined as \let\OR=\mid so the author can change the appearance of the bitwise-or. If I were to use the approach you describe, I would rather use this macro. Still, I hope there are better solutions. – FUZxxl Nov 18 '14 at 20:17
  • So what is your ideal situation in terms of the input you supply? You can define your own TeX-macros using the @f <name> TeX directly, and then define \<name> to your liking. – Werner Nov 18 '14 at 20:54
  • @Werner @f something TeX with \let\something=\OR won't work because something will be treated as an identifier and thus subject to different spacing than the bitwise-or operator. – FUZxxl Nov 18 '14 at 22:06
  • I'll repeat the question then: So what is your ideal situation in terms of the input you supply? – Werner Nov 18 '14 at 22:17
6

I asked the following in a letter to Donald Knuth:

Lastly, I started to use your CWEB system for a project to find out if literate programming is a style that suits me. So far, I like it very much! The only problem I am experiencing is that I haven't found a way yet to mention the bitwise-or operator | within a c-section. Is there special syntax for that or do I have to use kludges tricks with @t?

He gave this answer:

Amazingly – after thousands of programs of which more than 100 must surely use bitwise or – I've never felt the need for that operator inside a comment! (I do have \vert defined in plain.tex so that I don't need | in math much.)

There really is no way to “fool” the parser in ctangle/cweave, hence one cannot expect to just type any C code that includes the ‘|’ character. [That means || is problematic too]. My natural inclination, if I wanted ‘x|y’ within TEX code, would be $|x|\OR|Y|$ using the \OR from cwebmac.tex. [Actually I use different spacing in TAOCP …

bitwise AND is \mathchardef\band="2026  
        OR     \def\bor{\mathbin{\kern1mu\vert\kern1mu}}  

but \OR = \mid in cwebmac].

If you need it more often or in quite general contexts, the best kludge I can think of is to substitute another binary operator that you aren't using, and let TEX make the switch:

\def\bitor#1!{\begingroup\let\MOD=\OR#1\endgroup}
Look at |x%y| and \bitor|x%y|! for example.

comes out as

Look at x % y and x | y for example.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.