7

Under Typesetting hashtags with natural syntax Joseph Wrigh ask me to ask a new question:

In LaTeX, \makeatletter is short for changing the category code of @. But something like

\def\makenumbersignletter{\catcode`#=11}

gives error. What should I do?

Here is the non-working example.

\documentclass{minimal}
\begin{document}
    \def\makenumbersignletter{\catcode`#=11}
\end{document}

More Question

Based on Joseph's answer, the safest way is

\catcode`\#=11\relax

Then it comes into my mind why TeX accepts the unsafe way \catcode`@11? And what should \catcode`\=11 be: is it \catcode`\\=11 or \catcode`\==11? Same problem appears between \let\a\b and \let\a=\b. The worst thing is that and = do not have a specific category code, what if one day I need \makegraveletter or \makeequalsignletter?

  • I'm asking what's the use of # with category code 11. If any, it should be 12. The category code of ` and = is normally 12 (and this category code is required in the syntax of assignments and alphabetic constants). – egreg Sep 17 '14 at 10:04
  • @egerg I just wonder where is the limit. I am not going to play with it in any serious document. (But 12 is required for csname such as \parameter#xxiv, is it reasonable?) – Symbol 1 Sep 17 '14 at 11:16
  • No; catcode 12 is not needed if you use \csname a#b\endcsname (unless it's in the replacement text for \def, where you need ##). – egreg Sep 17 '14 at 11:19
  • @egreg Codes combining \csname, \string, \expandafter, and so and so always drive me crazy. I think I should stop here since I can use fullwidth characters in Unicode. – Symbol 1 Sep 17 '14 at 11:27
4

The simple answer here is that you need to escape # and use \#: some chars are not 'safe' on their own but the escaped versions always are. A classic example is %, which would start a comment normally but doesn't when you use \%. As David says, TeX always allows the use of single-character commands as part of the backtick syntax in contexts such as `\catcode, so it's always safe to use the escaped version even if not needed.

Taking that knowledge and using the example from the linked question to make a few different possibilities we might end up with something like

\documentclass{article}
\newcommand*\makehashletter{\catcode`\#=11 }
\newcommand*\makehashparameter{\catcode`\#=6 }
\newcommand*\makehashtag{%
  \begingroup
  \lccode`\~=\#
  \lowercase{%
    \endgroup
    \def~##1}{\maketag{##1}}%
  \catcode`\#=\active
}
\newcommand\maketag[1]{\##1}
\begin{document}
\makehashletter
#
\makehashtag
#text
\makehashparameter
\end{document}

Notice that I've added a space after the catcodes in both of the cases here: this stops TeX looking for more digits and is best practice when creating such commands. (I could use a \relax here.)

  • 1
    \begingroup\lccode`~=`#\lowercase{\endgroup\let~}\maketag is much easier – egreg Sep 17 '14 at 9:56
  • @egreg Depends what exactly you want to achieve (if \maketag changes should # or not). – Joseph Wright Sep 17 '14 at 17:23
4

you can use

`\#

The backtick notation takes a single character or a single character control token, for this reason.

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.