[Tex/LaTex] Using DeclareUnicodeCharacter locally (in document, not preamble)

pdftexpreambleunicode

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}

Best Answer

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}
Related Question