# Set \sffamily if needed font is not installed

I have installed non standard font (ISOCPEUR.TTF) whiсh respond to \showfont command like this:

\newcommand{\showfont}{encoding: \f@encoding{},
family:   \f@family{},
series:   \f@series{},
shape:    \f@shape{},
size:     \f@size{}
}


The command \newcommand{\iso}{\fontfamily{iso}\selectfont} set "iso" font, but if such is not installed switch to computern modern.

I want to create a command which set "iso" font, but if such font is not installed, the command must set \sffamily font. I tried but my efforts have not been successful

\newcommand\f@family@iso{iso}
\newcommand{\iso}{\fontfamily{iso}\selectfont%
\ifx\f@family\f@family@iso%
%it's ok, do nothing
\else
\sffamily\slshape\selectfont
\fi
}


In any case such code set \sffamily font even if "iso" font is installed.

• I’m afraid I don’t understand: how do you make the iso font family known to LaTeX? Have you also installed a t2aiso.fd file? If so, what are its contents? And more generally, what do you mean when you claim you have “installed” the font? What files have you actually installed? Last (but actually first from a logical standpoint): which typesetting engine are you using? (pdfTeX/XeTeX/LuaTeX) – GuM Dec 16 '16 at 22:37
• @Gustavo Mezzetti: I use MikTeX 2.9 an have separate folder with corresponding content: MiKTeX ttfonts |--dvips ... |--fonts ... |--miktex ... |--tex |--latex |--microsoft |--isocpeur |--ot1iso.fd t1iso.fd t2aiso.fd |--ttf2tfm ... This folder is recognizeв by MiKTeX so I can create commnd '\newcommand{\iso}{\fontfamily{iso}\selectfont}' and use it when I need "iso" font. And I use pdfTeX engine. – Konstantin Dec 17 '16 at 5:16
• I have updated my TeXLive 2016 distribution just now, and I’m still unable to find a file named t2aiso.fd; so, I can safely assume that it is some non-standard font (exactly as you claim, after all! :-) . LaTeX’s New Font Selection Scheme (which is not actually new, dating back to the early 1990’s) provides the [s]sub “size function” exactly for the purpose of defining custom font substitutions; it’s easy to write an answer along those lines, but before doing so I’d like to have a look to the contents of the t2aiso.fd file: would you mind posting them? (It shouldn’t be a long file.) – GuM Dec 23 '16 at 23:37

On second thought, maybe you are just looking for a simple solution like the following:

\documentclass[a4paper]{article}
\usepackage[T2A,T1]{fontenc}
\usepackage{lipsum} % just to get the "Lorem ipsum..." text

\IfFileExists{t2aiso.fd}{
\newcommand*{\iso}{\fontencoding{T2A}\fontfamily{iso}\selectfont}
}{
\newcommand*{\iso}{\sffamily\slshape} % no "\selectfont" needed here
}

% The same thing for a font that should exist on all systems:
\IfFileExists{t1cmdh.fd}{
\newcommand*{\dunhill}{\fontencoding{T1}\fontfamily{cmdh}\selectfont}
}{
\newcommand*{\dunhill}{\sffamily\slshape}
}

\begin{document}

\begingroup
\iso
\lipsum[1]
\endgroup

\begingroup
\dunhill
\lipsum[2]
\endgroup

Back to default font.

\end{document}


# Addition: Run-time check for the presence of a TFM file

I’m adding the answer I had originally devised for this problem, because I think that, after all, it illustrates an interesting technique. This solution doesn’t rely on the presence of a .fd file that describes how to implement a given font family (in principle, this is not mandatory in order to be able to use a font), but checks directly for the existence of the external font, or, more exactly, of its TFM (TeX Font Metric) file.

The following source file should be compiled with the --restricted-shell-escape command-line option. Among other things, it implements a command with three mandatory arguments named \IfTFMFileExists, which queries (via the piped input feature) the system for the existence of the TFM file whose basename is passed in the first argument, and executes the code contained in the second argument if the file is found, and the code contained in the third argument if it is not. This command is subsequently used to choose between two \DeclareFontShape declarations, one that binds the given font shape to its intended external font (if the latter is present on the system), and the other that substitutes for it another font shape.

Note that the first time you compile this code, it will probably invoke mktextfm to build the lasi1000.tfm TFM file, that is necessary for the font used as a substitute for the iso family, that is, Computer Modern Sans in the T2A encoding, and which is not installed by default in the standard TeXLive distribution.

\documentclass[a4paper]{article}
\usepackage[T2A,T1]{fontenc}
\usepackage{lipsum} % just to get the "Lorem ipsum..." text

\makeatletter

% Mere diagnostic command:
\newcommand*\showfont{%
\begingroup
\def~{ }%
\typeout{+-----> On input line \number\inputlineno:}%
\typeout{|~~~Encoding:~~~\f@encoding,}%
\typeout{|~~~family:~~~~~\f@family,}%
\typeout{|~~~series:~~~~~\f@series,}%
\typeout{|~~~shape:~~~~~~\f@shape,}%
\typeout{|~~~size:~~~~~~~\f@size;}%
\typeout{|~~~>>> external (TFM file) name: \fontname\font.}%
\typeout{+-----------------------}%
\endgroup
}

\newcommand*\IfTFMFileExists[1]{%
% Actual usage:
\begingroup
\openin\@inputcheck=|"kpsewhich #1.tfm"\relax % Why "\relax"?
% (Exercise... ;-)
\ifeof\@inputcheck
\aftergroup\@secondoftwo % kpsewhich couldn't be called
\else
\endlinechar \m@ne
\ifx\@tempa\@empty
\aftergroup\@secondoftwo % kpsewhich returned nothing
\else
\aftergroup\@firstoftwo % kpsewhich returned something
\fi
\fi
\closein\@inputcheck
\endgroup
}

\makeatother

%------ Declarations for the ISOCPEUR family follow: -----------
%
% On the generic user's system, we expect NOT to find the external font.
\DeclareFontFamily{T2A}{iso}{}
% I'm pretending that the external (TFM file) name of the relevant font is
% "fiemo6a", and that it is a fully scalable font; but please keep in mind
% that I have no idea of what is the correct binding between the NFSS font
% shapes and the external fonts for this ISOCPEUR family!
\IfTFMFileExists{fiemo6a}{
\typeout{==> fiemo6a found!}
% BEWARE OF SPACES in the arguments of "\DeclareFontShape":
% they get prematurely tokenized here!
\DeclareFontShape{T2A}{iso}{m}{n}{<->fiemo6a}{}
}{
% As above.
\DeclareFontShape{T2A}{iso}{m}{n}{<->sub*cmss/m/sl}{}
}
%
%------ End of declarations for the ISOCPEUR family. -----------

%------ Declarations for the CM dunhill family follow: ---------
%
% This serves as a benchmark, to check that an external font that should be
% present on all system is actually recognized.
\DeclareFontFamily{T1}{cmdh}{}
% Assume that if "ecdh1000.tfm" is present on the system, the other sizes
% are there as well:
\IfTFMFileExists{ecdh1000}{
\typeout{==> ecdh1000 found!}
% As above.
\DeclareFontShape{T1}{cmdh}{m}{n}%
{<5><6><7><8><9><10><10.95><12><14.4>%
<17.28><20.74><24.88><29.86><35.83>genb*ecdh}{}%
}{
% As above.
\DeclareFontShape{T1}{cmdh}{m}{n}{<->sub*cmss/m/n}{}
}
%
%------ End of declarations for the CM dunhill family. ---------

\begin{document}

\showfont
Text in normal font.

\begingroup
\usefont{T2A}{iso}{m}{n}
\showfont
\lipsum[1]
\endgroup

\begingroup
\usefont{T1}{cmdh}{m}{n}
\showfont
\lipsum[2]
\endgroup

Back to normal font.

\end{document}


Obviously, you can avoid warnings about the substitution of Computer Modern Sans for the iso family by replacing the sub size function with ssub.

The code could also be packaged in the form of a .fd file, of course: you would need to include only the defintion of the \IfTFMFileExists command and the (conditional) font declarations themselves.

• It's better to use \DeclareTextFontCommand for this, instead of \newcommand, but it's a good solution nonetheless. – egreg Dec 24 '16 at 10:10
• @Gustavo Mezzetti It's exactly what I need. Thank you. And I made <code>*.fd<code> files by myself with this tutorial ttf_fontinst. I can share files for those who need ISOCPEUR font in LaTeX. How can I do this? – Konstantin Dec 24 '16 at 12:37
• @egreg: As I said, the OP’s intentions aren’t perfectly clear, but it seems that (s)he asked for a declaration like \sffamily rather than for a text command like \textsf. Besides, I had originally interpreted the question as “How to make the .fd file automatically substitute cmss if the .tfm file for the required font is missing?”, and I had therefore devised a solution that implemented a \IfTFMFileExists command by querying kpsewhich via the piped input feature. – GuM Dec 24 '16 at 14:12
• @Konstantin: Write an isocpeur package and offer it to the CTAN (Comprehensive TeX Archive Network). – GuM Dec 24 '16 at 14:14