34

Using XeTeX and fontspec, I use the Liberation font family in of my documents. Unfortunately those fonts (esp. Liberation Serif) do not natively support small caps.

Is there a way to enable fake small caps in fontspec?

21

Here I compare an alternative to Yan's approach. I label his method "fake" as he did and label this approach as "faux". I have found (see Good small caps font to use with arev?, for example) that an unequal scaling of horizontal and vertical dimension is better able to capture the proportions of small caps. For Minion Pro, shown in this example, I use 83% horizontal, and 72% vertical scaling to create faux small caps, with 100% horizontal scaling (no change) on the sc Caps. By comparison, I use 91% horizontal and 75% vertical scaling on Computer Modern, with 111% horizontal scaling on sc Caps, and on Palatino, 76% horizontal, 68% vertical scaling, with no change on the sc Caps.

In addition, my \fauxsc macro is able to automatically differentiate lower-case from upper-case arguments, and render them appropriately, which eases the input syntax.

In this MWE, I compare both "fake" and "faux" small caps to the real McCoy for Computer Modern, Minion Pro, and Palatino, respectively.

\documentclass{article}
\usepackage{fontspec,graphicx}
\makeatletter
\newlength\fake@f
\newlength\fake@c
\def\fakesc#1{%
  \begingroup%
  \xdef\fake@name{\csname\curr@fontshape/\f@size\endcsname}%
  \fontsize{\fontdimen8\fake@name}{\baselineskip}\selectfont%
  \uppercase{#1}%
  \endgroup%
}
\makeatother
\newcommand\fauxsc[1]{\fauxschelper#1 \relax\relax}
\def\fauxschelper#1 #2\relax{%
  \fauxschelphelp#1\relax\relax%
  \if\relax#2\relax\else\ \fauxschelper#2\relax\fi%
}
\def\Hscale{.83}\def\Vscale{.72}\def\Cscale{1.00}
\def\fauxschelphelp#1#2\relax{%
  \ifnum`#1>``\ifnum`#1<`\{\scalebox{\Hscale}[\Vscale]{\uppercase{#1}}\else%
    \scalebox{\Cscale}[1]{#1}\fi\else\scalebox{\Cscale}[1]{#1}\fi%
  \ifx\relax#2\relax\else\fauxschelphelp#2\relax\fi}
\begin{document}
  \LARGE
{\def\Hscale{.91}\def\Vscale{.75}\def\Cscale{1.11}
  \makebox[1.2in][l]{This is fake} S\fakesc{mall} C\fakesc{aps} $\leftarrow$ other answer\par
  \makebox[1.2in][l]{This is real} \textsc{Small Caps}\par
  \makebox[1.2in][l]{This is faux} \fauxsc{Small Caps} $\leftarrow$ this answer
\par}\smallskip
  \fontspec{Minion Pro}
  \makebox[1.2in][l]{This is fake} S\fakesc{mall} C\fakesc{aps} $\leftarrow$ other answer\par
  \makebox[1.2in][l]{This is real} \textsc{Small Caps}\par
  \makebox[1.2in][l]{This is faux} \fauxsc{Small Caps} $\leftarrow$ this answer
\par\smallskip
{\fontspec{Palatino Linotype}
\def\Hscale{.76}\def\Vscale{.68}\def\Cscale{1.0}
  \makebox[1.2in][l]{This is fake} S\fakesc{mall} C\fakesc{aps} $\leftarrow$ other answer\par
  \makebox[1.2in][l]{This is real} \textsc{Small Caps}\par
  \makebox[1.2in][l]{This is faux} \fauxsc{Small Caps} $\leftarrow$ this answer
\par}

\end{document}

enter image description here

  • 1
    I am happy to report that \fauxsc works with pdflatex, and without fontspec, provided you load the graphicx package. – erreka Nov 21 '16 at 19:28
  • 1
    @LaTechneuse You can add pre and post kerns to the \fauxschelphelp macro, here .5pt: \def\fauxschelphelp#1#2\relax{\kern.5pt% \ifnum`#1>``\ifnum`#1<`\{\scalebox{\Hscale}[\Vscale]{\uppercase{#1}}\else% \scalebox{\Cscale}[1]{#1}\fi\else\scalebox{\Cscale}[1]{#1}\fi% \kern.5pt\ifx\relax#2\relax\else\fauxschelphelp#2\relax\fi} – Steven B. Segletes Nov 22 '17 at 20:48
  • 1
    @Alec If the slightly altered syntax, \fauxsc{\itshape Test} were acceptable to you, then this redefinition would handle it: \def\fauxschelphelp#1#2\relax{% \ifcat\relax#1#1\else% \ifnum`#1>``\ifnum`#1<`\{\scalebox{\Hscale}[\Vscale]{\uppercase{#1}}\else% \scalebox{\Cscale}[1]{#1}\fi\else\scalebox{\Cscale}[1]{#1}\fi% \fi% \ifx\relax#2\relax\else\fauxschelphelp#2\relax\fi} – Steven B. Segletes Apr 11 '18 at 13:11
  • 1
    @Alberto Additionally, the \fauxsc macro is not set up to read macros in general. If the macro expands directly to your desired text, you would use \expandafter\fauxsc\expandafter{\foo}, so that \foo is expanded to text before it is seen by \fauxsc. – Steven B. Segletes Dec 15 '18 at 21:56
  • 1
    @Alberto The first \expandafter skips over \fauxsc and executes the 2nd \expandafter, which skips over { and expands \foo. Then what is left is \fauxsc{<expanded-version-of-foo>}. – Steven B. Segletes Dec 15 '18 at 21:59
13

Here is a simple solution

\documentclass{article}
\usepackage{fontspec}
\makeatletter
\newlength\fake@f
\newlength\fake@c
\def\fakesc#1{%
  \begingroup%
  \xdef\fake@name{\csname\curr@fontshape/\f@size\endcsname}%
  \fontsize{\fontdimen8\fake@name}{\baselineskip}\selectfont%
  \uppercase{#1}%
  \endgroup%
}
\makeatother

\begin{document}
  \Huge

  \fontspec{Minion Pro}
  This is real \textsc{small caps} \par
  This is fake \fakesc{small caps} \par
  \medskip
  \fontspec{Didot Lt Std}
  This is real \textsc{small caps} \par
  This is fake \fakesc{small caps} \par
\end{document}

The effect is here, enter image description here

Clearly, this will not universally work. The \fakesc will resize the font to Cap size without changing the baselineskip. However, this will depend on the design of the font. With Minion Pro, set in \Huge and thus Minion Pro Disp is in use, setting the fake small caps will use Minion Pro Subh, and thus looks not too bad, since Subh is slightly heavier than Disp. But set in a non-optical size font, say Didot, the contrast of weights is even more observable. In addition, due to different x-height, the fake small caps will looks a little too large. So you would need to adjust the \fontsize accordingly.

  • Consider also this answer, if one uses fontspec and one's font, like Junicode, has native small-caps in normal shape, but lacks them in bold - then a remapping to fontspec's 'FakeBold' can be performed using \DeclareFontShape. Cheers! – sdaau May 16 '12 at 15:53
  • My understanding is that small-caps is supposed to leave the real capital letters normal, and only the convert the lower case letters to be small capital letters. This script appears to eliminate the upper case-ness of characters, making all characters, upper and lower, appear the same. Try a test case with both upper and lower characters, please. – AgilePro Jul 27 '14 at 3:59
  • \fakesc is nearly what I need. The only problem is that it doesn't work with \def and \newcommand. Example: \newcommand{\mycmd}{Something that is supposed to be printed using small caps}, \fakesc{\mycmd}. How can I solve it? – patryk.beza Feb 27 '17 at 13:10
  • Changing \uppercase to \MakeUppercase solved the problem (see explanation). :-) – patryk.beza Feb 27 '17 at 18:25
8

The best – and IMHO only acceptable – way to get small caps is to use a font containing them every fake looks bad, since the stroke widths of faked small caps won’t match the real uppercase letter.

Take a look at these images showing Linux Libertine, and make your own decision …

right
real small caps

wrong
faked small caps

  • 12
    Correct answer, not to my question though. – Michael May 13 '12 at 17:59
  • 2
    @Michael: Certainly it’s no answer showing how to get faked small caps but an advice showing why not to want small caps and for that worth to be mentioned here in my opinion ;-) – Tobi May 13 '12 at 22:27
8

You can also use FontForge to add automatically generated real small caps to the font. It's still not the same as real real small caps but it's way better than faked small caps: http://fontforge.sourceforge.net/Styles.html#Smallcaps

Edit: Just had an experience that makes me decidedly modify the above statement to: "It has the potential to be way better than faked small caps. The result can also be utterly useless, however."

  • +1 But we also want pictures – Earthliŋ Aug 3 '18 at 13:06
5

Yeah if your font does not provide native small caps you can try the following code to make fake caps. Works fine on my side...

\documentclass[]{article}
%\usepackage{fontspec}

\begin{document}

\makeatletter

\def\mycommand{\bgroup\obeyspaces\mycommandaux}
\def\mycommandaux#1{\mycommandauxii #1\relax\relax\egroup}
\def\mycommandauxii#1{%
\ifx\relax#1\else \ifcat#1\@sptoken{} \expandafter\expandafter\expandafter\mycommandauxii\else
\ifnum`#1=\uccode`#1 {\normalsize #1}\else {\footnotesize \uppercase{#1}}\fi \expandafter\expandafter\expandafter\mycommandauxii\expandafter\fi\fi}

\mycommand{All inside this are fake caps}

\end{document}

Hope it helps ++

  • I tried this and it almost worked. I seem to lose all the spaces in the string: all the words are smashed together. Any solution to that? – AgilePro Jul 27 '14 at 4:05
3

I’m a bit late to the game. Nevertheless, this was a fun project to learn expl3 with, so here’s my solution.

Improved Version

The updted version is better in a number of ways: it calculates font metrics (with fewer bugs) once per invocation of the function rather than once per character, detects more lowercase letters, scales the font rather than putting each letter in a box. It automatically detects the current language selected with Polyglossia and uses its special rules (if any) to capitalize the lowercase letters.

The bottom output line shows a word in different languages, each in a font face that does not come with real small caps: Latin Modern Roman Demibold, Latin Modern Roman Dunhill and Latin Modern Roman Bold Italic. You’ll notice that TÜRKİYE has a dot over the İ in Turkish and that Frauenfußball became FRAUENFUSSBALL.

\documentclass[varwidth, preview]{standalone}
\usepackage{fontspec}

\defaultfontfeatures{Scale=MatchUppercase}
\setmainfont[
  Scale=1.0,
  FontFace={sb}{n}{LMRomanDemi10-Regular},
  FontFace={sb}{i}{LMRomanDemi10-Oblique}
]{Latin Modern Roman}

\DeclareRobustCommand\sbseries{\fontseries{sb}\selectfont}
\DeclareTextFontCommand{\textsb}{\sbseries}

\usepackage{polyglossia}
\setdefaultlanguage{english}
\setotherlanguage{turkish}
\setotherlanguage{german}
\setotherlanguage{danish}

\newfontfamily\germanfont{Latin Modern Roman Dunhill}

\RequirePackage{expl3}
\ExplSyntaxOn

% As of June 2018, l3regex does not support Unicode, e.g. (\p{Ll}\p{M}*)+
% This regex is therefore incomplete.
\regex_const:Nn \c_ersatz_lower_regex
  {[[:lower:]ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿāăąćĉċčďđēĕėęěĝğġģĥħĩīĭįıĵķĸĺļľŀłńņňʼnŋōŏőŕŗřśŝşšţťŧũūŭůűųŵŷźżžıLjljNjnjǣDzdzƀƶǥǿȼɇɉɍɏɟɨʄᴓᵽⱥⱦꝁꝉαβγδεζηθικλμνξοπρςστυφχψωΐέήίΰϊϋόύώϙἀἁἂἃἄἅἆἇἐἑἒἓἔἕἠἡἢἣἤἥἦἧἰἱἲἳἴἵἶἷὀὁὂὃὄὅὐὑὒὓὔὕὖὗὠὡὢὣὤὥὦὧὰάὲέὴήὶίὸόὺύὼώᾀᾁᾂᾃᾄᾅᾆᾇᾐᾑᾒᾓᾔᾕᾖᾗᾠᾡᾢᾣᾤᾥᾦᾧᾰᾱᾲᾳᾴᾶᾷῂῃῄῆῇῐῑῒΐῖῗῠῡῢΰῤῥῦῧῲῳῴῶῷ]}

\cs_new:Npn \ersatz_helper:nnnn #1 #2 #3 #4 {
      {\addfontfeatures{ Scale={#3}, FakeStretch={#4} }
         \tl_upper_case:nn{#2}{#1}
      }
}

\cs_new:Npn \ersatz_force:x #1 {
  \str_clear_new:N \l_ersatz_str_str
  \str_set:Nn \l_ersatz_arg_str {#1}
  %\tl_log:N \l_ersatz_arg_str

  \dim_zero_new:N \l_ersatz_ex_dim
  \dim_set:Nn \l_ersatz_ex_dim {\fontdimen5\l_fontspec_font}

  % The ratio of the font’s official ex metric to the actual height
  % of the letter x is a rough-and-ready approximation of the current
  % font scaling.
  \box_clear_new:N \l_ersatz_x_box
  \vbox_set:Nn \l_ersatz_x_box {x}
  \dim_zero_new:N \l_ersatz_xheight_dim
  \dim_set:Nn \l_ersatz_xheight_dim {\box_ht:N \l_ersatz_x_box}

  \box_clear_new:N \l_ersatz_cap_box
  \vbox_set:Nn \l_ersatz_cap_box {H}
  \dim_zero_new:N \l_ersatz_capheight_dim
  \dim_set:Nn \l_ersatz_capheight_dim {\box_ht:N \l_ersatz_cap_box}

  \fp_zero_new:N \l_ersatz_prevscale_fp
  \fp_set:Nn \l_ersatz_prevscale_fp
             {\l_ersatz_xheight_dim / \l_ersatz_ex_dim}

  \fp_zero_new:N \l_ersatz_scale_fp
  \fp_set:Nn \l_ersatz_scale_fp
             {\l_ersatz_xheight_dim / \l_ersatz_capheight_dim}

  % A good horizontal scale factor, by eyeball, is the vertical scaling
  % factor to the power of 0.75.  That is, fonts with a relatively high
  % x-height should be stretched a little and fonts with a relatively lower
  % x-height should be stretched by a lot.  The ratio of hstretch / vscale
  % is therefore vscale**(0.75 - 1) = vscale**(-0.25).
  %
  % Alternative: take the ratio of the width of the lowercase m to 1 em?
  \fp_zero_new:N \l_ersatz_hstretch_fp
  \fp_set:Nn \l_ersatz_hstretch_fp {\l_ersatz_scale_fp ** (-0.25)}

  % We previously calculated the vertical scale factor relative to the
  % current one, so we need to convert it to a scale factor relevant to
  % \f@size.
  \fp_zero_new:N \l_ersatz_vshrink_fp
  \fp_set:Nn \l_ersatz_vshrink_fp
             {\l_ersatz_scale_fp * \l_ersatz_prevscale_fp}

  \str_clear_new:N \l_ersatz_lang_str
  % These languages are the only ones that currently have special-case
  % handling in expl3, according to the manual. OpenType language codes
  % are at
  % https://docs.microsoft.com/en-us/typography/opentype/spec/languagetags
  {\fontspec_if_current_language:nTF{TRK}
    {\str_set:Nn \l_ersatz_lang_str {tr}}
    {\fontspec_if_current_language:nTF{AZE}
       {\str_set:Nn \l_ersatz_lang_str {az}}
       {\fontspec_if_current_language:nTF{LTH}
         {\str_set:Nn \l_ersatz_lang_str {lt}}
         {\fontspec_if_current_language:nTF{NLD}
           {\str_set:Nn \l_ersatz_lang_str {nl}}
           {\str_set:Nn \l_ersatz_lang_str {en}}
         }
       }
     }
   }

  \regex_replace_all:NnN
    \c_ersatz_lower_regex
    {\c{ersatz_helper:nnnn}\cB\{\0\cE\}\cB\{\u{l_ersatz_lang_str}\cE\}\cB\{\c{fp_to_decimal:N} \u{l_ersatz_vshrink_fp}\cE\}\cB\{\c{fp_to_decimal:N} \u{l_ersatz_hstretch_fp}\cE\}}
    \l_ersatz_arg_str
  \l_ersatz_arg_str
}

\newcommand\dubioussc[1]{%
\fontspec_if_small_caps:TF{\textsc{#1}}{\ersatz_force:x{#1}}%
}

\newcommand\ersatzsc[1]{\ersatz_force:x{#1}}

\ExplSyntaxOff

\newcommand\test[1]{\fbox{#1}
\fbox{\emph{\ersatzsc{Ersatz}}\ersatzsc{ Small Caps}}
\fbox{\dubioussc{\emph{True} Small Caps?}}\relax}

\begin{document}

{\fontspec{lmroman5-regular.otf}[
  ItalicFont=lmroman7-italic.otf,
  SmallCapsFont=lmromancaps10-regular.otf,
  ItalicFeatures={SmallCapsFont=lmromancaps10-oblique.otf}]  
\test{Latin Modern 5}}

{\fontspec{lmroman10-regular.otf}[
  ItalicFont=lmroman10-italic.otf,
  SmallCapsFont=lmromancaps10-regular.otf,
  ItalicFeatures={SmallCapsFont=lmromancaps10-oblique.otf}]  
\test{Latin Modern 10}}

{\fontspec{lmroman17-regular.otf}[
  ItalicFont=lmroman12-italic.otf,
  SmallCapsFont=lmromancaps10-regular.otf,
  ItalicFeatures={SmallCapsFont=lmromancaps10-oblique.otf}]  
\test{Latin Modern 17}}

{\fontspec{TeX Gyre Pagella}
\test{Pagella}}

{\fontspec{TeX Gyre Termes}
\test{Termes}}

{\fontspec{TeX Gyre Adventor}
\test{Adventor}}

\textsb{\textturkish{Türkiye \ersatzsc{Türkiye}}}
\textgerman{Frauenfußball \ersatzsc{Frauenfußball}}
\textit{\textbf{\textdanish{København \ersatzsc{København }}}}

\end{document}

Ersatz SC Sample

Original Version

\documentclass[varwidth, preview]{standalone}
\usepackage{fontspec}
\defaultfontfeatures{Scale=MatchUppercase}

\RequirePackage{expl3}
\ExplSyntaxOn

\regex_const:Nn \c_ersatz_lower_regex {[[:lower:]]}

\cs_new:Npn \ersatz_helper:N #1 {
  \regex_match:NnTF
    \c_ersatz_lower_regex
    {#1}
    {
      \box_clear_new:N \l_ersatz_sc_box
      % Optionally add a language code to \tl_upper_case:nn
      % (see l3kernel manual).
      \hbox_set:Nn \l_ersatz_sc_box {\tl_upper_case:n{#1}}

      \dim_zero_new:N \l_ersatz_wd_dim
      \dim_set:Nn \l_ersatz_wd_dim {\box_wd:N \l_ersatz_sc_box}

      \dim_zero_new:N \l_ersatz_ht_dim
      \dim_set:Nn \l_ersatz_ht_dim {\box_ht:N \l_ersatz_sc_box}

      \dim_zero_new:N \l_ersatz_xheight_dim
      \dim_set:Nn \l_ersatz_xheight_dim {\fontdimen5\font}

      \box_clear_new:N \l_ersatz_cap_box
      \vbox_set:Nn \l_ersatz_cap_box {H}
      \dim_zero_new:N \l_ersatz_capheight_dim
      \dim_set:Nn \l_ersatz_capheight_dim {\box_ht:N \l_ersatz_cap_box}

      \fp_zero_new:N \l_ersatz_vscale_fp
      \fp_set:Nn \l_ersatz_vscale_fp
                 {\l_ersatz_xheight_dim / \l_ersatz_capheight_dim}

      \fp_zero_new:N \l_ersatz_hscale_fp
      \fp_set:Nn \l_ersatz_hscale_fp {\l_ersatz_vscale_fp ** 0.75}

      \box_resize_to_wd_and_ht:Nnn
         \l_ersatz_sc_box
         {\fp_to_dim:n {\l_ersatz_hscale_fp * \l_ersatz_wd_dim}}
         {\fp_to_dim:n {\l_ersatz_vscale_fp * \l_ersatz_ht_dim}}

      \box_use:N \l_ersatz_sc_box
    }
    {#1}
}

\cs_new:Npn \ersatz_force:x #1 {
  \str_clear_new:N \l_ersatz_arg_str
  \str_set:Nn \l_ersatz_arg_str {#1}
  \tl_log:N \l_ersatz_arg_str

  \str_map_function:NN \l_ersatz_arg_str \ersatz_helper:N
}

\newcommand\dubioussc[1]{%
\fontspec_if_small_caps:TF{\textsc{#1}}{\ersatz_force:x{#1}}%
}

\newcommand\ersatzsc[1]{\ersatz_force:x{#1}}

\ExplSyntaxOff

\newcommand\test[1]{\fbox{#1}
\fbox{\emph{\ersatzsc{Ersatz}}\ersatzsc{ Small Caps}}
\fbox{\dubioussc{\emph{True} Small Caps?}}\relax}

\begin{document}

\fontspec{lmroman5-regular.otf}[
  ItalicFont=lmroman7-italic.otf,
  SmallCapsFont=lmromancaps10-regular.otf,
  ItalicFeatures={SmallCapsFont=lmromancaps10-oblique.otf}]  
\test{Latin Modern 5}

\fontspec{lmroman10-regular.otf}[
  ItalicFont=lmroman10-italic.otf,
  SmallCapsFont=lmromancaps10-regular.otf,
  ItalicFeatures={SmallCapsFont=lmromancaps10-oblique.otf}]  
\test{Latin Modern 10}

\fontspec{lmroman17-regular.otf}[
  ItalicFont=lmroman12-italic.otf,
  SmallCapsFont=lmromancaps10-regular.otf,
  ItalicFeatures={SmallCapsFont=lmromancaps10-oblique.otf}]  
\test{Latin Modern 17}

\fontspec{Palatino Linotype}
\test{Palatino}

\fontspec{TeX Gyre Termes}
\test{Termes}

\fontspec{TeX Gyre Adventor}
\test{Adventor}

\end{document}

Small Caps Sample

Pros

  • Uses real small caps if fontspec knows about them, and falls back to ersatz small caps only when necessary.
  • Scales the x-height of the fake small caps to be the same as the regular font.
  • Expands the fake small caps horizontally by an appropriate ratio to the font metrics. I raised the horizontal scale factor to the power of 0.75—that is, fonts with a relatively tall x-height expand only a little more, and fonts with a relatively short x-height expand a lot more.
  • Supports all of Unicode.
  • LaTeX3 syntax is a lot more readable and maintainable. (I did write a partial solution with calc and graphicx first.)
  • Everything but the real/ersatz detection is compatible with legacy fonts.

Cons

  • Ersatz small caps cannot contain protected or font commands. You must write \emph{\ersatzsc{Ersatz}}, not \ersatzsc{\emph{Ersatz}}.
  • Its autodetection of the font metrics doesn’t work for every font.
  • I didn’t add an option to support other languages, such as i→İ in Turkish.
  • It could be made much more efficient. For example, it recalculates the scaling factors once per lowercase character, not once per invocation of the outermost function, even though the font metrics cannot currently change during the same invocation of the function.
  • It uses l3boxes rather than fontspec font features.
  • Btw: Frauenfußball could also use the uppercase German double-s instead of SS. We have had a reform last year. – TeXnician Aug 7 '18 at 14:28
  • @TeXnician There’s an option in the LaTeX3 kernel to enable that; would native speakers of German prefer that to be the default in their language? Should there be an option? – Davislor Aug 7 '18 at 14:49
  • @TeXnician Unless I’m misunderstanding, there needs to be a way to stick to the old rules, if only because not all fonts will have a capital ẞ? – Davislor Aug 7 '18 at 14:52
  • Currently you are allowed to use both and as a native speaker and writer I prefer the SS version, but mostly out of habit and because my keyboard doesn't provide the capital ẞ. If I had this key, I would probably use this letter for fonts that have it, but really it's very controversial. – TeXnician Aug 7 '18 at 15:01
  • @TeXnician So maybe \ersatzsc[GROSSESESZETT]{Frauenfußball}? :) – Davislor Aug 7 '18 at 15:08
2

Here is a very simple solution which I use when I'm forced by editors to use the Times New Roman font, which have no Small Caps implemented. Since I want to leave the rest of my document intact, I simple redefine \textsc in the following way:

\renewcommand{\textsc}[1]{{\footnotesize \uppercase{#1}}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.