TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why I get some errors with the code below, and how can I fix that behaviour?


\immediate\write\indexist{\catcode`\{=11{\catcode`\{=2 text}
\immediate\write\indexist{other text\catcode`\}=11}\catcode`\}=2}
share|improve this question
\string\\ (you're forgetting that \write does complete expansion). Why would you write on a .ist file? Isn't it easier writing it with an editor? – egreg Dec 3 '12 at 17:02
@egreg I have updated my question... I would to write on a .ist file because I want to automatize the procedure to get the index formatted correctly from the .ist file... – FormlessCloud Dec 3 '12 at 18:07
There's no point in writing an .ist file one line at a time with \write; the filecontents* environment may be much better for the task at hand. – egreg Dec 3 '12 at 18:50
up vote 12 down vote accepted

The problem with \\ is already solved by egreg: \string\\. That prevents expansion and converts \\ to two \ tokens with catcode 12 (same catcode as digits).

The same is needed for unmatched { and }. However unmatched braces do not work inside \write. A trick is using macros that are defined via \edef (expanded def), then \leftbracechar and \rightbracechar contain the braces as tokens with catcode 12.



\immediate\write\indexist{\leftbracechar text}
\immediate\write\indexist{other text\rightbracechar}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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