Changing the font based on availability to fontspec












5















I would like to distribute a class that uses a professional font loaded via fontspec, and falls back to one of the default fonts in TeX Live if this is not available. I know that one can detect fonts packaged with TeX Live:



documentclass{article}
usepackage{fontspec}
IfFileExists{tgpagella.sty}{setmainfont{TeX Gyre Pagella}}{}

begin{document}

fontnamefont at thefontdimen6font

end{document}


This doesn't work, however, if one instead tries to detect a font file in the same directory:



documentclass{article}
usepackage{fontspec}
IfFileExists{texgyrepagella-regular.otf}{setmainfont{TeX Gyre Pagella}}{}

begin{document}

fontnamefont at thefontdimen6font

end{document}


Is there a mechanism somewhere to include .otf files in what IfFileExists is able to detect?










share|improve this question























  • fontspec has a fallback mechanism these days older answer but Will added a proper interface to that if I recall correctly

    – David Carlisle
    Jan 21 at 16:38








  • 1





    fontspec_font_if_exist:nTF and similar commands look to be the right thing....

    – David Carlisle
    Jan 21 at 16:43






  • 1





    @DavidCarlisle the documentation suggests IfFontExistsTF.

    – Ulrike Fischer
    Jan 21 at 16:44






  • 5





    @UlrikeFischer but it looks much more impressive with _ and :

    – David Carlisle
    Jan 21 at 16:45











  • Brilliant; thank you both!

    – Andrew Dunning
    Jan 21 at 16:46
















5















I would like to distribute a class that uses a professional font loaded via fontspec, and falls back to one of the default fonts in TeX Live if this is not available. I know that one can detect fonts packaged with TeX Live:



documentclass{article}
usepackage{fontspec}
IfFileExists{tgpagella.sty}{setmainfont{TeX Gyre Pagella}}{}

begin{document}

fontnamefont at thefontdimen6font

end{document}


This doesn't work, however, if one instead tries to detect a font file in the same directory:



documentclass{article}
usepackage{fontspec}
IfFileExists{texgyrepagella-regular.otf}{setmainfont{TeX Gyre Pagella}}{}

begin{document}

fontnamefont at thefontdimen6font

end{document}


Is there a mechanism somewhere to include .otf files in what IfFileExists is able to detect?










share|improve this question























  • fontspec has a fallback mechanism these days older answer but Will added a proper interface to that if I recall correctly

    – David Carlisle
    Jan 21 at 16:38








  • 1





    fontspec_font_if_exist:nTF and similar commands look to be the right thing....

    – David Carlisle
    Jan 21 at 16:43






  • 1





    @DavidCarlisle the documentation suggests IfFontExistsTF.

    – Ulrike Fischer
    Jan 21 at 16:44






  • 5





    @UlrikeFischer but it looks much more impressive with _ and :

    – David Carlisle
    Jan 21 at 16:45











  • Brilliant; thank you both!

    – Andrew Dunning
    Jan 21 at 16:46














5












5








5








I would like to distribute a class that uses a professional font loaded via fontspec, and falls back to one of the default fonts in TeX Live if this is not available. I know that one can detect fonts packaged with TeX Live:



documentclass{article}
usepackage{fontspec}
IfFileExists{tgpagella.sty}{setmainfont{TeX Gyre Pagella}}{}

begin{document}

fontnamefont at thefontdimen6font

end{document}


This doesn't work, however, if one instead tries to detect a font file in the same directory:



documentclass{article}
usepackage{fontspec}
IfFileExists{texgyrepagella-regular.otf}{setmainfont{TeX Gyre Pagella}}{}

begin{document}

fontnamefont at thefontdimen6font

end{document}


Is there a mechanism somewhere to include .otf files in what IfFileExists is able to detect?










share|improve this question














I would like to distribute a class that uses a professional font loaded via fontspec, and falls back to one of the default fonts in TeX Live if this is not available. I know that one can detect fonts packaged with TeX Live:



documentclass{article}
usepackage{fontspec}
IfFileExists{tgpagella.sty}{setmainfont{TeX Gyre Pagella}}{}

begin{document}

fontnamefont at thefontdimen6font

end{document}


This doesn't work, however, if one instead tries to detect a font file in the same directory:



documentclass{article}
usepackage{fontspec}
IfFileExists{texgyrepagella-regular.otf}{setmainfont{TeX Gyre Pagella}}{}

begin{document}

fontnamefont at thefontdimen6font

end{document}


Is there a mechanism somewhere to include .otf files in what IfFileExists is able to detect?







fonts fontspec






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 21 at 16:16









Andrew DunningAndrew Dunning

578214




578214













  • fontspec has a fallback mechanism these days older answer but Will added a proper interface to that if I recall correctly

    – David Carlisle
    Jan 21 at 16:38








  • 1





    fontspec_font_if_exist:nTF and similar commands look to be the right thing....

    – David Carlisle
    Jan 21 at 16:43






  • 1





    @DavidCarlisle the documentation suggests IfFontExistsTF.

    – Ulrike Fischer
    Jan 21 at 16:44






  • 5





    @UlrikeFischer but it looks much more impressive with _ and :

    – David Carlisle
    Jan 21 at 16:45











  • Brilliant; thank you both!

    – Andrew Dunning
    Jan 21 at 16:46



















  • fontspec has a fallback mechanism these days older answer but Will added a proper interface to that if I recall correctly

    – David Carlisle
    Jan 21 at 16:38








  • 1





    fontspec_font_if_exist:nTF and similar commands look to be the right thing....

    – David Carlisle
    Jan 21 at 16:43






  • 1





    @DavidCarlisle the documentation suggests IfFontExistsTF.

    – Ulrike Fischer
    Jan 21 at 16:44






  • 5





    @UlrikeFischer but it looks much more impressive with _ and :

    – David Carlisle
    Jan 21 at 16:45











  • Brilliant; thank you both!

    – Andrew Dunning
    Jan 21 at 16:46

















fontspec has a fallback mechanism these days older answer but Will added a proper interface to that if I recall correctly

– David Carlisle
Jan 21 at 16:38







fontspec has a fallback mechanism these days older answer but Will added a proper interface to that if I recall correctly

– David Carlisle
Jan 21 at 16:38






1




1





fontspec_font_if_exist:nTF and similar commands look to be the right thing....

– David Carlisle
Jan 21 at 16:43





fontspec_font_if_exist:nTF and similar commands look to be the right thing....

– David Carlisle
Jan 21 at 16:43




1




1





@DavidCarlisle the documentation suggests IfFontExistsTF.

– Ulrike Fischer
Jan 21 at 16:44





@DavidCarlisle the documentation suggests IfFontExistsTF.

– Ulrike Fischer
Jan 21 at 16:44




5




5





@UlrikeFischer but it looks much more impressive with _ and :

– David Carlisle
Jan 21 at 16:45





@UlrikeFischer but it looks much more impressive with _ and :

– David Carlisle
Jan 21 at 16:45













Brilliant; thank you both!

– Andrew Dunning
Jan 21 at 16:46





Brilliant; thank you both!

– Andrew Dunning
Jan 21 at 16:46










1 Answer
1






active

oldest

votes


















9














Recent fontspec releases allow you to test for a font being loadable with IfFontExistsTF or its internal expl3 version fontspec_font_if_exist:nTF:



documentclass{article}
usepackage{fontspec}
IfFontExistsTF{texgyrepagella-regular.otf}{setmainfont{TeX Gyre Pagella}}{}

begin{document}

fontnamefont at thefontdimen6font

end{document}





share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "85"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471157%2fchanging-the-font-based-on-availability-to-fontspec%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9














    Recent fontspec releases allow you to test for a font being loadable with IfFontExistsTF or its internal expl3 version fontspec_font_if_exist:nTF:



    documentclass{article}
    usepackage{fontspec}
    IfFontExistsTF{texgyrepagella-regular.otf}{setmainfont{TeX Gyre Pagella}}{}

    begin{document}

    fontnamefont at thefontdimen6font

    end{document}





    share|improve this answer






























      9














      Recent fontspec releases allow you to test for a font being loadable with IfFontExistsTF or its internal expl3 version fontspec_font_if_exist:nTF:



      documentclass{article}
      usepackage{fontspec}
      IfFontExistsTF{texgyrepagella-regular.otf}{setmainfont{TeX Gyre Pagella}}{}

      begin{document}

      fontnamefont at thefontdimen6font

      end{document}





      share|improve this answer




























        9












        9








        9







        Recent fontspec releases allow you to test for a font being loadable with IfFontExistsTF or its internal expl3 version fontspec_font_if_exist:nTF:



        documentclass{article}
        usepackage{fontspec}
        IfFontExistsTF{texgyrepagella-regular.otf}{setmainfont{TeX Gyre Pagella}}{}

        begin{document}

        fontnamefont at thefontdimen6font

        end{document}





        share|improve this answer















        Recent fontspec releases allow you to test for a font being loadable with IfFontExistsTF or its internal expl3 version fontspec_font_if_exist:nTF:



        documentclass{article}
        usepackage{fontspec}
        IfFontExistsTF{texgyrepagella-regular.otf}{setmainfont{TeX Gyre Pagella}}{}

        begin{document}

        fontnamefont at thefontdimen6font

        end{document}






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 21 at 19:23









        Andrew Dunning

        578214




        578214










        answered Jan 21 at 17:26









        David CarlisleDavid Carlisle

        490k4111321881




        490k4111321881






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471157%2fchanging-the-font-based-on-availability-to-fontspec%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Probability when a professor distributes a quiz and homework assignment to a class of n students.

            Aardman Animations

            Are they similar matrix