Using DeclareUnicodeCharacter locally (in document, not preamble)

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}


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}

• @egreg did you try that:-) ? – David Carlisle Oct 30 '16 at 15:58

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}


• Why is \newunicodechar \@onlypreamble? – Ulrike Fischer Oct 30 '16 at 16:20
• @UlrikeFischer Exactly for avoiding users do like in this question. ;-) – egreg Oct 30 '16 at 16:22
• 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. – Ulrike Fischer Oct 30 '16 at 16:36
• @UlrikeFischer Yes, I mixed things up. And I'll think to that option. – egreg Oct 30 '16 at 16:37
• Great suggestion.... – MadyYuvi Jun 23 '20 at 14:30