6

I'm very new to ConTeXt, so please excuse the newbie question. I'm trying to load a font from a file. The .otf files are stored in a directory named fonts/ which is in the directory of the .tex file I'm trying to compile.

This code does not work, and reverts to the default font:

\definefontfamily [mainface][serif] [My Font]
                  [tf=file:./fonts/MyFont.otf,
                   it=file:./fonts/MyFont-It.otf,
                   bf=file:./fonts/MyFont-Bd.otf,
                   bi=file:./fonts/MyFont-BdIt.otf]

\setupbodyfont[mainface]                   

\starttext
\title{Hello, world!}

Hello World! \emph{Hi!}

\stoptext

However, if I copy the .otf files into the same directory as the .tex file, and write tf=file:MyFont.otf etc, then everything works fine.

How do I specify a relative path to the font, inside the .tex file?

In LaTeX, I would use fontspec package and do the following:

\setmainfont {MyFont.otf}
             [Path        =./fonts/,
              ItalicFont  = MyFont-It.otf]

However, I couldn't find the equivalent syntax for \definefontfamily in ConTeXt.

Just to mention, I am using ConTeXt version: 2019.03.21 21:39 as part of TeX Live, on Manjaro Linux.

6
  • Wouldn't it be better to have this font installed for any *tex file? Put it in your ~SHOME/texmf/fonts/ directory and run in your console mtxrun --script fonts --reload and then mtxrun --generate
    – sztruks
    Nov 15, 2019 at 17:23
  • @sztruks Perhaps, but I also need an option like this for portability (I want other people to be able to compile the .tex file without having to install fonts). For now, I keep these fonts in the same directory, because it's the only way I can get the file to compile, but it's a total mess... I can't believe that what I'm asking is not possible, it seems like such a trivial issue.
    – Kresimir
    Nov 15, 2019 at 17:36
  • 1
    It is certainly possible, but I am not used to playing with fonts. Just wait a bit, very competent folks will have a look around here within several hours.
    – sztruks
    Nov 15, 2019 at 17:50
  • 4
    No, absolute paths do not work, either. I did not try this Lua magic, but I did try to type the path in full.
    – Kresimir
    Nov 16, 2019 at 3:54
  • 3
    Ask on the context mailing list. Nov 19, 2019 at 8:45

1 Answer 1

3

Following the advice in the comments, I asked this question on the ConTeXt mailing list and got a reply from Hans Hagen. The solution is to add:

\ctxlua{os.setenv("RUNTIMEFONTS","./fonts")}

After that

\definefontfamily [mainface][serif] [My Font]
                  [tf=file:MyFont.otf,
                   it=file:MyFont-It.otf,
                   bf=file:MyFont-Bd.otf,
                   bi=file:MyFont-BdIt.otf]

works as expected.

However, I was also warned that this leaves garbage in the cache that can affect other projects that use different fonts with the same name, so the solution is to either clear the cache or rename the fonts to have a unique name. Ideally, however, fonts should be installed in texmf/fonts/ directory, but when that is not possible (e.g. for portability reasons), the above should work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .