8

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?

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

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 LuaLaTeX (both codes)

enter image description here

Output with pdflatex (four ^)

enter image description here

Output with pdflatex (six ^)

enter image description here

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}

enter image description here

  • 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. – Ulrike Fischer Jul 7 '15 at 12:52
  • @UlrikeFischer That's a very interesting problem, but requires some thought. – egreg Jul 7 '15 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. – Ulrike Fischer Jul 7 '15 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 – David Carlisle Jul 7 '15 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! – burnpanck Jul 7 '15 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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