[Tex/LaTex] Implementing \DeclareUnicodeCharacter in LuaLaTeX an XeLaTeX

luatexunicodexetex

In my code, I used to write

\DeclareUnicodeCharacter{27E6}{\begin{description}} % ⟦
\DeclareUnicodeCharacter{27E7}{\end{description}} % ⟧ 
\DeclareUnicodeCharacter{2022}{\item} %  •

which worked great with pdflatex. Moving to lualatex the command does not compile. Is there a way I can define my version of \DeclareUnicodeCharacer so that it still works?

Best Answer

I'd consider

⟦
•[Gnats] are small animals
•[Gnus] are big animals
⟧

as code obfuscation. Anyway, this works with all engines. However, for pdflatex it requires utf8 passed to inputenc or inputenx (as opposed to utf8x).

\usepackage{newunicodechar}
\newunicodechar{⟦}{\begin{description}}
\newunicodechar{⟧}{\end{description}}
\newunicodechar{•}{\item}

If you want to reuse the declarations you already have, you could do like this:

\documentclass{article}
\usepackage{ifxetex,ifluatex}

\newif\ifunicode
\ifxetex\unicodetrue\fi
\ifluatex\unicodetrue\fi

\ifunicode
  \usepackage{fontspec}
  \usepackage{newunicodechar}
  \newcommand{\DeclareUnicodeCharacter}[2]{%
    \begingroup\lccode`|=\string"#1\relax
    \lowercase{\endgroup\newunicodechar{|}}{#2}%
  }
\else
  \usepackage[utf8]{inputenc}
\fi

\DeclareUnicodeCharacter{27E6}{\begin{description}} % ⟦
\DeclareUnicodeCharacter{27E7}{\end{description}} % ⟧ 
\DeclareUnicodeCharacter{2022}{\item} %  •

\begin{document}

Some text
⟦
•[Gnats] are small animals
•[Gnus] are big animals
⟧
and some other text

\end{document}

enter image description here