8

If \@percentchar represents %, is there a macro for #? I have tried \@sharpchar and \@numberchar but both do not exist. Do you have any idea what is the correct macro name for #?

2 Answers2

13

To typeset # you can use \# but to have a macro that expands to a catcode 12 (normal character) # use \zzz defined by

\edef\zzz{\string#} 

or some other macro name of your choice.

David Carlisle
  • 757,742
8

It depends on what you want to do. If all you need is to print a # then \# is good; but \# is unexpandable, so in a \write it will be passed as \#. In a \write18 it will be the shell's duty to interpret it correctly. For instance, on a Unix system you can't do

\write18{echo # foo}

(probably a more useful command would be passed), because the shell wouldn't see foo as # is a comment character. Doing

\write18{echo \# foo}

will echo # foo. On the other hand,

\write18{echo '# foo'}

would echo ## foo, because of the peculiar TeX's habit of doubling hash marks in token lists when # has, as usual, category code 6. But

\write18{echo '\# foo'}

would echo \# foo.

So a good strategy is to have a control sequence that expands to a category code 12 hash mark in all cases, given that TeX expands token lists in a \write:

\edef\hashmark{\string#}

Thus

\write 18{echo '\hashmark\space foo'}

will echo # foo.

Unfortunately, different shells tend to have their own idiosyncrasies and with special characters it's always a pain getting equal behavior on all of them on every operating system. Some need quotes around the commands, some don't want them (I guess that Windows has very different conventions than Unix).

egreg
  • 1,121,712