# What does fontenc do and when is it applied? Based on an example of creating my own package

After quite a time of using LaTeX, I am now trying to understand how it works. The following example concerns the functionality of fontenc.

In order to not have to write down the inclusions of all packages that I normally use, I created the package MP, which does these inclusions and which also provides a command to create a titlepage. For the latter functionality key-value options are supplied to MP via pgfopts.

MP has

\usepackage[T1]{fontenc}


handles input via

\pgfkeys{
keyone/.store in=\MP@keyone
...
}
\ProcessPgfPackageOptions*


and provides a title page command

\newcommand\MPtitlepage{...}


However, when I use MP in my main document, without having \usepackage[T1]{fontenc} in the main document, and supply an option with foreign characters to MP, Latex displays them incorrectly. For example the main document

\documentclass[a4paper]{article}
\usepackage[keyone=R\aul]{MP}
\begin{document}
\MPtitlepage
\end{document}


displays Raul with a quotation mark under the u. On the other hand, I can omit the fontenc in MP without having this problem.

Using this example, can someone explain what fontenc does? In particular I am confused because it seems to me from the example that TeX "translates" variables to output letters not at output time but at definition time. But I don't really know what the different "translations" that occur are and what they are good for.

• It's not a problem with fontenc, but a more basic one: unless you change the standard setup with a “key-value” package, options to packages are fully expanded and the accent command \  doesn't survive this. – egreg Nov 2 '15 at 8:44
• But how can this expansion go wrong? I mean, at the end my goal IS expansion of the  command. And within the package I do have the right fontenc, so shouldn't it be expanded in the correctly accented ù? – Bananach Nov 2 '15 at 8:55
• The \a command has to do assignments in order to end with the instructions for printing à, so it doesn't survive the “full expansion” step. Expansion refers to macros, but typesetting commands and assignments of values are not executed during macro expansion. – egreg Nov 2 '15 at 9:00
• I do not quite understand this. No matter, what exactly is expanded during the expansion, what exactly is the reason that in one instance (fontenc in the main document) the expansion yields the desired result, and in the other instance (fontenc only in the package definition, before the use of pgfkeys) the result is not what i want? – Bananach Nov 2 '15 at 9:53
• To me it seems that the problem is that fontenc is not yet changed at the point where I pass keys to the package. How can changing the setup of the key-value package affect anything at that point? The key-value handling only comes into play in the MP package, after the fontenc has already been changed. – Bananach Nov 2 '15 at 9:56

I'm using as example the \' accent instead of yours for easier markdown input.

In OT-encoding the accent command \' is defined as

 \DeclareTextAccent{\'}{OT1}{19}


So it puts at the end an accent from position 19 over an character a.

In T1-encoding (t1enc.def) one find this definition:

 \DeclareTextComposite{\'}{T1}{a}{225}


This means at the end \'a is mapped to the glyph at position 225.

In your code the accent command is expanded in the package option and so you are somewhere in the middle on the way to the output. You can visualize the state by using \edef and \show:

\documentclass{report}
\usepackage[T1]{fontenc}

\begin{document}
\edef\test{\'a}\show\test

\fontencoding{OT1}\selectfont
\edef\test{\'a}\show\test

\end{document}


This gives for the first show (T1 encoding is active)

> \test=macro:
->á.
l.37 \edef\test{\'a}\show\test


and for the second

> \test=macro:
->\unhbox \voidb@x \bgroup \let \unhbox \voidb@x \setbox \@tempboxa \hbox {a\gl
obal \mathchardef \accent@spacefactor \spacefactor }\accent 19 a\egroup \spacef
actor \accent@spacefactor .
l.40 \edef\test{\'a}\show\test

• The second \test you show looks so much more familiar to me:-) – David Carlisle Nov 2 '15 at 9:29
• So, basically, the OP seems to ignore that the expansion provided by fontenc varies according to the font encoding: it is different in OT1, T1 or LY1, for instance. – erreka Nov 2 '15 at 21:46