1

I need to resolve absolute path to fonts:

\setmainjfont[
    Path          = \currfileabsdir,
    UprightFont   = fonts/HGS_Mincho/HGRMB.ttc,
    BoldFont      = fonts/HGS_Mincho/HGRME.ttc
]{HGS Mincho}

The solution \usepackage[abspath]{currfile} requires -recorder option, but I don't use console, and setup -recorder option is potentially unnecessary routine.

Other solution based on lfs library, but if we just use it as shown below, error will occur (at least, in Windows):

\edef\currfileabsdir{\directlua{tex.sprint(lfs.currentdir())}/}
! Undefined control sequence.
l.1 C:\Users
          \i\Documents\TeX\LuaLaTeX_Development
l.6 ...sdir{\directlua{tex.sprint(lfs.currentdir())}
                                                  /}

It was recommended to use catcodes as shown below:

\edef\currfileabsdir{\directlua{tex.sprint(-2, lfs.currentdir())}/}

What really will be is backslashes will be removed from absolute path, so

C:Users/i/Documents/TeX/LuaLaTeX_Development/fonts/HGS_Mincho/HGRMB.ttc

becomes to

C:UsersmeDocumentsTeXLuaLaTeX_Development/fonts/HGS_Mincho/HGRMB.ttc

How I get correct absolute path to font?


Update: MWE with first suggested solution

\documentclass[a4paper, twoside]{ltjsarticle}

\usepackage{luatexja-fontspec}

\makeatletter
\edef\currfileabsdir{\directlua{tex.sprint(\the\catcodetable@string, lfs.currentdir())}/}
\makeatother

\setmainjfont[
    Path          = \currfileabsdir,
    UprightFont   = fonts/HGS_Mincho/HGRMB.ttc,
    BoldFont      = fonts/HGS_Mincho/HGRME.ttc
]{HGS Mincho}

\setsansjfont[
    Path          = \currfileabsdir,
    UprightFont   = fonts/HGS_Gothic/HGRGM.ttc,
    BoldFont      = fonts/HGS_Gothic/HGRGE.ttc,
]{HGS Gothic}


\begin{document}
  日本語文字  Latin letters Кириллица
\end{document} 

Error:

luaotfload | db : Reload initiated (formats: otf,ttf,ttc); reason: "File not found: C:UsersiDocumentsTeXLuaLaTeX_Development/fonts/HGS_Mincho/HGRMB.ttc.".

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! fontspec error: "font-not-found"
! 
! The font "fonts/HGS_Mincho/HGRMB.ttc" cannot be found.
! 
! See the fontspec documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

l.13 ]{HGS Mincho}

enter image description here

Henri Menke
  • 109,596

1 Answers1

2

Your code fails as the backslash used on windows in the path is in lua also the escape char. As the path is passed on to the lua backend one need to escape the backslash with another backslash. You can use \luaescapestring for this.

\edef\currfileabsdir{\luaescapestring{\directlua{tex.sprint(-2,lfs.currentdir())}}/}

Or if you want to use \currfileabsdir also in other places, add the \luaescapestring to the Path:

\edef\currfileabsdir{\directlua{tex.sprint(-2,lfs.currentdir())}/}
\setmainjfont[
   Path          = \luaescapestring{\currfileabsdir},
   ....
Ulrike Fischer
  • 327,261