TransWikia.com

Locally change the scale of the chosen (mono) font without specifying its name?

TeX - LaTeX Asked on July 17, 2021

Suppose a custom class which, thanks to package titlesec, prints the sections titles in uppercase. Suppose now the user of this class wants the mono font to be Scale = MatchLowercase. If a section title contains both main and mono fonts, the later will look too small (see MCEs below).

If the titleformat{section}{...} contains setmonofont[Scale = MatchUppercase]{⟨chosen mono font⟩} the size of the mono font will be OK but, obviously, the class cannot anticipate the ⟨chosen mono font⟩ by the user, so it is a dead end. So I tried with defaultfontfeatures[ttfamily]{Scale = MatchUppercase} which doesn’t needs to specify the ⟨chosen mono font⟩, but it does not have any effect.

So my question: is it possible to locally change the scale of the ⟨chosen mono font⟩ without specifying its name?

By default or with defaultfontfeatures[ttfamily]{Scale = MatchUppercase}: doesn’t look nice

begin{filecontents*}[overwrite]{myclass.cls}
ProvidesExplClass
  {myclass}
  {2021/06/08}
  {0.1}
  {
    My Nice Class
  }
NeedsTeXFormat{LaTeX2e}
LoadClass { article }
%
RequirePackage{fontspec}
RequirePackage{titlesec}
%
AddToHook{begindocument/before}{%
  titleformat{section}{
    normalfont
    Large
    % setmonofont[Scale = MatchUppercase]{RobotoMono}
    % defaultfontfeatures[ttfamily]{Scale = MatchUppercase}
    bfseries
    text_uppercase:n
  }{thesection}{1em}{}{}
}
end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
documentclass{myclass}

setmainfont{KpRoman}
setsansfont{KpSans}
setmonofont[Scale = MatchLowercase]{RobotoMono}

begin{document}

Footexttt{bar}OOf

section{Footexttt{bar}OOf}
end{document}

enter image description here

With setmonofont[Scale = MatchUppercase]{⟨chosen mono font⟩}: looks nice but not realistic

begin{filecontents*}[overwrite]{myclass.cls}
ProvidesExplClass
  {myclass}
  {2021/06/08}
  {0.1}
  {
    My Nice Class
  }
NeedsTeXFormat{LaTeX2e}
LoadClass { article }
%
RequirePackage{fontspec}
RequirePackage{titlesec}
%
AddToHook{begindocument/before}{%
  titleformat{section}{
    normalfont
    Large
    setmonofont[Scale = MatchUppercase]{RobotoMono}
    % defaultfontfeatures[ttfamily]{Scale = MatchUppercase}
    bfseries
    text_uppercase:n
  }{thesection}{1em}{}{}
}
end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
documentclass{myclass}

setmainfont{KpRoman}
setsansfont{KpSans}
setmonofont[Scale = MatchLowercase]{RobotoMono}

begin{document}

Footexttt{bar}OOf

section{Footexttt{bar}OOf}
end{document}

enter image description here

One Answer

One way to do this is to define a new font family, perhaps

newfontfamilyalttt{RobotoMono}[Scale=MatchUppercase]
DeclareTextFontCommandtextalttt{alttt}

This lets you write section{Footextalttt{bar}OOf}.

Alternatively, you could addfontfeature, as in:

DeclareTextFontCommandtextrescale{addfontfeature{Scale=MatchUppercase}}

You can then write section{Footexttt{textrescale{bar}}OOf}.

A more complex solution would be to keep the current monospace scaling in a variable, like ttscale, and redefine ttfamily to apply addfontfeature{Scale=ttscale}. Consider whether it is worth this.

Answered by Davislor on July 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP