# expand macro in single byte encoded code

In this minimal (not) working example, I try to use single-byte latin10-encoded source file, and expand a command-macro containing a character with \catcode254:

    % -*- mode: latex; unibyte: t; coding: iso-8859-16 -*-
\documentclass[a4paper,10pt]{report}

\usepackage[T1]{fontenc}
\usepackage[latin10]{inputenc}

\newcommand\mwe{Lista de demonstrații}

\begin{document}

\chapter{Oscilații}

\xdef\MWE{\mwe}

\MWE

\end{document}


but I get this "\crcr" error in the log:

    ! Illegal parameter number in definition of \MWE.
\crcr
l.14 \xdef\MWE{\mwe
}


If (file is not UTF-8!) I replace the ț (\catcode254) with ș (\catcode186), same error.

Instead, if I replace with â (\catcode226) or with just t, the error disappears.

These latin10 letters are for Southeastern European languages.

The useful code I need for a \LaTeX class is related with \addcontentsline; here is another example:

    % -*- mode: latex; unibyte: t; coding: iso-8859-16 -*-

\documentclass[a4paper,10pt]{report}

\usepackage[T1]{fontenc}
\usepackage[latin10]{inputenc}

\newcommand\mwe{Lista de demonstrații}

\begin{document}

\makeatletter
\@expandtwoargs\toc{\mwe}{}
\makeatother

\end{document}


and the log:

    ! Illegal parameter number in definition of \reserved@a.
\crcr
l.14 \@expandtwoargs\toc{\mwe}{}


Thank s.

Unicode (i.e., \usepackage[utf8]{inputenc}) is not a solution here, since these latin10-encoded lines come from \TeX text input streams (\immediate\read), hence the single-byte constraint.

-
You mean charcode not \catcode, right? –  Martin Scharrer May 15 '12 at 15:39
Yes, i meant i typeset \catcode254=12 just before \read, so i was just hinting. –  sjb May 15 '12 at 16:08
Ok, because "a character with \catcode254" doesn't make much sense. You probably mean "the character \char254 with catcode 12". –  Martin Scharrer May 15 '12 at 16:37

@David: \catcode254 is the catcode of character 254, likely 13. I have to admit some astonishment about your statement, but then the original poster also uses this wrongly, likely meaning \char254.

There is always the possibility of using eTeX features for sidestepping LaTeX's protection issues:

\edef\MWE{\unexpanded\expandafter{\mwe}}


In the particular issue of using LaTeX's write commands, however, \protected@xdef seems like the way to go: writing typically involves several staggered ways of expansion, and anticipating them means knowing the effect of LaTeX internals. The above \edef, in contrast, operates from the assumption that \mwe will work in the given context, and produces something equivalent.

One can also add "ultimate" protection by using \protected\edef followed by the above. In the given application, it would then write out the 8bit character unchanged, whereas LaTeX's default would be to write the LICR (LaTeX internal character representation), something like \d{t} or so. Consequently being fit for getting read back in even under different input encodings.

-
I'm getting too old for this game I'll delete the catcode remark, thanks:-)+1 It's true that protected edef would work here but probably other packages will make unprotected definitions and so the basic point that using xdef is in general unsafe still stands. –  David Carlisle May 15 '12 at 15:00
@DavidKastrup: The @user syntax only works within the comments of the same posts, not between posts. You might want to post a comment in addition to your answer next time. Also, I just saw that your account is still unregistered. You might want to register it properly, otherwise you might loose access to it after a while, e.g. if your session cookie expires etc. –  Martin Scharrer May 15 '12 at 15:43
This \@expandtwoargs\toc{\unexpanded\expandafter{\mwe}}{} is needed for a \prelim@contents i employed (from Sergio Gelato's cornell class, but with \inputenconding{latin10} \read macros), thank s. –  sjb May 15 '12 at 16:25
@MartinScharrer: as you can see, I only use this "account" to answer people. The "privileges" are not available without registering, and I don't need to feed my data into another address harvester and keep track of another password and associated usage conditions. If I would actually start to care, it would be time anyway to get rid of another time sink, so "losing access" would be just the thing. –  user9588 May 16 '12 at 16:53
@DavidKastrup: I just mentioned it, in case you simply forgot to register. Your name and email address are already in the system, so there wouldn't be any change here. –  Martin Scharrer May 16 '12 at 16:58

If I run your example I get

! Illegal parameter number in definition of \MWE.
\crcr
l.12       \xdef\MWE{\mwe


Active characters are made robust using the LaTeX \protect mechanism but that only works if you use the LaTeX mechanisms. \xdef will break any fragile command. If you change your document to

\makeatletter
\protected@xdef\MWE{\mwe}
\makeatother


It runs for me without error.

-
Ok, thanks, actually i have a my@def[2] interface for defining macros, where i employed \xdef; i will use \protected@xdef now. For the MWE i presented, it works. –  sjb May 15 '12 at 16:14