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
    2 days ago








  • 1





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

    – David Carlisle
    2 days ago






  • 1





    @DavidCarlisle the documentation suggests IfFontExistsTF.

    – Ulrike Fischer
    2 days ago






  • 5





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

    – David Carlisle
    2 days ago











  • Brilliant; thank you both!

    – Andrew Dunning
    2 days ago
















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
    2 days ago








  • 1





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

    – David Carlisle
    2 days ago






  • 1





    @DavidCarlisle the documentation suggests IfFontExistsTF.

    – Ulrike Fischer
    2 days ago






  • 5





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

    – David Carlisle
    2 days ago











  • Brilliant; thank you both!

    – Andrew Dunning
    2 days ago














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 2 days ago









Andrew DunningAndrew Dunning

532212




532212













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

    – David Carlisle
    2 days ago








  • 1





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

    – David Carlisle
    2 days ago






  • 1





    @DavidCarlisle the documentation suggests IfFontExistsTF.

    – Ulrike Fischer
    2 days ago






  • 5





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

    – David Carlisle
    2 days ago











  • Brilliant; thank you both!

    – Andrew Dunning
    2 days ago



















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

    – David Carlisle
    2 days ago








  • 1





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

    – David Carlisle
    2 days ago






  • 1





    @DavidCarlisle the documentation suggests IfFontExistsTF.

    – Ulrike Fischer
    2 days ago






  • 5





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

    – David Carlisle
    2 days ago











  • Brilliant; thank you both!

    – Andrew Dunning
    2 days ago

















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

– David Carlisle
2 days ago







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

– David Carlisle
2 days ago






1




1





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

– David Carlisle
2 days ago





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

– David Carlisle
2 days ago




1




1





@DavidCarlisle the documentation suggests IfFontExistsTF.

– Ulrike Fischer
2 days ago





@DavidCarlisle the documentation suggests IfFontExistsTF.

– Ulrike Fischer
2 days ago




5




5





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

– David Carlisle
2 days ago





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

– David Carlisle
2 days ago













Brilliant; thank you both!

– Andrew Dunning
2 days ago





Brilliant; thank you both!

– Andrew Dunning
2 days ago










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 2 days ago









        Andrew Dunning

        532212




        532212










        answered 2 days ago









        David CarlisleDavid Carlisle

        486k4111231867




        486k4111231867






























            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

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]