7

I have a collection of auto-generated LaTeX snippets (each in an individual file) that I include in multiple documents. These snippets use Unicode symbols instead of math commands (←, λ, ⇒, … instead of \leftarrow, \lambda, \Rightarrow, …).

Adding the following to my preamble allows pdfLaTeX to compile these snippets:

\DeclareUnicodeCharacter{03BB}{$\lambda$}
\DeclareUnicodeCharacter{2190}{$\leftarrow$}
\DeclareUnicodeCharacter{21D2}{$\Rightarrow$}

But I need to add it to the preamble of the document itself. Ideally, I'd rather keep these things hidden right in the LaTeX snippet files. This way the snippet files would contain the following:

\DeclareUnicodeCharacter{03BB}{$\lambda$}
\DeclareUnicodeCharacter{2190}{$\leftarrow$}
\DeclareUnicodeCharacter{21D2}{$\Rightarrow$}
(λ x ⇒ y ← x; y)

…and I would be able to include them without thinking about these pesky \DeclareUnicodeCharacter.

Is there a way to achieve the same as \DeclareUnicodeCharacter, but inside the body of a document? Can the effect be restricted to a single environment?

MWE:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

\DeclareUnicodeCharacter{03BB}{$\lambda$}
\DeclareUnicodeCharacter{2190}{$\leftarrow$}
\DeclareUnicodeCharacter{21D2}{$\Rightarrow$}

\begin{document}
\texttt{(λ x ⇒ y ← x; y)}
\end{document}

2 Answers 2

7

It's only made preamble only to save a few dozen bytes that were useful last century, you could turn that off...

\documentclass{article}


\makeatletter
\let\@onlypreamble\@gobble
\makeatother
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

\begin{document}



\begin{center}

\DeclareUnicodeCharacter{03BB}{$\lambda$}
\DeclareUnicodeCharacter{2190}{$\leftarrow$}
\DeclareUnicodeCharacter{21D2}{$\Rightarrow$}
\texttt{(λ x ⇒ y ← x; y)}
\end{center}
\end{document}
1
  • 1
    @egreg did you try that:-) ? Oct 30, 2016 at 15:58
7

You can use newunicodechar, which has the advantage of working also with XeLaTeX and LuaLaTeX.

You need to keep a copy of \newunicodechar.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{etoolbox}    
\usepackage{newunicodechar}
\let\xnewunicodechar\newunicodechar
\AfterEndPreamble{\let\newunicodechar\xnewunicodechar}

\begin{document}

\begin{center}

\newunicodechar{λ}{$\lambda$}
\newunicodechar{⇒}{$\rightarrow$}
\newunicodechar{←}{$\leftarrow$}
\texttt{(λ x ⇒ y ← x; y)}
\end{center}
\end{document}

enter image description here

5
  • 2
    Why is \newunicodechar \@onlypreamble? Oct 30, 2016 at 16:20
  • @UlrikeFischer Exactly for avoiding users do like in this question. ;-)
    – egreg
    Oct 30, 2016 at 16:22
  • 2
    In general it is certainly better to have such definitions in the preamble, but imho an option or something similar "onlypreamble=false" could be useful sometimes. Btw: imho the \AtBeginDvi does nothing in your example. Oct 30, 2016 at 16:36
  • 1
    @UlrikeFischer Yes, I mixed things up. And I'll think to that option.
    – egreg
    Oct 30, 2016 at 16:37
  • If it's not possible to modify the preamble for some reason, it's possible to copy-paste the source code: add \makeatletter\def\newunicodechar#1#2{\if\relax\detokenize{#1}\relax\nuc@emptyargerr\else\if\relax\detokenize\expandafter{\@cdr#1\@nil}\relax\ifnum`#1>\string"7F \catcode`#1=\active\begingroup\lccode`\~=`#1 \lowercase{\endgroup\protected\def~}{#2}\else\nuc@onebyteerr\fi\else\nuc@invalidargerr\fi\fi}\makeatother to the start of the document. (still need the \usepackage{newunicodechar} however)
    – user202729
    Feb 2, 2022 at 13:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .