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
?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityUsing 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
?
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.
EDITED to handle UNICODE inputs in Xelatex. Previously, I detected lowercase by seeing if ˋ#1
was in the ASCII lowercase range. This, obviously does not work for UNICODE. Therefore, I adapted macro \fauxschelphelp
to test for lowercase-ness using \lccode
.
Result can now be seen in last line.
\documentclass{article}
\usepackage{fontspec,graphicx}
\usepackage{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=\lccode`#1\relax\scalebox{\Hscale}[\Vscale]{\char\uccode`#1}\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}
\fauxsc
works with pdflatex, and without fontspec, provided you load the graphicx
package.
\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}
Nov 22, 2017 at 20:48
\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}
Apr 11, 2018 at 13:11
\@title
to fauxsc and getting: Improper alphabetic constant. <to be read again> \@title l.22 A one-character control sequence belongs after a ` mark. So I'm essentially inserting \0 here.
\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
.
Dec 15, 2018 at 21:56
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,
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.
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!
\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?
Feb 27, 2017 at 13:10
\uppercase
to \MakeUppercase
solved the problem (see explanation). :-)
Feb 27, 2017 at 18:25
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 …
real small caps
faked small caps
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."
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’m a bit late to the game. Nevertheless, this was a fun project to learn expl3 with, so here’s my solution.
The updated 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} }
\text_uppercase: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}
\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 \text_uppercase:nn
% (see l3kernel manual).
\hbox_set:Nn \l_ersatz_sc_box {\text_uppercase:nn{#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}
fontspec
knows about them, and falls back to ersatz small caps only when necessary.calc
and graphicx
first.)\emph{\ersatzsc{Ersatz}}
, not \ersatzsc{\emph{Ersatz}}
.l3boxes
rather than fontspec
font features.egregdoesnotlikesansseriftitles
:)
Aug 7, 2018 at 15:10
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}}}