3

I'm trying to define a command that should give different outputs depending on the currently active font, especially depending on if the font is a monospace or serif/sans serif font.

I know there's \the\font to get the currently active font, which in LaTeX gives something like \OT1/cmr/m/n/10 or \OT1/cmtt/m/n/10. This can be split to extract the cmr/cmtt part and test against this string explicitly. However, I'm wondering if there's a more flexible approach that doesn't require hardcoding the font name.

Using pdflatex, is there a way to test whether the current font is any monospace font? If not, is there a built-in option to test if the current font is equal to the default monospace font?

siracusa
  • 13,411
  • 5
    For the default font you can test \f@family against \ttdefault. For a general monotony check if i and m have the same width. – Ulrike Fischer Jul 13 '19 at 14:18
  • 2
    To do the check that @UlrikeFischer suggests about i and m try: \noindent iiiiiiiiii.\\mmmmmmmmmm. inside your document environment and check if the width is the same. – koleygr Jul 13 '19 at 15:14
  • @UlrikeFischer Do you want to write up an answer? – siracusa Jul 14 '19 at 12:48

1 Answers1

1

As mentioned in the comments, both tests can be performed quite easily.

To test for the default monospace font, the expansion of the \f@family macro can be compared to the expansion of \ttdefault. Note that both macros differ in their prefixes (\long in this case), which is especially important when \ifx is used.

Testing for any monospace font can be done by filling two hboxes with a single character each, where either would have a different width in a proportional font, and comparing the boxes' widths afterwards. . and M seem reasonable choices to me.

The following code demonstrates the idea by defining tests \ifttdefault and \iftt:

\documentclass{article}

\makeatletter

\newcommand\ifttdefault{%
    \edef\@tempa{\f@family}%
    \edef\@tempb{\ttdefault}%
    \ifx\@tempa\@tempb
        \expandafter\@firstoftwo
    \else
        \expandafter\@secondoftwo
    \fi
}

\newcommand\iftt{%
    \begingroup
    \setbox0=\hbox{.}%
    \setbox1=\hbox{M}%
    \ifdim\wd0=\wd1
        \expandafter\endgroup\expandafter\@firstoftwo
    \else
        \expandafter\endgroup\expandafter\@secondoftwo
    \fi
}

\makeatother

\begin{document}
\frenchspacing

Default monospace font: \ifttdefault{yes}{no}\par
\texttt{Default monospace font: \ifttdefault{yes}{no}}\par
{\fontfamily{txtt}\selectfont Default monospace font: \ifttdefault{yes}{no}}\par
\medskip

Any monospace font: \iftt{yes}{no}\par
\texttt{Any monospace font: \iftt{yes}{no}}\par
{\fontfamily{txtt}\selectfont Any monospace font: \iftt{yes}{no}}\par

\end{document}

enter image description here

siracusa
  • 13,411
  • Out of curiosity, why do you use \frenchspacing here? (I know what it does in general, I just don't see what it illustrates here.) – LokiRagnarok Oct 09 '19 at 07:46
  • @LokiRagnarok With the default setting the space after : in the monospace fonts is much bigger than with the proportional font, which is what I wanted to get rid of. It's completely unrelated to the question, though, just a personal obsession with details. :-) – siracusa Oct 09 '19 at 23:11