14

I have seen Change \ttfamily font for entire document, and this is sort of a follow up to this.

I'd like to use a "palatino" like font (tgpagella) - but I'd also like to use {lmodern}'s lmtt as the teletype (typewriter) font. Consider this MWE:

\documentclass[10pt]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{lmodern}            % have lmtt loaded; but...
\usepackage{tgpagella}          % use "palatino" as main font, ...
\renewcommand{\ttdefault}{lmtt} % and use lmtt for teletype family

\begin{document}
testing %
\makeatletter % must be wrapping outside the \texttt; not just the \typeout inside the \texttt !
\texttt{testing %
  \typeout{ttdefault is \ttdefault, \f@size ; \meaning\f@size} %
}
\makeatother %
t\texttt{t}e\texttt{e}s\texttt{s}t\texttt{t}i\texttt{i}n\texttt{n}g\texttt{g}
\end{document}

The output is:

test.png

... and it should be visible that lmtt teletype is just slightly smaller than the tgpagella roman. Correspondingly, I'd like to increase just the size of the teletype font; which by the \typeout of \f@size, I gather, is:

ttdefault is lmtt, 10; macro:->10

... the same as the one, set in the \documentclass.

So my questions are:

  • Am I reading (or rather, \typeout-ing) the current font size of the teletype font correctly (i.e. is just the \f@size inside \texttt enough)?
  • How can I change just the size of the teletype font document-wide?
  • 2
    \usepackage{etoolbox} \appto\ttfamily{\fontsize{11pt}{12pt}\selectfont}? – Manuel Feb 4 '15 at 23:32
  • Thanks for that, @Manuel - so I should basically redefine \ttfamily? That seems to work for me, except \fontsize{10.5pt}{12.0pt} seems to be good enough for me (I printed the default the \f@baselineskip to get at the "12.0pt"; not sure if this is correct either). Feel free to post this as an asnwer - cheers! – sdaau Feb 4 '15 at 23:44
10

You may scale up Latin Modern Mono or down TeX Gyre Pagella. The latter is easier, because tgpagella accepts a scale option.

\documentclass[10pt]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[scale=0.95877]{tgpagella} % use "palatino" as main font, ...
\renewcommand{\ttdefault}{lmtt}       % and use lmtt for teletype family

\begin{document}
testing %
t\texttt{t}e\texttt{e}s\texttt{s}t\texttt{t}i\texttt{i}n\texttt{n}g\texttt{g}

\sbox0{e}\the\ht0

\sbox0{\ttfamily e}\the\ht0

\end{document}

As you see, the height of e is the same in the text font and in the mono font. The shapes are not the same (Palatino has quite wide overshoot), but you get the idea.

enter image description here

How did I determine the scaling factor? Trial and error.

  • Thanks for that @egreg - but there may be a case where I've "inherited" the roman font properties from an extern document class (so I wouldn't want to scale the roman font); there may be a case where I'm using fonts where neither the roman nor the monospaced fonts support a scale option. How would one proceed in that case - should \appto\ttfamily... as in Manuel's comment be used? – sdaau Feb 5 '15 at 0:02
  • 1
    @sdaau No easy solution for that; you have to copy the t1lmtt.fd file adding a scale factor. The \appto strategy can't work. I'd probably use a different monospaced font, the serifs of lmtt don't go along with those of Palatino. – egreg Feb 5 '15 at 0:03
  • Thanks @egreg - can you please just note one example pitfall with the \appto strategy as a reminder ( otherwise I might feel compelled to try it at a later date, should I bump into the same with different fonts :) ) Cheers! – sdaau Feb 5 '15 at 0:06
  • 1
    @sdaau If you do \xapptocmd{\ttfamily}{\fontsize{10.5}{12}\selectfont} as suggested (with xpatch), the font will be wrong when \ttfamily is used at a different size (say in footnotes or in section titles). – egreg Feb 5 '15 at 0:07
8

Since the TeX Gyre Pagella and Latin Modern font families are both available in Opentype format (and, incidentally, are produced by the same digital foundry), you may want to consider switching to either XeLaTeX or LuaLaTeX and using the fontspec package to load the fonts. The fontspec package provides the option Scale=MatchLowercase to automate the process of scaling fonts.

enter image description here

\documentclass[10pt]{book}
\usepackage{fontspec,unicode-math}
\setmainfont{TeX Gyre Pagella}
\setmonofont[Scale=MatchLowercase]{Latin Modern Mono}  % equate the x-heights of the fonts
\setmathfont{TeX Gyre Pagella Math}
\begin{document}
testing 
\texttt{testing}
t\texttt{t}e\texttt{e}s\texttt{s}t\texttt{t}i\texttt{i}n\texttt{n}g\texttt{g}
\end{document}
5

If @mico recommend you to switch to XeLaTeX then I can recommend you to switch to plain TeX and OPmac. Example:

\input opmac
\input cs-pagella

normal: t{\tt t}e{\tt e}s{\tt s}t{\tt t}i{\tt i}n{\tt n}g{\tt g}

\def\tt{\tentt\thefontscale[1050]} % correction of \tt font

corrected: t{\tt t}e{\tt e}s{\tt s}t{\tt t}i{\tt i}n{\tt n}g{\tt g}

\typosize[12/14] % the correction is valid for all sizes

corrected: t{\tt t}e{\tt e}s{\tt s}t{\tt t}i{\tt i}n{\tt n}g{\tt g}

\bye!

ttcorr

  • 1
    I like your recommendation/s. It is good to have a strong advocate of plain tex in the site. +1. – user11232 Feb 5 '15 at 8:29
  • Thanks @wipet - I had no idea it would look so relatively easy to do in plain tex - cheers! – sdaau Feb 8 '15 at 17:30

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.