I'm trying to change a latex template to use the Cooper Hewitt font for section headings. (I'm using pandoc to convert markdown to pdf, by tweaking the built-in pandoc template, and using the xelatex engine.)
For some bizarre reason, however, when I try to change the font on the headings, it always comes out in italics unless I hard-code the path to the specific style of the font I want---which seems super-brittle, and can't possibly be the only way to do this, right?
Stuff that doesn't work:
Here's my basic attempt. I put the following three lines in my template file, after all other package declarations and such, and before the title is declared:
\usepackage{sectsty}
\setsansfont{Cooper Hewitt}
\allsectionsfont{\sffamily}
Cooper Hewitt is installed in the ordinary fashion on my (MacOS) system (shows up in Font Book, which doesn't report any errors, etc.). But, for some reason, the headers become italics. After some investigating (see below), the best I can discern is that XeLaTeX just can't figure out which version of the installed font is which.
I've tried changing the last line to \allsectionsfont{\sffamily\upshape} and \allsectionsfont{\upshape\sffamily}, but that makes no difference, it still comes out italics.
When I remove the specific font declaration, i.e.,
\usepackage{sectsty}
\allsectionsfont{\sffamily}
then the section font correctly comes out non-italic and in whatever the default sans serif font is (some kind of computer modern sans?). So the problem seems to only be introduced when I specify the particular sans serif font I want to use.
I've even tried
\usepackage{sectsty}
\setsansfont{Cooper Hewitt}
\allsectionsfont{\sffamily}
\allsectionsfont{\upshape}
but that just reverts the whole thing, and I end up with section headings in serif computer modern.
I've tried a different custom sans font, i.e., \setsansfont{Fira Sans} and that works ok (i.e., doesn't come out italic), but I don't want Fira Sans, I want Cooper Hewitt...
I've also tried specifying the particular subtype, i.e., \setsansfont{Cooper Hewitt Semibold} but then latex throws the following error:
kpathsea:make_tex: Invalid filename `Cooper Hewitt Semibold', contains ' '
Error producing PDF.
! Package fontspec Error: The font "Cooper Hewitt Semibold" cannot be found.
Following this SO I also tried:
\usepackage{sectsty}
\setsansfont{Cooper Hewitt}[
FontFace={sb}{n}{Font=* SemiBold}
]
\DeclareRobustCommand{\sbseries}{\fontseries{sb}\selectfont}
\allsectionsfont{\sbseries}
And this generated the same font not found error. (I also tried with "Semibold" instead of SemiBold, ditto.)
What works, but sucks
Finally, I tried simply hard-coding the absolute path to the specific weight in, loosely following this SO.
\usepackage{sectsty}
\newfontfamily\headerfont[Path=/Users/myuser/Library/Fonts/]{CooperHewitt-Semibold}
\allsectionsfont{\headerfont}
This finally, finally, bloody worked. But it seems a bit brittle, since I seem to have to hard-code specific font paths on every machine I use, or else bundle the fonts with every document I write, neither of which makes any sense at all (that's the operating system's job!).
Is there a better way? There must be a better way!
CooperHewitt-Semibold, notCooperHewitt Semibold. Changing that font option toFontFace={sb}{n}{Font=*-SemiBold}should work. If not,\setsansfont{CooperHewitt}[Extension = .otf, FontFace={sb}{n}{*-Semibold}]plus whichever ofUprightFont,ItalicFont, etc. it fails to detect automatically. – Davislor May 27 '20 at 21:01\allsectionsfont{\sffamily\sbseries\upshape}. – Davislor May 27 '20 at 21:02