While further working on code for my answer to Different approach to literate programming for LaTeX, I am encountering a problem to which I am sure there is a very simple solution, but it completely escapes me.

The issue is: I want to be able to automatically index macro names, some of which will contain the @ character; hence, I need to preprocess them and substitute @ with "@ for makeindex to accept them. I have some code that works while in a listings environment but not when used in a normal environment or command (and I need them too). What's more, if I try to replace any normal letter, it works everywhere – it's only the @ that doesn't work.

So, I suspect the answer has got something to do with listings doing some wizardry on the catcodes, but I would like to find out what it is and how to reproduce it.

Here is the minimal (in which I am reproducing the actual structure of my code, in case the problem lies somewhere there):










(Note that it all works if you replace the @ with y in \StrSubstitute.)


It's your wizardry on the category codes. :)

You're defining the main macro in a context where @ has category code 11; but when you use it, \StrSubstitute can't find a category code 11 @. Use


without any \makeatletter.

If you're in a .cls or .sty file and want to keep the internal name \set@macro@type, then

\begingroup\lccode`?=`@ \lowercase{\endgroup

will do. The \lowercase operation will transform ? (which has category code 12) into a category code 12 @ before processing \endgroup and \newcommand. Only character tokens are affected by \lowercase, so the list of possibly changed tokens is {[1]#}?" and only ? (under the default setting) will be "lowercased" according to the previous assignment.

  • Thanks a million :) Since I am in a class file, the \makeatletter of the MWE wasn't technically wrong, but I altered the catcodes manually in \maketexcs and it all works fine now. Thanks. – ienissei Jun 28 '12 at 8:27
  • @ienissei I've added a way to do it without too many somersaults. – egreg Jun 28 '12 at 8:37

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.