# Need help with font and siunitx issue?

I don't really understand anything about fonts, as much as I have tried to read about them. I ultimately just wanted a font that kinda looked like it was written in a typewriter style, which also had clear math symbols to read. I found an example somewhere here on the stackexchange (see link below), used pdflatex to compile it, I thought it looked ok. But some of the math fonts, when using siunitx look squashed in the inline math mode.

My minimum working example is this:

\documentclass[a4paper,openany]{book}

\usepackage[variablett]{lmodern}  % line 2
\renewcommand*\familydefault{\ttdefault} % line 3
\usepackage{siunitx}

\sisetup{
detect-all = true,
detect-inline-family = text,
detect-inline-weight = text
}

\begin{document}

\noindent normal text: [\SI{5}{\milli\meter\squared}] \\
inline math mode: $[\SI{5}{\milli\meter\squared}]$ \\
mm

\end{document}


If I just use the default fonts, i.e. remove line 2 and 3, then use siunitx, the "mm" looks like the same font as the text.

Can anyone help me prevent the text getting squashed like this, or suggest an alternative font that won't have this issue?

EDIT: I edited the MWE with \sisetup but was not able to fix the issue.

One option is to turn off automatic detection of the mode, and specify siunitx to always use text mode, which will use \texttt instead of \mathtt (which is the squished up one).

\documentclass[a4paper,openany]{book}

\usepackage[variablett]{lmodern}  % line 2
\renewcommand*\familydefault{\ttdefault} % line 3
\usepackage{siunitx}

\sisetup{
detect-mode=false,
mode=text,
}

\begin{document}

\noindent
normal text: [\SI{5}{\milli\meter\squared}] \\
inline math mode: $[\SI{5}{\milli\meter\squared}]$ \\
mm \\
$\mathtt{mm}$

\end{document}


You can re-declare the \mathtt.

\documentclass[a4paper,openany]{book}

\usepackage[variablett]{lmodern}  % line 2
\renewcommand*\familydefault{\ttdefault} % line 3
\usepackage{siunitx}

\sisetup{
detect-all = true,
detect-inline-family = text,
detect-inline-weight = text
}

\DeclareMathAlphabet{\mathtt}{OT1}{lmvtt}{m}{n}
\SetMathAlphabet{\mathtt}{bold}{OT1}{lmvtt}{b}{n}

\begin{document}

\noindent normal text: [\SI{5}{\milli\meter\squared}] \\
inline math mode: $[\SI{5}{\milli\meter\squared}]$ \\
mm

\noindent
{\boldmath mathtt in bold math: $\mathtt{mm}$}% testing


\end{document}

That we need to correct \mathtt is arguably a bug of lmodern.sty: it offers an option variablett which switches to lmvtt as typewriter font in place of lmtt but it still defines \mathtt to use lmtt because the declaration does not use \ttdefault.

If we look at alternative newtxtext/newtxtt/newtxmath we see that newtxmath does use \ttdefault in its declaration of \mathtt.

So the following works as expected:

\documentclass[a4paper,openany]{book}
\usepackage[T1]{fontenc}
\usepackage{newtxtext}
\usepackage[nomono, ttdefault]{newtxtt}% tt with variable stretch/shrink
\usepackage{newtxmath}
\usepackage{siunitx}

\sisetup{
detect-all = true,
detect-inline-family = text,
detect-inline-weight = text
}

\begin{document}
\noindent normal text: [\SI{5}{\milli\meter\squared}] \\
inline math mode: $[\SI{5}{\milli\meter\squared}]$ \\
mm

\noindent
{\boldmath mathtt in bold math: $\mathtt{mm}$}% testing

\end{document}


On the other hand the ttzdefault option of newtxtt does not redefine \ttdefault, so we would still need a \let\ttdefault\ttzdefault before loading newtxmath then. Anyway, the nomono, ttdefault options did the job.

And the typewriter font will allow hyphenation and text justification.

This is similar to the lmodern "variablett" but lmodern additionally has variable lengths glyphs (each among a..z has its own width), its m in lmvtt is not squeezed contrarily to the one from newtxtt package even with nomono option.

• Yeah I am confused about mathtt. Does it only change inline math font? I haven't used it anywhere so what should I do? I normally just do $a + b$ etc or use an equation environment. The font packages I've used in my report are the ones I gave in the MWE. Is this the correct practice? – slew123 Sep 16 '18 at 11:50
• This problem arises only in siunitx context: it appears in math mode, with the options you passed, it uses \mathtt. After loading lmodern this \mathtt will use lmtt, i.e. the normal monospaced lmodern font. But you loaded lmodern with option variablett, so for \mathtt to match the actual "tt" font, we need to correct it to use lmvtt where v stands for variable. It is arguably a bug of lmodern package, because as you discovered the m glyph looks quite different in the two fonts. You may try to report to lmodern auth. (I did some other bug report years ago to no avail). – user4686 Sep 16 '18 at 12:12