\gap is meant to print its argument, and also write it to an external file, as an argument of a \keyitem command, which is then invoked to print the argument again. That it does, but in the .key file created after running the above code through LaTeX I found that the argument had been written as a string of unrecognizable characters, with an extra \T1 before the \l{}, and some unnecessary spaces. The \T generates an "undefined control sequence" until I explicitly write \def\T1{\relax} in the preamble (ugly, I know).
Incredibly, the gibberish from the .key file is printed correctly in the .dvi file. However, I would like to keep all my files "pure" ASCII for the publisher's benefit, to preclude printing errors.
Can anyone here suggest a way to make LaTeX export to the .key file exactly what I give \gap in the .tex file? In other words, to make the .key file read
I understand what you are saying, but in the string I input for the .key file there aren't any non-standard characters -- only backslashes, apostrophes, braces, letters. Or at least I'd like TeX to treat them as such, which for some reason it refuses to do, regardless how many \string commands I put in -- it continues to treat "\k{a}" as a single non-ASCII character, rather than five standard characters.
Additionally, if I change the input encoding appropriately using the inputenc package, LaTeX writes the .key file with the correct national characters and diacritics. In so doing, however, I would make the .key file non-ASCII, which I am trying to avoid.
I'm playing with redefining the character categories just for the purpose of \write, so it would stop treating \ as an escape character and \' or \l as control sequences, but no luck so far...
I'm not an expert because I still have difficulties in understanding TeX. Especially macro expansion is a closed book to me. But I think that the argument for the \gap command expands first thus produces accented characters and causes trouble.
This is not nearly as cool as verbatim \write to a file would be, but it works in this particular case. I took advantage of only dealing with a finite set of control sequences, and I redefined each of them locally for the purpose of the \write command.
This code allows the foreign characters, e.g. \.z, to be output output to the .key file as "\.z" exactly (three characters), not as a single character (the letter "z" with a superior dot). There are still some extra spaces present in the .key file, but these are absorbed by the control sequences. I don't know how or if this solution could be adapted to work on any control sequence used as the argument of \write.
Thank you everyone for any time and effort that you have devoted to helping me out with this problem!