5

How can I scale up/down the size of a font so that it looks coherent when mixed with other fonts on the same line?

As a concrete example, how can the first 'a' in the following snippet be scaled down to the size of the second and third?

a-character-in-different-sizes

\documentclass[12pt]{article}
\pagestyle{empty}

\newcommand\pagmsl{\usefont{\encodingdefault}{pag}{m}{sl}}
\newcommand\pbklit{\usefont{\encodingdefault}{pbk}{l}{it}}
\newcommand\bchmit{\usefont{\encodingdefault}{bch}{m}{it}}

\begin{document}

\pagmsl a
\pbklit a
\bchmit  a

\end{document}
1
  • 1
    If you are able to use the fontspec package, you might want to try out the [Scale=MatchLowercase] option, which automatically scales the fonts to the same x-height. Sep 10, 2013 at 12:41

2 Answers 2

5

Forcing a bit the font selection scheme, you can define a command \definesamplefont that defines a whole new font family based on the main font x-height. You can typeset paragraphs with the newly defined font switches.

\documentclass[12pt]{article}
\pagestyle{empty}

\usepackage{expl3}
\ExplSyntaxOn
\cs_set_eq:NN \compute \fp_eval:n
\ExplSyntaxOff

\makeatletter
\newcommand{\definesamplefont}[4]{%
  \sbox0{\normalfont a}%
  \sbox2{\normalfont\usefont{\encodingdefault}{#2}{#3}{#4}a%
         \xdef\@temp{\fontname\font\space}}%
  \begingroup\edef\x{\endgroup
    \noexpand\DeclareFontFamily{\encodingdefault}{#1}{}
    \noexpand\DeclareFontShape{\encodingdefault}{#1}{X}{X}
     { <-> [\compute{\strip@pt\ht0/\strip@pt\ht2}] \expandafter\xget@name\@temp\@nil }{}
  }\x
 \@namedef{#1}{\usefont{\encodingdefault}{#1}{X}{X}}
}
\def\xget@name#1 #2\@nil{#1}
\makeatother
\definesamplefont{pagmsl}{pag}{m}{sl}
\definesamplefont{pbklit}{pbk}{l}{it}
\definesamplefont{bchmit}{bch}{m}{it}

\begin{document}

\pagmsl a
\pbklit a
\bchmit a

\end{document}

enter image description here

Another way to get the required ratio is to use the package fp. Here's the complete example for this case:

\documentclass[12pt]{article}
\pagestyle{empty}

\usepackage{fp}

\makeatletter
\newcommand{\definesamplefont}[4]{%
  \sbox0{\normalfont a}%
  \sbox2{\normalfont\usefont{\encodingdefault}{#2}{#3}{#4}a%
         \xdef\@temp{\fontname\font\space}}%
  \FPdiv\computed@ratio{\number\ht0}{\number\ht2}%
  \begingroup\edef\x{\endgroup
    \noexpand\DeclareFontFamily{\encodingdefault}{#1}{}
    \noexpand\DeclareFontShape{\encodingdefault}{#1}{X}{X}
     { <-> [\computed@ratio] \expandafter\xget@name\@temp\@nil }{}
  }\x
 \@namedef{#1}{\usefont{\encodingdefault}{#1}{X}{X}}
}
\def\xget@name#1 #2\@nil{#1}
\makeatother
\definesamplefont{pagmsl}{pag}{m}{sl}
\definesamplefont{pbklit}{pbk}{l}{it}
\definesamplefont{bchmit}{bch}{m}{it}

\begin{document}
a
\pagmsl a
\pbklit a
\bchmit a

\end{document}

A full expl3 implementation of the same code.

\documentclass[12pt]{article}
\pagestyle{empty}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\definesamplefont}{mmmm}
 {
  \calaf_define_sample_font:nnnn { #1 } { #2 } { #3 } { #4 }
 }

\box_new:N \l__calaf_normal_ht_box
\hbox_set:Nn \l__calaf_normal_ht_box {\normalfont a}
\box_new:N \l__calaf_new_ht_box
\tl_new:N \g__calaf_fontname_tl
\seq_new:N \l__calaf_fontname_seq
\fp_new:N \l__calaf_ratio_fp

\cs_generate_variant:Nn \seq_set_split:Nnn { Nnx }

\cs_new_protected:Npn \calaf_define_sample_font:nnnn #1 #2 #3 #4
 {
  \hbox_set:Nn \l__calaf_new_ht_box
   {
    \normalfont\usefont{\encodingdefault}{#2}{#3}{#4} a
    \seq_set_split:Nnx \l__calaf_fontname_seq { ~ } { \fontname\font \c_space_tl }
    \seq_pop_left:NN \l__calaf_fontname_seq \l_tmpa_tl
    \tl_gset_eq:NN \g__calaf_fontname_tl \l_tmpa_tl
   }
  \fp_set:Nn \l__calaf_ratio_fp
   {
    \dim_to_fp:n { \box_ht:N \l__calaf_normal_ht_box}
    /
    \dim_to_fp:n {\box_ht:N \l__calaf_new_ht_box }
   }
  \use:x
   {
    \exp_not:N \DeclareFontFamily {\encodingdefault} {#1} {}
    \exp_not:N \DeclareFontShape  {\encodingdefault} {#1} {X} {X}
     { <-> [ \fp_use:N \l__calaf_ratio_fp ] \g__calaf_fontname_tl } {}
   }
  \cs_new:cpn { #1 } {  \usefont{\encodingdefault}{#1}{X}{X} }
 }

\ExplSyntaxOff
\definesamplefont{pagmsl}{pag}{m}{sl}
\definesamplefont{pbklit}{pbk}{l}{it}
\definesamplefont{bchmit}{bch}{m}{it}

\begin{document}

\pagmsl a
\pbklit a
\bchmit a

\end{document}
6
  • On my side, pdflatex bails out at '\compute'.
    – Calaf
    Sep 10, 2013 at 0:45
  • @Calaf You need to update your TeX distribution, then.
    – egreg
    Sep 10, 2013 at 8:27
  • I'm running MacTeX 2012. I guess even the TeX crowd has become so keen on being bleeding edge that not updating four months after the release of 2013 makes my installation obsolete.
    – Calaf
    Sep 10, 2013 at 11:56
  • 1
    @Calaf There should be other ways to define \compute; I'll try with other packages. But an upgrade to MacTeX 2013 is recommended anyway.
    – egreg
    Sep 10, 2013 at 12:02
  • Confirmed: Upgrading to 2013 did indeed solve the issue.
    – Calaf
    Sep 10, 2013 at 14:09
4

In terms of vertical height, you can store some fixed height in a length and scale elements to that height elsewhere in your document.

Below I've used \resizebox{!}{\fixedheight}{<stuff>} to resize <stuff> to the height of \fixedheight (whatever that may be; a in Computer Modern in my case below):

enter image description here

\documentclass[12pt]{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\pagestyle{empty}

\newcommand\pagmsl{\usefont{\encodingdefault}{pag}{m}{sl}}
\newcommand\pbklit{\usefont{\encodingdefault}{pbk}{l}{it}}
\newcommand\bchmit{\usefont{\encodingdefault}{bch}{m}{it}}

\begin{document}

\newlength{\fixedheight}\settoheight{\fixedheight}{a}

\fbox{%
  a
  {\pagmsl a}
  {\pbklit a}
  {\bchmit a}%
}

\fbox{%
  a
  \resizebox{!}{\fixedheight}{\pagmsl a}
  \resizebox{!}{\fixedheight}{\pbklit a}
  \resizebox{!}{\fixedheight}{\bchmit a}%
}
\end{document}

Of course the caveat here is that you won't enjoy line-breaking with this approach. However, your example listed only characters. Moreover, you will lose certain dimensions with the scaling.

scalerel provides similar functionality.

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.