# Writing Cyrillic text to a file

could you explain, what is going on?

I am using the latest MiKTeX 2.9.6960 64-bit version (installed just yesterday).

I am trying to write some Cyrillic text to the separate file without using inputenc package:

\documentclass{article}

\begin{document}

\newwrite\testwrite

\immediate\openout\testwrite=testwrite.txt

\immediate\write\testwrite{Ббббббббббббб}

\immediate\closeout\testwrite

\end{document}


But LaTeX does not tell me that there is something wrong with encoding, but instead of this it tells me that some its internal commands are undefined. Here is a part of the corresponding log-file:

! Undefined control sequence.
\GenericError  ...
#4  \errhelp \@err@     ...
l.9 \immediate\write\testwrite{Ббббббббббббб}

?
! Undefined control sequence.
\GenericError  ...
\let \@err@               ...
l.9 \immediate\write\testwrite{Ббббббббббббб}

?
! Undefined control sequence.
\GenericError  ...
\@empty \def \MessageBreak...
l.9 \immediate\write\testwrite{Ббббббббббббб}

?
! Undefined control sequence.
\GenericError  ...
\endgroup
l.9 \immediate\write\testwrite{Ббббббббббббб}

?
! Undefined control sequence.
\GenericError  ...
#4  \errhelp \@err@     ...
l.9 \immediate\write\testwrite{Ббббббббббббб}

?
! Undefined control sequence.
\GenericError  ...
\let \@err@               ...
l.9 \immediate\write\testwrite{Ббббббббббббб}

?
! Undefined control sequence.
\GenericError  ...
\@empty \def \MessageBreak...
l.9 \immediate\write\testwrite{Ббббббббббббб}


and so on.

Is it a bug or what is going on?

• I suppose you are using pdfTeX, in which case Cyrillic characters aren't plain characters, but active ones. TeX expands characters when writing to a file, but you haven't declared any encoding for them. You'd have to use some verbatim approach to avoid the expansion of these characters, like the \detokenize approach David just posted, the filecontents environment, or the \verbwrite command I created here. – Phelype Oleinik Feb 28 at 15:49

The active characters are set up to expand to typesetting instructions (or to error messages saying that the character is not set up) you just want to write the text verbatim so:

\documentclass{article}

\begin{document}

\newwrite\testwrite

\immediate\openout\testwrite=testwrite.txt

\immediate\write\testwrite{\detokenize{Ббббббббббббб}}

\immediate\closeout\testwrite

\end{document}

• Thank you so much, but as already I wrote below to Ulrike Fischer, my question was "Why LateX letts me that some its internal commands which are absolutely not related to my code are undefined". If I write Cyrillic text as a text, LaTeX tells me that Cyrillic letters have wrong Unicode codes. But if the Cyrillic text is inside \write{...}, Latex tells me that \GenericError command or some other comands inside it are undefined. – Andrei Malokostov Feb 28 at 18:36
• @AndreiMalokostov you are using a \write command, which expands its argument completly without any precautions. It is easy to get such error messages with it if you put commands in its argument. If you want a better error message use \makeatletter \immediate\protected@write{...}. – Ulrike Fischer Feb 28 at 18:50
• OK, thank you so much. I thought from my 25+ year experience that TeX (and LateX also) have very good system of error messages, and these messages are quite informative. So I was very confused to get such strange (from my point of view) messages. But if you suppose that it is all OK with them, I will trust you. But in any case I would prefer to obtain from LateX something like "If all else fails, read the instructions" :-) In any case thank you so much for you answers and for your attention. – Andrei Malokostov Feb 28 at 19:10
• @AndreiMalokostov unfortunately the error occurs while tex is writing the file and it really can't stop and make a sensible latex-specified error at that point if it fails it just fails with some more or less spurious low level error. – David Carlisle Feb 28 at 19:26

If you don't want to use inputenc, add \UseRawInputEncoding

\UseRawInputEncoding
\documentclass{article}

\begin{document}

\newwrite\testwrite

\immediate\openout\testwrite=testwrite.txt

\immediate\write\testwrite{Ббббббббббббб}

\immediate\closeout\testwrite

\end{document}

• Thank you so much, and thank you for your recommendations. But my question is NOT about "How to avoid such error messages", but "Why LaTeX responda in such a way and why it tells me that \GenericError command (or some other command inside the definition of \GenericError) are undefined?". I know other ways to avoid such error messages. For example I can declare \newtoks\Bbbbtoks, assign \Bbbbtoks={Ббббббб} and then use the command \write\testwrite{\the\Bbbbtoks}, this works well. But I would like to get an answer to my second question. – Andrei Malokostov Feb 28 at 18:28
• If you are interested in the processing of the active chars (Б and б are now active and so commands) then I would suggest that you add tracingmacros=1 and then look in the log file. If you want to know why they are active you could read this latex news letter: latex-project.org/news/latex2e-news/ltnews28.pdf – Ulrike Fischer Feb 28 at 18:34
• OK, thank you so much. I will try to use \tracingmacros=1 understand, how commands like "Б" or "б" are transformed to the error messages of the type ! Undefined control sequence. \GenericError ... \let \@err@ ... – Andrei Malokostov Feb 28 at 18:51