1

How to make it so that following latex

\documentclass{article}
\begin{document}
n
\end{document}

produces a pdf with letter n replaced by m?

From How to automatically replace one letter with another before rendering I see one option is to

\catcode`\X=\active
\def  X{Y}

But that don't work with letter n

\catcode`\n=\active
\def  n{m}
KcFnMi
  • 1,242
  • 1
    The \catcode`\n=\active doesn't work if you put it into the preamble, because \begin{document} contains an n (and you have just changed the meaning of n). It does work if you make the definition inside the document environment and additionally open a group around it and the text it should affect (to avoid messing with \end{document}). But you still won't be able to use any commands with n in their name within that group. – moewe May 08 '19 at 05:27
  • Related: https://tex.stackexchange.com/q/213947/35864 – moewe May 08 '19 at 05:28
  • 1
    Just out of curiosity: Why do you want to do that? As surprising as it may sound, (La)TeX is not extremely good at manipulating the raw text you input into the document. So I would usually try to avoid having to do that as much as possible. (Many editors have search and replace or RegEx features to do the replacement directly in the document.) – moewe May 08 '19 at 05:35

2 Answers2

2

With luatex you could add a substituation rule to the font:

\documentclass{article}
\usepackage{fontspec}
\directlua
{
 fonts.handlers.otf.addfeature
  {
    name = "mysubs",
    type = "substitution",
    data =
      {
          ["n"] = {"m"},
      },
  }
}

\setmainfont{Latin Modern Roman}[RawFeature=+mysubs;]

\begin{document}
n m o p 
\end{document}

With xelatex something similar is possible with TECkit mappings (such mappings are e.g. used to map -- to an endash.).

Ulrike Fischer
  • 327,261
1

It can work only if you scope limit the catcode change, since any macro with an "n" in its name (such as \end) will break the code.

\documentclass{article}
\newcommand\Nsubon{\catcode`\n=\active}
\newcommand\Nsuboff{\catcode`\n=11 }
{\Nsubon\gdef n{m}}
\begin{document}
\Nsubon jklmn
and when I am done...
\Nsuboff

{\Nsubon Alternately, scope the code in a group}
\end{document}

enter image description here