Using the new tokcycle
package (https://www.ctan.org/pkg/tokcycle), we can map tokens from the input into gibberish and write them to an external file. Then, in a second document, as long as the mapping is known from the 1st document, we can unmap the externally input gibberish back into LaTeX code:
MAPPING
This maps a block of input and writes it to the file xform.tex
. Any characters not mapped (e.g., capital letters, numerals, etc.) are presented in the clear. Obviously, the mapping must be one-to-one, or the unmapping will be ambiguous.
EDITED to avoid using the \expanded
option of \addcytoks
, for those users who still have an older LaTeX installation.
After mapping, I also show a detokenized form of the gibberish:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tokcycle}
\newwrite\xfile
\long\def\remaptext#1#2\endremaptext{\tokcycle
{\addcytoks[4]{\tcremap{##1}}}
{\processtoks{##1}}
{\addcytoks{##1}}
{\addcytoks{##1}}{#2}%
\immediate\openout\xfile=#1
\immediate\write\xfile{\the\cytoks}
}
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}%
\tcmapfrom{#1}{#2}}
\newcommand*\tcmapfrom[2]{\expandafter\def\csname tcmapfrom#2\endcsname{#1}}
\newcommand*\tcremap[1]{\ifcsname tcmapto#1\endcsname
\csname tcmapto#1\expandafter\endcsname\else\expandafter#1\fi}
\tcmapto am \tcmapto bf \tcmapto cz \tcmapto de \tcmapto ey
\tcmapto fl \tcmapto gx \tcmapto hb \tcmapto ic \tcmapto jn
\tcmapto ki \tcmapto lr \tcmapto mh \tcmapto nt \tcmapto ok
\tcmapto ps \tcmapto qa \tcmapto ro \tcmapto sq \tcmapto tw
\tcmapto uj \tcmapto vp \tcmapto wd \tcmapto xg \tcmapto yu
\tcmapto zv \tcmapto ^& \tcmapto &^ \tcmapto +- \tcmapto -+
\begin{document}
\remaptext{xform.tex}%
\section{We can map sectioning, as well}
What can't we \textit{accomplish} if we try?
\begin{equation}
y = f(x) = \frac{1 + x^a}{47b}
\end{equation}
Let us be of good spirit and put our minds to it!
\endremaptext
Encoded input (detokenized):\\
\detokenize\expandafter{\the\cytoks}
\end{document}

An examination of the file xform.tex
shows the same (line breaks added by me):
\section {Wy zmt hms qyzwcktctx, mq dyrr} \par Wbmw zmt'w dy
\textit {mzzkhsrcqb} cl dy wou? \begin {yajmwckt} u = l(g) =
\frac {1 - g&m}{47f} \end {yajmwckt} Lyw jq fy kl xkke qscocw
mte sjw kjo hcteq wk cw!
UNMAPPING
Now, we run the unmap of the gibberish (contained in the external file xform.tex
), using the mappings from the original encode:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tokcycle,etoolbox}
\tokcycleenvironment\unmaptext
{\addcytoks[4]{\tcunmap{##1}}}
{\processtoks{##1}}
{\addcytoks{##1}}
{\addcytoks{##1}}
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}%
\tcmapfrom{#1}{#2}}
\newcommand*\tcmapfrom[2]{\expandafter\def\csname tcmapfrom#2\endcsname{#1}}
\newcommand*\tcunmap[1]{\ifcsname tcmapfrom#1\endcsname
\csname tcmapfrom#1\expandafter\endcsname\else\expandafter#1\fi}
\tcmapto am \tcmapto bf \tcmapto cz \tcmapto de \tcmapto ey
\tcmapto fl \tcmapto gx \tcmapto hb \tcmapto ic \tcmapto jn
\tcmapto ki \tcmapto lr \tcmapto mh \tcmapto nt \tcmapto ok
\tcmapto ps \tcmapto qa \tcmapto ro \tcmapto sq \tcmapto tw
\tcmapto uj \tcmapto vp \tcmapto wd \tcmapto xg \tcmapto yu
\tcmapto zv \tcmapto ^& \tcmapto &^ \tcmapto +- \tcmapto -+
\newread\infile
\makeatletter
\newcommand\unmapfile[1]{%
\def\fcont{}
\openin\infile=#1
{\endlinechar=-1
\everyeof{}%
\loop\unless\ifeof\infile
\read\infile to \temp
\ifx\temp\empty\else
\expandafter\g@addto@macro\expandafter\fcont\expandafter{\temp}%
\fi
\repeat
}%
\closein\infile
\expandafter\unmaptext\fcont\endunmaptext
}
\makeatother
\begin{document}
\unmapfile{xform.tex}
\end{document}
which provides the desired output:

So, if you have possession of the original mapping function and the file containing the mapped text, you can reconstitute the original!
\tracingall
the code?\tracingall
will almost always be less efficient than, say,pdftex
.