# LuaTeX unicode escape sequence in an ASCII file (for graceful degradation)

I have a reusable component in which I would like to include a few unicode symbols. I use LuaLaTeX. However, I want the file to fail gracefully (it should detect running under something else and e.g. replace the symbols). However, for this, the file needs to be readable by ASCII tools, such that I cannot enter the unicode symbol directly. Is there any mechanism/macro in LuaTeX to escape unicode characters?

• Typing ^^^^1e0f is equivalent to typing ḏ (that is, U+1E0F). The number of ^ represents the number of following hexadecimal digits (lowercase for abcdef). Jul 7, 2015 at 11:54

Maybe this is what you're looking for.

\documentclass{article}
\usepackage{ifluatex}

\ifluatex
\usepackage{fontspec}
\else
\catcode\^^^=\active
\begingroup\lccode~=\^^^\lowercase{\endgroup
\def~^}#1#2#3#4{$\langle$U+\uppercase{#1#2#3#4}$\rangle$}

\fi

\begin{document}

^^^^1e0f

\end{document}


The ^^^^ notation in LuaTeX specifies the Unicode character corresponding to the following four hexadecimal digits (lowercase for abcdef).

If standard pdflatex is used, the sequence ^^^ is interpreted as a single character (with code "1E) that's made active so it gobbles the fourth ^ and absorbs the four digits.

If you have to use Unicode points higher than U+FFFF, it would be better to use six ^ and six digits (padding with zeros is always allowed). Here's the modification:

\documentclass{article}
\usepackage{ifluatex}

\ifluatex
\usepackage{fontspec}
\else
\catcode\^^^=\active
\begingroup\lccode~=\^^^\lowercase{\endgroup
\def~~}#1#2#3#4#5#6{$\langle$U+\uppercase{#1#2#3#4#5#6}$\rangle$}
\fi

\begin{document}

^^^^^^001e0f

\end{document}


## Output with pdflatex (six ^)

A hack for substituting the character (if it has a definition in some .dfu file)

\documentclass{article}
\usepackage{ifluatex}

\ifluatex
\usepackage{fontspec}
\else
\catcode\^^^=\active
\begingroup\lccode~=\^^^\lowercase{\endgroup
\def~^}#1#2#3#4{\uppercase{\csname GRACEFUL@#1#2#3#4\endcsname}}
\begingroup
\makeatletter
\def\DeclareUnicodeCharacter#1#2{%
\global\@namedef{GRACEFUL@#1}{#2}%
}
\input{t1enc.dfu}% and the other .dfu files you need
\input{ix-utf8enc.dfu}% and the other .dfu files you need
\endgroup
\fi

\begin{document}

^^^^017e

\end{document}


• That's a neat trick with the ^^^. Do you see some easy way with pdflatex + utf8-inputenc to output the correct symbols in the cases where they are declared? I thought about patching \DeclareUnicodeChar to create suitable commands but perhaps there is something better. Jul 7, 2015 at 12:52
• @UlrikeFischer That's a very interesting problem, but requires some thought. Jul 7, 2015 at 12:56
• I have been thinking about the problem already some time (and mentioned it yesterday to Joseph in the chat). Imho some standarized, engine independent method to do "unicode input in ascii" would be very useful in styles and for sorting applications like biber or xindy. Jul 7, 2015 at 13:03
• @UlrikeFischer It wouldn't be hard to define \Uchar(cat) for pdftex that expanded to the some (non expandable) code standing in for the character, easier to do that than make ^^^^ work Jul 7, 2015 at 13:09
• Nice! I actually intend to degrade on a higher level, but I would have had to google for something like \ifluatex` anyways. Thanks! Jul 7, 2015 at 13:19