Difference between revisions of "Font Switching"

From Wiki
Jump to navigation Jump to search
(Official ConTeXt documentation --> Manuals)
 
(34 intermediate revisions by 10 users not shown)
Line 1: Line 1:
< [[Basic Text Formatting]] | [[Fonts]] | [[Font Sizes]] >
+
In ConTeXt there are four ways to switch fonts:
  
 +
* font style/alternative commands ({{cmd|rm}}, {{cmd|ss}}, etc.),
 +
* font size commands ({{cmd|tfa}}, {{cmd|tfb}}, etc.),
 +
* mnemonic command aliases ({{cmd|bold}}, {{cmd|sans}}, etc.),
 +
* a complete font change ({{cmd|setupbodyfont}}, {{cmd|switchtobodyfont}}).
  
In ConTeXt there are four ways to switch fonts:
+
==  Before you start ...  ==
  
* font style (<cmd>rm</cmd>, <cmd>ss</cmd>, etc.),
+
1. Placing a font switch at the begin of a paragraph can sometimes generate unexpected side-effects. This can be avoided by forcing horizontal mode before any font change:
* font size  (<cmd>tfa</cmd>, <cmd>tfb</cmd>, etc.),
+
<texcode>
* alternative font style (<cmd>bold</cmd>, <cmd>sans</cmd>, etc.),
+
\dontleavehmode{\bf Warning!} Care must be taken when a font switch is used at the beginning of a paragraph.\par
* a complete font change (<cmd>setupbodyfont</cmd>, <cmd>switchtobodyfont</cmd>).
+
</texcode>
  
== Font styles ==
+
2. It is usually a bad style to use explicit font switches in the text. It is better to    define a semantic command that takes care of the font switching. For example, instead of  using <code>{\bf\red important text}</code>
 +
<texcode>
 +
\definehighlight[important][style=bold, color=red]
 +
</texcode>
 +
and then use <code>\important{important text}</code> in the text.
 +
 
 +
== Font styles and alternatives ==
  
There are three types of font families: serif, sans serif, and teletype.  To
+
There are three main types of font styles: serif, sans serif, and teletype.  To
switch between these families, use <cmd>rm</cmd> for serif, <cmd>ss</cmd> for
+
switch between these styles, use {{cmd|rm}} for serif, {{cmd|ss}} for
sans serif, and <cmd>tt</cmd> for teletype.
+
sans serif, and {{cmd|tt}} for teletype.
  
Each of these families come in different styles: upright, bold, italic,
+
Each of these styles comes in different alternatives: normal, bold, italic,
 
slanted, bold-italic, bold-slanted, and small-capped. To switch to a different
 
slanted, bold-italic, bold-slanted, and small-capped. To switch to a different
style, use <cmd>tf</cmd> for upright, <cmd>bf</cmd> for bold, <cmd>it</cmd>
+
alternative, use {{cmd|tf}} ("typeface") for normal, {{cmd|bf}} for bold, {{cmd|it}}
for italic, <cmd>sl</cmd> for slanted, <cmd>bi</cmd> for bold-italic,
+
for italic, {{cmd|sl}} for slanted, {{cmd|bi}} for bold-italic,
<cmd>bs</cmd> for bold-slanted, and <cmd>sc</cmd> for small-capped.
+
{{cmd|bs}} for bold-slanted, and {{cmd|sc}} for small-capped.
  
You can generally combine font families and font styles, so if you want to
+
You can generally combine font styles with alternatives, so if you want to
 
switch to bold sans serif, you can use either <code>\bf\ss</code> or
 
switch to bold sans serif, you can use either <code>\bf\ss</code> or
<code>\ss\bf</code>.  
+
<code>\ss\bf</code>. The various combinations look like the following.
 +
 
 +
<context mode="mkiv">
 +
\setupbodyfont[8pt]
 +
\setupcolors[rgb, state=start]
 +
\definecolor[one][c=0, m=.4, y=1, k=0]
 +
\definecolor[two][c=0, m=.2, y=.5, k=0]
 +
\setupTABLE[frame=off,offset=0.5em]
  
There is a font switch <cmd>em</cmd> to ''emphasize'' text. This is somewhat
+
\setupTABLE[c][2][style=\rm]
special: it does automatic italic correction and changes the style depending
+
\setupTABLE[c][3][style=\ss]
on the current font style. For example, if the current font style is upright,
+
\setupTABLE[c][4][style=\tt]
<cmd>em</cmd> switches to slanted; and if the current font style is slanted,
+
\setupTABLE[r][1][background=color, backgroundcolor=one]
<cmd>em</cmd> switches to upright.
+
\setupTABLE[c][1][background=color, backgroundcolor=one]
  
ConTeXt uses the Latin Modern fonts by default; these fonts look
+
\startTABLE
similar to the original Computer Modern fonts, but have a much larger
+
  \NC            \NC \type{\rm} serif \NC \type{\ss} sans serif \NC \type{\tt} teletype \NC \NR
character repertoire. As it happens, in the Latin Modern (and Computer
+
  \NC \type{\tf} \NC Normal          \NC Normal                \NC Normal              \NC \NR
Modern) fonts, the slanted font does not stand out from the
+
\NC \type{\bf}  \NC \bf Bold            \NC \bf Bold                  \NC \bf Bold  \NC \NR
upright font enough for some tastes; so, many people prefer to use the
+
\NC \type{\it}  \NC \it Italic          \NC \it Italic                \NC \it Italic \NC \NR
italic font for emphasis. To do that use
+
\NC \type{\sl}  \NC \sl Slanted          \NC \sl Slanted              \NC \sl Slanted\NC \NR
<texcode>
+
\NC \type{\bi}  \NC \bi Bold Italic      \NC \bi Bold Italic          \NC \bi Bold Italic\NC \NR
\definebodyfontenvironment[default][em=italic]
+
\NC \type{\bs}  \NC \bs Bold Slanted    \NC \bs Bold Slanted          \NC \bs Bold Slanted\NC \NR
</texcode>
+
\NC \type{\sc}  \NC \sc Smallcaps    \NC \sc Smallcaps          \NC \sc Smallcaps\NC \NR
 +
\stopTABLE
 +
 
 +
</context>
  
A font switch remains valid for the rest of the group. So, if you want to
+
A font switch remains valid for the rest of the ''group''. So, if you want to
 
temporarily switch to a different font, use the font style command inside a
 
temporarily switch to a different font, use the font style command inside a
 
group. The easiest way to start a group is to enclose the text within braces
 
group. The easiest way to start a group is to enclose the text within braces
Line 49: Line 69:
 
This is serif text
 
This is serif text
 
{\ss This is sans serif}
 
{\ss This is sans serif}
 +
This is serif again
 
{\tt and this is typewriter}
 
{\tt and this is typewriter}
 +
And serif again
 
</texcode>
 
</texcode>
  
Line 57: Line 79:
 
This is serif text \crlf
 
This is serif text \crlf
 
{\ss This is sans serif} \crlf
 
{\ss This is sans serif} \crlf
{\tt and this is typewriter}
+
This is serif again \crlf
 +
{\tt and this is typewriter} \crlf
 +
And serif again
 +
</context>
 +
 
 +
== Emphasizing text ==
 +
There is a font switch {{cmd|em}} to ''emphasize'' text. This is somewhat
 +
special: it does automatic italic correction and changes the alternative depending
 +
on the current alternative. For example, if the current font alternative is normal (i.e. upright),
 +
{{cmd|em}} switches to slanted; and if the current font alternative is slanted,
 +
{{cmd|em}} switches to normal.
 +
 
 +
<context>
 +
\starttabulate[|l|f{\tf}l|f{\it}l|f{\sl}l|f{\bf}l|f{\bi}l|f{\bs}l|]
 +
\NC                \NC \tttf \tex{tf} \NC \tttf \tex{it} \NC \tttf \tex{sl} \NC
 +
\tttf \tex{bf} \NC \tttf \tex{bi} \NC \tttf \tex{bs} \NC\NR
 +
\NC                \NC          Text \NC          Text \NC          Text \NC
 +
Text \NC          Text \NC          Text \NC\NR
 +
\NC \tex{em}      \NC \em      Text \NC \em      Text \NC \em      Text \NC
 +
\em      Text \NC \em      Text \NC \em      Text \NC\NR
 +
\NC \tex{boldface} \NC \boldface Text \NC \boldface Text \NC \boldface Text \NC
 +
\boldface Text \NC \boldface Text \NC \boldface Text \NC\NR
 +
\NC \tex{typeface} \NC \typeface Text \NC \typeface Text \NC \typeface Text \NC
 +
\typeface Text \NC \typeface Text \NC \typeface Text \NC\NR
 +
\NC \tex{swapface} \NC \swapface Text \NC \swapface Text \NC \swapface Text \NC
 +
\swapface Text \NC \swapface Text \NC \swapface Text \NC\NR
 +
\stoptabulate
 
</context>
 
</context>
 +
 +
ConTeXt uses the Latin Modern fonts by default; these fonts look
 +
similar to the original Computer Modern fonts, but have a much larger
 +
character repertoire.  As it happens, in the Latin Modern (and Computer
 +
Modern) fonts, the slanted font does not stand out from the
 +
upright font enough for some tastes; so, many people prefer to use the
 +
italic font for emphasis. To do that use
 +
<texcode>
 +
\definebodyfontenvironment[default][em=italic]
 +
</texcode>
 +
 +
{{note| For Opentype fonts, italic correction needs to be set using
 +
<texcode>\definefontfeature[default][default][itlc=yes]</texcode>}}
  
 
==  Font sizes ==
 
==  Font sizes ==
  
Occasionally one needs to change the font size. ConTeXt provides two series
+
Occasionally one needs to use a font size different from the bodyfont. ConTeXt provides two series
of commands for that. To increase the font you can use <cmd>tfa</cmd> to scale
+
of commands for that. For a larger font size, you can use {{cmd|tfa}} to scale
the font size by a factor of <code>1.2</code>, <cmd>tfb</cmd> to scale by a factor of
+
the font size by a factor of <code>1.2</code>, {{cmd|tfb}} to scale by a factor of
<code>(1.2)^2 = 1.44</code>, <cmd>tfc</cmd> to scale by <code>(1.2)^3 = 1.728</code> and <cmd>tfd</cmd> to
+
<code>(1.2)^2 = 1.44</code>, {{cmd|tfc}} to scale by <code>(1.2)^3 = 1.728</code> and {{cmd|tfd}} to
scale by <code>(1.2)^4 = 2.074</code>.
+
scale by <code>(1.2)^4 = 2.074</code>. The scale is relative to the current bodyfont size.
 +
 
 +
 
 +
To decrease the font size, you can use {{cmd|tfx}}
 +
to scale the font by a factor of <code>0.8</code> and {{cmd|tfxx}} to scale by a factor
 +
of <code>0.6</code>.
 +
 
 +
The complete set of relative font sizes are listed below:
 +
{| style="border:1px solid #ffcc00; width:100%; padding: 0.5em;"
 +
|- align="right"
 +
!size||factor        ||4pt||5pt||6pt||7pt||8pt||9pt||10pt||11pt||12pt||14.4pt||17.3pt||20.7pt
 +
|- align="right"
 +
|xx  ||0.6          ||4  ||5  ||5  ||5  ||5  ||5  ||6  ||7  ||8  ||10    ||12    ||14.4
 +
|- align="right"
 +
|x  ||0.8          ||4  ||5  ||5  ||6  ||6  ||7  ||8  ||9  ||10  ||12    ||14.4  ||17.3
 +
|- align="right" style="background-color: #ffeebb;"
 +
|tf  ||1.0          ||4  ||5  ||6  ||7  ||8  ||9  ||10  ||11  ||12  ||14.4  ||17.3  ||20.7
 +
|- align="right"
 +
|a  ||1.2          ||4.8||6.0||7.2||8.4||9.6||10.8||12.0||13.2||14.4||17.3 ||20.7  ||24.8
 +
|- align="right"
 +
|b  ||1.44          ||5.8||7.2||8.6||10.1||11.5||13.0||14.4||15.8||17.3||20.7||24.9 ||29.8
 +
|- align="right"
 +
|c  ||1.728        ||6.9||8.6||10.4||12.1||13.8||15.6||17.3||19.0||20.7||24.9||29.9||35.8
 +
|- align="right"
 +
|d  ||2.074        ||8.3||10.4||12.4||14.5||16.6||18.7||20.7||22.8||24.9||29.9||35.9||42.9
 +
|-
 +
| _
 +
|- align="right"
 +
|scriptscript|| 0.5  ||4  ||5  ||5  ||5  ||5  ||5  ||5  ||6  ||7  ||9  ||10  ||12
 +
|- align="right"
 +
|script|| 0.7        ||4  ||5  ||5  ||6  ||6  ||7  ||7  ||8  ||9  ||11  ||12  ||14.4
 +
|- align="right"
 +
|small|| 0.8        ||4  ||4  ||5  ||5  ||6  ||7  ||8  ||9  ||10  ||12  ||14.4||17.3
 +
|- align="right"
 +
|big || 1.2          ||6  ||7  ||8  ||9  ||10 ||11 ||12 ||12 ||14.4||17.3||20.7||20.7
 +
|}
 +
 
 +
As defined in [[source:font-ini.mkvi|font-ini]]. All sizes in pt, except factor.
 +
 
  
To decrease the font size, you can use <cmd>tfx</cmd>
+
The mapping of particular command suffixes (a, x, etc.) and current bodyfont size to effective font size can
to scale the font by a factor of <code>0.8</code> and <cmd>tfxx</cmd> to scale by a factor
+
be changed by {{cmd|definebodyfontenvironment}}. The default values defined in [[source:font-ini.mkiv|font-ini]] are:
of <code>0.6</code>. The scale factors can be a function of the current font size and can
+
<texcode>
be changed by <cmd>definebodyfontenvironment</cmd>.
+
\definebodyfontenvironment
 +
  [\s!default]
 +
  [        \s!text=1.0,
 +
        \s!script=0.7,
 +
  \s!scriptscript=0.5,
 +
              \c!a=1.200,
 +
              \c!b=1.440,
 +
              \c!c=1.728,
 +
              \c!d=2.074,
 +
                *=\currentfontscale, % wildcard
 +
              \c!x=0.8,
 +
            \c!xx=0.6,
 +
            \c!big=1.2,
 +
          \c!small=0.8,
 +
\c!interlinespace=,
 +
            \c!em=\v!slanted]
 +
</texcode>
 +
'''N.B.''': <code>scirpt</code> and <code>scriptsize</code> are used in math mode.
  
For example, if you want
+
It is also possible to set the scaling factors for specific font sizes. For example, if you want {{cmd|tfa}} to set the effective font size to 12pt when the bodyfont size is 10pt, and to set the effective font size to  
<cmd>tfa</cmd> to be equal to 12pt when you are using 10pt font, and be equal to
+
14pt when the bodyfont size is 11pt, then add
14pt when you are using 11pt font, then add
 
 
<texcode>
 
<texcode>
 
\definebodyfontenvironment [10pt] [a=12pt]
 
\definebodyfontenvironment [10pt] [a=12pt]
 
\definebodyfontenvironment [11pt] [a=14pt]
 
\definebodyfontenvironment [11pt] [a=14pt]
 
</texcode>
 
</texcode>
<cmd>definebodyfontenvironment</cmd> is described in detail in  
+
 
the ConTeXt manual and the [[source:font-ini.tex|font-ini.tex]] source file.
+
'''Note:''' When the first parameter is <code>default</code>, the scaling factors ''must'' have numerical values. When the first parameter is a font size (e.g., <code>12pt</code>), the scaling factors can either be numerical value or a dimension.
 +
 
 +
{{cmd|definebodyfontenvironment}} is described in detail in  
 +
the ConTeXt manual and the [[source:font-ini.mkiv|font-ini]] source file.
  
 
Font size can be combined with font styles. As a shortcut, you can use
 
Font size can be combined with font styles. As a shortcut, you can use
<cmd>bfa</cmd> to get bold font scaled by <code>1.2</code>, <cmd>bfx</cmd> to get a bold font
+
{{cmd|bfa}} to get bold font scaled by <code>1.2</code>, {{cmd|bfx}} to get a bold font
 
scaled by <code>0.8</code> and similar commands for other font styles.
 
scaled by <code>0.8</code> and similar commands for other font styles.
  
These font size switches are meant for changing the font size of a few
+
These quick font switches are meant for changing the font style, alternative, or size of a few
words: they do not change the interline spacing and math font sizes. So, if
+
words: they do not change the bodyfont, so they don't affect interline spacing or math font sizes. So, if
 
you want to change the font size of an entire paragraph, use
 
you want to change the font size of an entire paragraph, use
<cmd>switchtobodyfont</cmd> described below in [[Font_Switching#Complete_font_change|Complete Font Change]]. However, it
+
{{cmd|switchtobodyfont}} described below in [[Font_Switching#Complete_font_change|Complete Font Change]]. However, it
 
is fine to use them as style directives in setup commands, that is, using them
 
is fine to use them as style directives in setup commands, that is, using them
as an option for <code>style=...</code> in any setup command that accepts
+
as an option for <code>[[Style_and_Color_Parameters|style=]]...</code> in any setup command that accepts
 
<code>style</code> option.
 
<code>style</code> option.
  
== Alternative font styles ==
+
== Mnemonic font switches ==
  
 
While learning a document markup language like ConTeXt, it can be hard to
 
While learning a document markup language like ConTeXt, it can be hard to
remember all the commands. ConTeXt provides easy to remember alternative
+
remember all the commands. ConTeXt provides other, easy-to-remember font
font styles. So for bold you can use <cmd>bold</cmd>, for italic you can use
+
switches. So for bold you can use {{cmd|bold}}, for italic you can use
<cmd>italic</cmd>, for slanted you can use <cmd>slanted</cmd>, and so on. You can
+
{{cmd|italic}}, for slanted you can use {{cmd|slanted}}, and so on. You can
 
probably guess what the following do:
 
probably guess what the following do:
  
 
{|
 
{|
  |  <cmd>normal</cmd>
+
  |  {{cmd|normal}}
  |  <cmd>slanted</cmd>
+
  |  {{cmd|slanted}}
 
  |-
 
  |-
  |  <cmd>boldslanted</cmd>
+
  |  {{cmd|boldslanted}}
  |  <cmd>slantedbold</cmd>
+
  |  {{cmd|slantedbold}}
 
  |-
 
  |-
  |  <cmd>bolditalic</cmd>
+
  |  {{cmd|bolditalic}}
  |  <cmd>italicbold</cmd>
+
  |  {{cmd|italicbold}}
 
  |-
 
  |-
  |  <cmd>small</cmd>
+
  |  {{cmd|small}}
  |  <cmd>smallnormal</cmd>
+
  |  {{cmd|smallnormal}}
 
  |-
 
  |-
  |  <cmd>smallbold</cmd>
+
  |  {{cmd|smallbold}}
  |  <cmd>smallslanted</cmd>
+
  |  {{cmd|smallslanted}}
 
  |-
 
  |-
  |  <cmd>smallboldslanted</cmd>
+
  |  {{cmd|smallboldslanted}}
  |  <cmd>smallslantedbold</cmd>
+
  |  {{cmd|smallslantedbold}}
 
  |-
 
  |-
  |  <cmd>smallbolditalic</cmd>
+
  |  {{cmd|smallbolditalic}}
  |  <cmd>smallitalicbold</cmd>
+
  |  {{cmd|smallitalicbold}}
 
  |-
 
  |-
  |  <cmd>sans</cmd>
+
  |  {{cmd|sans}}
  |  <cmd>sansserif</cmd>
+
  |  {{cmd|sansserif}}
 
  |-
 
  |-
  |  <cmd>sansbold</cmd>
+
  |  {{cmd|sansbold}}
  |  <cmd>smallcaps</cmd>
+
  |  {{cmd|smallcaps}}
 
  |}
 
  |}
  
In addition, the commands <cmd>smallbodyfont</cmd> and
+
In addition, the commands {{cmd|smallbodyfont}} and
<cmd>bigbodyfont</cmd> can be used to change the font size.
+
{{cmd|bigbodyfont}} can be used to change the font size. The relative size depends on the value of <code>big</code> and <code>small</code> in {{cmd|definebodyfontenvironment}}.
  
These alternative font styles are pretty smart. You can either use them as
+
These mnemonic font switches are pretty smart. You can either use them as
font style switches inside a group, or as a font changing command that takes an
+
font style switches inside a { ''group'' }, or as a font changing command that takes an
 
argument. For example,
 
argument. For example,
  
 
<context source="yes">
 
<context source="yes">
This is {\bold bold} and so is \bold{this}.
+
This is {\bold bold} and so is \bold{this}. But this is not.
 
</context>
 
</context>
  
These alternative font styles can also be used for all <code>style=...</code>
+
These mnemonic font switches can also be used for all <code>style=...</code>
 
options, and while using them as style options, you can just give the command
 
options, and while using them as style options, you can just give the command
name, for example:
+
name without the backslash. For example:
 
<texcode>
 
<texcode>
 
\setuphead[section][style=bold]  
 
\setuphead[section][style=bold]  
 
</texcode>
 
</texcode>
  
== Complete font change ==
+
(<code>\boldface</code> shown above is also defined to parallel the
 +
<code>\typeface</code> and <code>\swapface</code> switches.)
 +
 
 +
== Capitalizing words ==
 +
 
 +
* Note that {{cmd|WORD}}, {{cmd|Word}} and all following macros aren't ''switches'', but ''commands'' (brackets behind, not around): <tt>{</tt>{{cmd|em}} <tt>switch}</tt>, vs {{cmd|Word}}<tt>{macro}</tt>.
 +
 
 +
== Underline, strike through, and overline ==
 +
 
 +
* Underlined, struck, and overlined text can be achieved with {{cmd|overbar}} , {{cmd|overbars}}, {{cmd|overstrike}}, {{cmd|overstrikes}}, {{cmd|underbar}}, and {{cmd|underbars}}.
 +
 
 +
The canonical way is <code>\overstrike{text}</code>. However, it you're using mycrotypography with <code>\setupalign[hz,hanging]</code> expect the rule to be placed over the character (with the Latin Modern fonts you should not have this problem, experienced with Linux Libertine and mkiv). This is not what is expected. The workaround is to put the overstriked text in a <code>\inframed[frame=off]{}</code>.
 +
 
 +
E.g.
 +
<texcode>
 +
bla bla bla \inframed[frame=off]{\overstrike{striked}} bla bla
 +
</texcode>
 +
 
 +
<context>
 +
bla bla bla \inframed[frame=off]{\overstrike{striked}} bla bla
 +
</context>
 +
 
 +
Disabling mycrotypography also works, but probably not desiderable.
 +
<pre>
 +
\setupalign[nothanging,nohz]
 +
\strikeout{text}
 +
\setupalign[hanging,hz]
 +
</pre>
 +
 
 +
== Complete font (bodyfont) change ==
  
 
If you need to change to a different font size and take care of interline
 
If you need to change to a different font size and take care of interline
spacing, you can use <cmd>switchtobodyfont</cmd>. For example, to switch to 12pt
+
spacing, you can change the bodyfont by using {{cmd|switchtobodyfont}}. For example, to switch to 12pt
 
you can use <code>\switchtobodyfont[12pt]</code>.
 
you can use <code>\switchtobodyfont[12pt]</code>.
  
  
ConTeXt provides two relative sizes, called ''big'' and ''small''. So, to go to a
+
ConTeXt provides two relative sizes, called ''big'' and ''small''. So, to increase the bodyfont size, use <code>\switchtobodyfont[big]</code> (or <code>\setbigbodyfont</code>), and to decrease the bodyfont size, use  <code>\switchtobodyfont[small]</code> (or <code>\setsmallbodyfont</code>). The exact
bigger font size use <code>\switchtobodyfont[big]</code> and to use a
 
smaller font size use  <code>\switchtobodyfont[small]</code>. The exact
 
 
size used for big and small can be set using  
 
size used for big and small can be set using  
<cmd>definebodyfontenvironment</cmd>.
+
{{cmd|definebodyfontenvironment}}.
  
The <cmd>setupbodyfont</cmd> command accepts all the same arguments as
+
The {{cmd|setupbodyfont}} command accepts all the same arguments as
<cmd>switchtobodyfont</cmd>. The difference between the two is that
+
{{cmd|switchtobodyfont}}. The difference between the two is that
<cmd>setupbodyfont</cmd> also changes the font for headers, footers and other
+
{{cmd|setupbodyfont}} also changes the font for headers, footers and other
page markings, while <cmd>switchtobodyfont</cmd> does not. So you should use
+
page markings, while {{cmd|switchtobodyfont}} does not. So you should use
<cmd>setupbodyfont</cmd> for global font definitions to apply to the whole document, and
+
{{cmd|setupbodyfont}} for global font definitions to apply to the whole document, and
<cmd>switchtobodyfont</cmd> for local font changes. The effect of
+
{{cmd|switchtobodyfont}} for local font changes (i.e. changes to the running text only). The effect of
<cmd>switchtobodyfont</cmd> can be localized within a group as usual.
+
{{cmd|switchtobodyfont}} can be localized within a group as usual.
  
== Different typefaces ==
+
== Switching typefaces ==
  
 
So far we have discussed style and size changes within a given typeface
 
So far we have discussed style and size changes within a given typeface
 
family.  If you want to use a different typeface altogether, such as Times or
 
family.  If you want to use a different typeface altogether, such as Times or
Palatino, the Pragma web site has recipes covering all the commonly available
+
Palatino, use `\switchtobodyfont[times]` or `\swtichtobodyfont[palatino]`. Here `times` and `palatino` refer to the name of the typescript definitions for the font. ConTeXt distribution comes with some pre-defined typescripts; if you want to switch to another font, you need to define your own typescript. For details, see [[Fonts | the page on fonts]].
typefaces [http://pragma-ade.com/general/manuals/showfont.pdf], while a
+
 
separate manual describes how to write support for new typefaces
+
== From font-var.mkvi [https://source.contextgarden.net/tex/context/base/mkiv/font-var.mkvi?search=fontface#l49] ==
[http://pragma-ade.com/general/manuals/mfonts.pdf].
+
 
 +
<context source=yes>
 +
% fontclass      % modern dejavu
 +
% fontbody        % ... 10pt 11pt 12pt ...
 +
% fontstyle      % rm ss tt mm hw cg ...
 +
% fontalternative % tf bf sl it bs bi sc ...
 +
% fontsize        % a b c d ...
 +
% fontface        % 0=normal 1=text 2=script 3=scriptscript 4=x 5=xx (math are temporary)
 +
 
 +
class: \fontclass
 +
 
 +
body : \fontbody
 +
 
 +
style: \fontstyle
 +
 
 +
alternative: \fontalternative
 +
 
 +
size: \fontsize
  
The recipes as given work with the standalone ConTeXt distribution, but not
+
face: \fontface
with TeX Live et al.  [http://pragma-ade.com/general/technotes/tfmetrics.pdf]
 
explains why ConText uses separate font metrics, and gives some differences
 
between the sets. To use the recipes with other distributions,
 
try adding one of <code>\usetypescript[berry][ec]</code> or
 
<code>\usetypescript[adobekb][ec]</code>.
 
  
==  Conclusion ==
+
</context>
  
There are many other ways of choosing font styles in ConTeXt. If these
 
basic styles do not satisfy your needs, have a look at the [[Manuals|manuals]],
 
or ask on the [[ConTeXt Mailing Lists]].
 
  
 
[[Category:Fonts]]
 
[[Category:Fonts]]

Latest revision as of 23:09, 1 September 2020

In ConTeXt there are four ways to switch fonts:

Before you start ...

1. Placing a font switch at the begin of a paragraph can sometimes generate unexpected side-effects. This can be avoided by forcing horizontal mode before any font change:

\dontleavehmode{\bf Warning!} Care must be taken when a font switch is used at the beginning of a paragraph.\par

2. It is usually a bad style to use explicit font switches in the text. It is better to define a semantic command that takes care of the font switching. For example, instead of using {\bf\red important text}

\definehighlight[important][style=bold, color=red]

and then use \important{important text} in the text.

Font styles and alternatives

There are three main types of font styles: serif, sans serif, and teletype. To switch between these styles, use \rm for serif, \ss for sans serif, and \tt for teletype.

Each of these styles comes in different alternatives: normal, bold, italic, slanted, bold-italic, bold-slanted, and small-capped. To switch to a different alternative, use \tf ("typeface") for normal, \bf for bold, \it for italic, \sl for slanted, \bi for bold-italic, \bs for bold-slanted, and \sc for small-capped.

You can generally combine font styles with alternatives, so if you want to switch to bold sans serif, you can use either \bf\ss or \ss\bf. The various combinations look like the following.

A font switch remains valid for the rest of the group. So, if you want to temporarily switch to a different font, use the font style command inside a group. The easiest way to start a group is to enclose the text within braces (also called curly brackets), for example

This is serif text
{\ss This is sans serif}
This is serif again
{\tt and this is typewriter}
And serif again

which gives (notice the braces in the above lines)

Emphasizing text

There is a font switch \em to emphasize text. This is somewhat special: it does automatic italic correction and changes the alternative depending on the current alternative. For example, if the current font alternative is normal (i.e. upright), \em switches to slanted; and if the current font alternative is slanted, \em switches to normal.

ConTeXt uses the Latin Modern fonts by default; these fonts look similar to the original Computer Modern fonts, but have a much larger character repertoire. As it happens, in the Latin Modern (and Computer Modern) fonts, the slanted font does not stand out from the upright font enough for some tastes; so, many people prefer to use the italic font for emphasis. To do that use

\definebodyfontenvironment[default][em=italic]

NOTE: For Opentype fonts, italic correction needs to be set using
\definefontfeature[default][default][itlc=yes]

Font sizes

Occasionally one needs to use a font size different from the bodyfont. ConTeXt provides two series of commands for that. For a larger font size, you can use \tfa to scale the font size by a factor of 1.2, \tfb to scale by a factor of (1.2)^2 = 1.44, \tfc to scale by (1.2)^3 = 1.728 and \tfd to scale by (1.2)^4 = 2.074. The scale is relative to the current bodyfont size.


To decrease the font size, you can use \tfx to scale the font by a factor of 0.8 and \tfxx to scale by a factor of 0.6.

The complete set of relative font sizes are listed below:

size factor 4pt 5pt 6pt 7pt 8pt 9pt 10pt 11pt 12pt 14.4pt 17.3pt 20.7pt
xx 0.6 4 5 5 5 5 5 6 7 8 10 12 14.4
x 0.8 4 5 5 6 6 7 8 9 10 12 14.4 17.3
tf 1.0 4 5 6 7 8 9 10 11 12 14.4 17.3 20.7
a 1.2 4.8 6.0 7.2 8.4 9.6 10.8 12.0 13.2 14.4 17.3 20.7 24.8
b 1.44 5.8 7.2 8.6 10.1 11.5 13.0 14.4 15.8 17.3 20.7 24.9 29.8
c 1.728 6.9 8.6 10.4 12.1 13.8 15.6 17.3 19.0 20.7 24.9 29.9 35.8
d 2.074 8.3 10.4 12.4 14.5 16.6 18.7 20.7 22.8 24.9 29.9 35.9 42.9
_
scriptscript 0.5 4 5 5 5 5 5 5 6 7 9 10 12
script 0.7 4 5 5 6 6 7 7 8 9 11 12 14.4
small 0.8 4 4 5 5 6 7 8 9 10 12 14.4 17.3
big 1.2 6 7 8 9 10 11 12 12 14.4 17.3 20.7 20.7

As defined in font-ini. All sizes in pt, except factor.


The mapping of particular command suffixes (a, x, etc.) and current bodyfont size to effective font size can be changed by \definebodyfontenvironment. The default values defined in font-ini are:

\definebodyfontenvironment
  [\s!default]
  [        \s!text=1.0,
         \s!script=0.7,
   \s!scriptscript=0.5,
              \c!a=1.200,
              \c!b=1.440,
              \c!c=1.728,
              \c!d=2.074,
                 *=\currentfontscale, % wildcard
              \c!x=0.8,
             \c!xx=0.6,
            \c!big=1.2,
          \c!small=0.8,
 \c!interlinespace=,
             \c!em=\v!slanted]

N.B.: scirpt and scriptsize are used in math mode.

It is also possible to set the scaling factors for specific font sizes. For example, if you want \tfa to set the effective font size to 12pt when the bodyfont size is 10pt, and to set the effective font size to 14pt when the bodyfont size is 11pt, then add

\definebodyfontenvironment [10pt] [a=12pt]
\definebodyfontenvironment [11pt] [a=14pt]

Note: When the first parameter is default, the scaling factors must have numerical values. When the first parameter is a font size (e.g., 12pt), the scaling factors can either be numerical value or a dimension.

\definebodyfontenvironment is described in detail in the ConTeXt manual and the font-ini source file.

Font size can be combined with font styles. As a shortcut, you can use \bfa to get bold font scaled by 1.2, \bfx to get a bold font scaled by 0.8 and similar commands for other font styles.

These quick font switches are meant for changing the font style, alternative, or size of a few words: they do not change the bodyfont, so they don't affect interline spacing or math font sizes. So, if you want to change the font size of an entire paragraph, use \switchtobodyfont described below in Complete Font Change. However, it is fine to use them as style directives in setup commands, that is, using them as an option for style=... in any setup command that accepts style option.

Mnemonic font switches

While learning a document markup language like ConTeXt, it can be hard to remember all the commands. ConTeXt provides other, easy-to-remember font switches. So for bold you can use \bold, for italic you can use \italic, for slanted you can use \slanted, and so on. You can probably guess what the following do:

\normal \slanted
\boldslanted \slantedbold
\bolditalic \italicbold
\small \smallnormal
\smallbold \smallslanted
\smallboldslanted \smallslantedbold
\smallbolditalic \smallitalicbold
\sans \sansserif
\sansbold \smallcaps

In addition, the commands \smallbodyfont and \bigbodyfont can be used to change the font size. The relative size depends on the value of big and small in \definebodyfontenvironment.

These mnemonic font switches are pretty smart. You can either use them as font style switches inside a { group }, or as a font changing command that takes an argument. For example,

This is {\bold bold} and so is \bold{this}. But this is not.

These mnemonic font switches can also be used for all style=... options, and while using them as style options, you can just give the command name without the backslash. For example:

\setuphead[section][style=bold] 

(\boldface shown above is also defined to parallel the \typeface and \swapface switches.)

Capitalizing words

  • Note that \WORD, \Word and all following macros aren't switches, but commands (brackets behind, not around): {\em switch}, vs \Word{macro}.

Underline, strike through, and overline

The canonical way is \overstrike{text}. However, it you're using mycrotypography with \setupalign[hz,hanging] expect the rule to be placed over the character (with the Latin Modern fonts you should not have this problem, experienced with Linux Libertine and mkiv). This is not what is expected. The workaround is to put the overstriked text in a \inframed[frame=off]{}.

E.g.

bla bla bla \inframed[frame=off]{\overstrike{striked}} bla bla

Disabling mycrotypography also works, but probably not desiderable.

\setupalign[nothanging,nohz]
\strikeout{text}
\setupalign[hanging,hz]

Complete font (bodyfont) change

If you need to change to a different font size and take care of interline spacing, you can change the bodyfont by using \switchtobodyfont. For example, to switch to 12pt you can use \switchtobodyfont[12pt].


ConTeXt provides two relative sizes, called big and small. So, to increase the bodyfont size, use \switchtobodyfont[big] (or \setbigbodyfont), and to decrease the bodyfont size, use \switchtobodyfont[small] (or \setsmallbodyfont). The exact size used for big and small can be set using \definebodyfontenvironment.

The \setupbodyfont command accepts all the same arguments as \switchtobodyfont. The difference between the two is that \setupbodyfont also changes the font for headers, footers and other page markings, while \switchtobodyfont does not. So you should use \setupbodyfont for global font definitions to apply to the whole document, and \switchtobodyfont for local font changes (i.e. changes to the running text only). The effect of \switchtobodyfont can be localized within a group as usual.

Switching typefaces

So far we have discussed style and size changes within a given typeface family. If you want to use a different typeface altogether, such as Times or Palatino, use \switchtobodyfont[times] or \swtichtobodyfont[palatino]. Here times and palatino refer to the name of the typescript definitions for the font. ConTeXt distribution comes with some pre-defined typescripts; if you want to switch to another font, you need to define your own typescript. For details, see the page on fonts.

From font-var.mkvi [1]

% fontclass       % modern dejavu
% fontbody        % ... 10pt 11pt 12pt ...
% fontstyle       % rm ss tt mm hw cg ...
% fontalternative % tf bf sl it bs bi sc ...
% fontsize        % a b c d ...
% fontface        % 0=normal 1=text 2=script 3=scriptscript 4=x 5=xx (math are temporary)

class: \fontclass

body : \fontbody

style: \fontstyle

alternative: \fontalternative

size: \fontsize

face: \fontface