4

According to this answer,

You can access the current font using the \font command, i.e. using \the\font which expand to e.g. \OT1/cmr/m/n/10 for the normal Computer Modern font.

Then why does the following LaTeX manuscript, when compiled with pdfTeX (TeXLive 2017), produce no pdf file?

\documentclass{article}
\begin{document}
\the\font
\end{documen‌​t}
  • 2
    \the\font doesn't produce any output; it has never. – egreg Jun 22 '17 at 19:40
  • 1
    It expands to the command \OT1/cmr/m/n/10 which selects that font not to the verbatim text \OT1/cmr/m/n/10 – David Carlisle Jun 22 '17 at 19:43
  • 1
    in plain tex it is as if it said \the\font expands to \rm it does not mean it makes the letters backslash r m it means it expands to the command \rm which selects a roman font – David Carlisle Jun 22 '17 at 19:44
  • 2
    \expandafter\meaning\the\font – David Carlisle Jun 22 '17 at 19:47
  • 1
    @DavidCarlisle: \expandafter\meaning\the\font yields, for example, select font cmtt10, where cmtt10 is the external (that is, filesystem’s) name of the font; to get something of the form \OT1/cmtt/m/n/10, say, I’d rather suggest \expandafter\string\the\font. – GuM Jun 22 '17 at 22:53
5

It turned out that the OP’s preference goes to \expandafter\string\the\font.

The following (compilable) example compares three approaches that have been proposed:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}



\begin{document}

Compare
\begin{center}
    \expandafter\meaning\the\font
\end{center}
with
\begin{center}
    \expandafter\string\the\font
\end{center}
and with
\begin{center}
    \fontname\font
\end{center}

\end{document}

The output is:

Output of the code


Addendum

Let me add a few remark, just to clarify a couple of points.

As I already observed in a comment, in the so-called New Font Selection Scheme (NFSS), which is part of LaTeX2e, the fact that the control sequence for selecting a font has the form

\<encoding>/<family>/<series>/<shape>/<size>

is not a “happy incident”, but a precise design principle on which the NFSS itself relies heavily.

Simply think of how a typical “low level” font selection like

\fontfamily{cmdh}\fontseries{m}\fontshape{n}%
\selectfont

works: first, the three declarations in the first line set the corresponding internal macros \f@family, \f@series, and \f@shape; then, the \selectfont command uses the updated values to build, and subsequently invoke, the appropriate “font-selecting” control sequence. Indeed, the LaTeX2e kernel defines \fontfamily, \fontseries, and \fontshape as follows:

\DeclareRobustCommand\fontfamily[1]{\edef\f@family{#1}}
\DeclareRobustCommand\fontseries[1]{\edef\f@series{#1}}
\DeclareRobustCommand\fontshape [1]{\edef\f@shape{#1}}

\fontencoding and \fontsize are a bit more complicated, but among the other things they do they set, in a similar way, the internal macros \f@encoding and \f@size. On the other hand, the definition of \selectfont is

\DeclareRobustCommand\selectfont
        {%
    \ifx\f@linespread\baselinestretch \else
      \set@fontsize\baselinestretch\f@size\f@baselineskip \fi
    \xdef\font@name{%
      \csname\curr@fontshape/\f@size\endcsname}%
    \pickup@font
    \font@name
    \size@update
    \enc@update
    }

The lines that are relevant to our discussion are

    \xdef\font@name{%
      \csname\curr@fontshape/\f@size\endcsname}%
    \pickup@font
    \font@name

As you can see, the macro \font@name is made to expand to a control sequence whose name is obtained by the concatenation of the full expansion of \curr@fontshape, a /, and the full expansion of \f@size; since \curr@fontshape is (statically) defined as

\def\curr@fontshape{\f@encoding/\f@family/\f@series/\f@shape}

and since this definition gets fully expanded, we see that this process indeed yields a control sequence of the form

\T1/cmdh/m/n/10

(for example). The \pickup@font macro ensures that this control name is defined and that it loads the correct external font (this is where the real “hard work” takes place!), after which the font selector that has just been constructed is invoked by executing \font@name.

We can add that \size@update and \enc@update are simply “hooks” where actions that need to be executed when the font size and/or encoding, respectively, have changed can be stored, and that the two lines of code that mention \baselinestretch take into account the possibility that the user has directly modified this obsolete LaTeX2.09 style parameter.

All that said, and following a suggestion from the OP, we can supplement our original example with a couple of other methods to obtain information about the current font:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}



\begin{document}

Compare
\begin{center}
    \expandafter\meaning\the\font
\end{center}
with
\begin{center}
    \expandafter\string\the\font
\end{center}
and with
\begin{center}
    \fontname\font
\end{center}
Other methods that use internal commands follow.

\makeatletter

Taking font components apart:
\begin{center}
    \begin{tabular}{ll}
        Encoding: & \f@encoding  \\
        Family:   & \f@family    \\
        Series:   & \f@series    \\
        Shape:    & \f@shape     \\
        Size:     & \f@size
    \end{tabular}
\end{center}
All in a row:
\begin{center}
    \selectfont % needed in order to get "\font@name" in sync
    \expandafter\string\font@name
\end{center}

\makeatother

\end{document}

The corresponding output is:

Output of the second code example

  • Sorry, I deleted the comment that you linked to at the beginning of your answer before I realized that you had posted an answer. The reason why I deleted it was that I realized (please correct me if I'm wrong) that it is a happy accident that the name of the command for switching to the Computer Modern font contains the entire font specifications (i.e encoding, family, series, shape, and size), but it can happen for some other font that not all this information will be encapsulated in the switching command's name, in which case the method you described won't give me the information I desire. – Evan Aad Jun 23 '17 at 1:45
  • You might as well add to your answer the fourth method proposed by me (copied from Mike Renfro) in the comment to egreg's reply... (which is the most reliably informative method of them all) – Evan Aad Jun 23 '17 at 1:50
  • Or maybe the method I described in the comment to egreg's answer is information-equivalent to your method in that what the \f@... macros do is simply parse the font name string and break it down to its components? – Evan Aad Jun 23 '17 at 2:21
  • 2
    @Evan the other direction: latex constructs the csname from the encoding, family, shape, etc – David Carlisle Jun 23 '17 at 6:12
  • @EvanAad: “it is a happy accident that the name of the command for switching to [a] font contains the entire font specifications”. For fonts selected through the NFSS, it is not a “happy incident”, it is so by design: indeed, the LaTeX2e kernel provides macros both for extracting the encoding, family, etc. from the font selector (i.e., the csname) and for assembling a font selector from the separate specifications. Perhaps you are referring to fonts that are not selected through the NFSS; but in that case, do the macros \f@encoding, \f@family, etc. still make sense? – GuM Jun 23 '17 at 21:10
5

The usage of \the\font is to define a control sequence that expands to the current font selector.

So \edef\thisfont{\the\font} would make \thisfont expand to, say, \tenrm.

Used by itself it just produces the font selector, hence no output.

You may also do

\expandafter\let\expandafter\thisfont\the\font

and \thisfont would become equivalent to the font selector for the current font.

Maybe you were meaning \fontname\font, that expands to the name of the TFM file corresponding to the current font.

  • Thanks. Actually, after some experimenting based on Mike Renfro's answer I've found out what I was after, namely the following code (to be inserted inside the body of a LaTeX document): \makeatletter encoding:\f@encoding{},family:\f@family{},series:\f@series{},shape:\f@shape{},size:\f@size{}\makeatother. – Evan Aad Jun 22 '17 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.