Changes

Jump to navigation Jump to search
14,499 bytes added ,  18:48, 17 October 2012
new article, beginner-level introduction to the omnipresent ''style''-parameter
== Style Alternatives ==

ConTeXt provides a consistent interface to a variety of font properties
through the pervasive ''style'' parameter.
This parameter accepts high-level ''style alternatives'' like
<code>bold</code> and <code>slanted</code> which should suffice in
common situations.
Additionally, raw font switching by means of low-level macros, e.g.
{{cmd|WORD|link=no}} and {{cmd|bi|link=no}}, is supported to
accommodate more complicated requirements.

== Usage Examples ==

=== Headings ===
Structural elements all have a ''style'' key, so this
[[Titles#Formatting_Titles_with_.5Csetuphead|works with all titles]]
from {{cmd|part|link=no}} to {{cmd|subsection|link=no}} and,
of course, their respective clones.
Additionally the number and the title string can be styled
independently through the parameters ''numberstyle'' and ''textstyle'',
respectively.

<texcode>
%% general style
\setuphead [section] [style=bold]
\setuphead [subsection] [style=smallcaps]

%% clones inherit
\definehead [nothersection] [section]

%% separate configuration of the number and text style
\definehead [yetanothersection] [section] [
numberstyle=bold,
textstyle=smallcaps,
]

\starttext
\section{foo}
\subsection{bar}
\nothersection{baz}
\yetanothersection{xyzzy}
\stoptext

</texcode>

=== Header and Footer ===

The content of headers and footers is configurable via the ''style''
parameter too, so are page numbers.
(Cf. {{cmd|setupheader}}, {{cmd|setupfooter}} and
{{cmd|setuppagenumbering}}.)
The following example shows all three in action.

<texcode>
%% current section title in head
\setupheadertexts[section]
\setupfootertexts[{My favorite Ed Tufte quotes}][pagenumber]
%% set style of head and foot
\setupheader[style=smallcaps]
\setupfooter[style=italic]
%% set the style of the page number
\setuppagenumbering[style=mediaeval,location=]


\starttext %% demo
\dorecurse{15}{%
\startsection[title={Iteration: \romannumeral\recurselevel}]
\input tufte
\page
\stopsection
}
\stoptext
</texcode>

''Caveat emptor'':
Observe how ''style parameters may or may not be additive'', i.e. the page footer
is typeset in italics while the page number inside said footer is set
in both italics and text figures.
In above code this is an artifact of the ''mediaeval'' style which
internally triggers a font feature.
Genuine font switches do not exhibit this behavior.
If the pagenumbering had the option <code>style=bold</code> instead,
this would not result in the number being typeset in bold italics.
Therefore one should not rely on the outcome being a a union of
both styles.
Rather, either use the proper alternative ''bolditalic'', if it exists,
or define a custom style alternative as shown below.

== List of Pre-Defined Styles ==

=== Type Face ===

The following styles set the text with the respective switch enabled:

{|
| ''normal'' || <code>\tf</code> || regular
|-
| ''italic'' || <code>\it</code> || italics
|-
| ''bold'' || <code>\bf</code> || bold
|-
| ''bolditalic'' || <code>\bi</code> || bold italic
|-
| ''type'', ''mono'' || <code>\tt</code> || non-proportional
|-
| ''slanted'' || <code>\sl</code> || slanted (oblique)
|-
| ''boldslanted'' || <code>\sl</code> || bold slanted
|}

<texcode>
\definehighlight [myregular] [style=normal]
\definehighlight [myitalics] [style=italic]
\definehighlight [knuthsitalics] [style=slanted]
\definehighlight [mybold] [style=bold]
\definehighlight [mybolditalics] [style=bolditalic]
\definehighlight [mytypewriter] [style=type]
\definehighlight [myboldslanted] [style=boldslanted]

\starttext
{\bold before \myregular{text} after}\par
before \myitalics{text} after \par
before \knuthsitalics{text} after \par
before \mybold{text} after \par
before \mybolditalics{text} after \par
before \mytypewriter{text} after \par
before \myboldslanted{text} after \par
\stoptext
</texcode>

Sans serif variants can be accessed via the values ''sans'',
''sansserif'' and ''sansbold''.

=== Font Size ===

Some, but not all of the above have a corresponding ''small...''
version. ''small'', ''smaller'' and ''smallnormal'' are synonyms.

<texcode>
\definehighlight [mysmall] [style=small]
\definehighlight [mysmallbold] [style=smallbold]
\definehighlight [mysmallmono] [style=smalltype]
\definehighlight [mysmallslanted] [style=smallslanted]
%% etc. ...
\definehighlight [mysmaller] [style=smaller]
\definehighlight [mybigger] [style=bigger]

\starttext
before \mysmall {\CONTEXT\ is great} after \par
before \mysmallmono {\CONTEXT\ is great} after \par
before \mysmallbold {\CONTEXT\ is great} after \par
before \mysmallslanted{\CONTEXT\ is great} after \par
{\bold before \mysmaller {\CONTEXT\ is great} after} \par
{\bold before \mybigger {\CONTEXT\ is great} after} \par
\stoptext
</texcode>

Apart from that, there are the following font size switches that do not
explicitly set a typeface, thus preserving the style of the surrounding
text:

{|
| ''smallbodyfont'' || smaller
|-
| ''bigbodyfont'' || larger
|}

<texcode>
\definehighlight [mysmall] [style=small]
\definehighlight [mysmallerfont] [style=smallbodyfont]
\definehighlight [mybiggerfont] [style=bigbodyfont]

\starttext
{\bold before \mysmall {\CONTEXT\ is great} after} \par
{\bold before \mysmallerfont {\CONTEXT\ is great} after} \par
{\bold before \mybiggerfont {\CONTEXT\ is great} after} \par
\stoptext
</texcode>

=== Case and Capitalization ===

There are two alternatives for small capitals:
{|
| ''smallcaps'' || the real ones, as far as they are supported by the font
|-
| ''capital'' || pseudo small caps
|}

Apart from these, the character casing switches are valid style
alternatives as well:
{|
| ''word'' || lower case
|-
| ''WORD'' || upper case
|-
| ''Word'' || upper first
|-
| ''Words'' || title case (no exceptions)
|}

<texcode>
\definehighlight [mysmallcaps] [style=smallcaps] %% real small caps
\definehighlight [myfakecaps] [style=capital] %% pseudo caps
\definehighlight [myword] [style=word] %% lower case
\definehighlight [myWORD] [style=WORD] %% upper case
\definehighlight [myWord] [style=Word] %% First work capitalized
\definehighlight [myWords] [style=Words] %% Title Case All

\starttext
foo \mysmallcaps{bar} baz\par
foo \myfakecaps{bar} baz\par
foo \myword{BAR} baz\par
foo \myWORD{bar} baz\par
foo \myWord{the bar in the baz} baz\par
foo \myWords{the bar in the baz} baz\par
\stoptext
</texcode>

=== Miscellaneous ===

''mediaeval'' will get you text figures (lowercase numbers; very handy
when typesetting page numbers).
Assuming an appropriate font setup you can also request ''handwritten''
and ''calligraphic'' (<code>\hw</code>, <code>\cg</code>).

<texcode>
\definehighlight [mytextfigures] [style=mediaeval]

%% [configure your fonts here]
\definehighlight [myhandwriting] [style=handwritten]
\definehighlight [mycalligraphy] [style=calligraphic]

\starttext
123 \mytextfigures{456} 789 \par
foo \myhandwriting{can you read this?} bar
foo \mycalligraphy{ain’t this pretty?} bar
\stoptext
</texcode>

== Defining a Style Alternative ==

The governing macro generator is {{cmd|definealternativestyle}}.
It takes three arguments in brackets.
The first one is the identifier, or a comma list of identifiers, that
will be used to assign the style to an element.
The second argument contains the tokens that are executed to achieve
the style.

For example, to hook custom styles into your headings, have a look at
below snippet:

<texcode>
%% define some weird font combinations
\definealternativestyle [head:large] [\ssd\bold] []
\definealternativestyle [head:fancy] [\ssb\italic] []
\definealternativestyle [head:neat] [\tf\word\sc] []

%% use as any other style parameter
\setuphead [chapter] [style=head:large]
\setuphead [section] [style=head:fancy]
\setuphead [subsection] [style=head:neat]

\starttext
\dorecurse{3}
{\startchapter[title=foo]
\dorecurse{2}
{\startsection[title=bar]
\dorecurse{5}
{\startsubsection[title=baz]\input dawkins\stopsubsection}
\stopsection}
\stopchapter}
\stoptext
</texcode>

Above definition prevents, however, nested style definitions.
It will not work if an outer style is already active, as for example
with the ''textstyle'' parameter of headings:

<texcode>
\definealternativestyle [head:neat] [\WORD] []
\definealternativestyle [head:title:neat] [\bold] []
\definealternativestyle [head:num:neat] [\italic] []

\setuphead [subsection] [
style=head:neat, %% outer style
numberstyle=head:num:neat, %% inner ...
textstyle=head:title:neat, %% ... styles
]
...
</texcode>

The rationale behind this is simply that too much markup is rarely
desirable -- a hint that the author prefers his word processor to
typographical rigor. <!-- just trolling ;-) -->
To override this behavior you can specify what happens in case of
nested styles by setting the third parameter.

<texcode>
\definealternativestyle [head:neat] [\WORD] []
\definealternativestyle [head:title:neat] [\bold] [\word\sc]
\definealternativestyle [head:num:neat] [\italic] [\slanted]

\setuphead [subsection] [
style=head:neat, %% inner
numberstyle=head:num:neat, %% additional ...
textstyle=head:title:neat, %% ... style layers
]
...
</texcode>

== Deploying a Style Handler in Your Macros ==
The ConTeXt namespacing model allows for painless integration of a
style parameter in user macros.
This is achieved by means of a dedicated ''style handler'' that may be
installed separately with {{cmd|installstyleandcolorhandler}} or
implicitly with {{cmd|installcommandhandler}} and other helpers.
(Cf. {{src|mult-aux.mkiv}}.)

In this mini-tutorial we first create the namespace ''look'' that we
will use for our personal highlight generator.
Using this namespace, we register a ''command handler'' that will get
us started with
{|
| a parameter handler || key value arguments
|-
| a macro generator || <code>\define...</code>
|-
| a setup handler || like {{cmd|getparameters}}, only better
|-
| the ''style and color handler'' || the point of this exercise.
|}

<texcode>
% macros=mkvi
\unprotect %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\installnamespace {look}
\installcommandhandler \????look {look} \????look
</texcode>

Now we can to do something with the command:
Whenever {{cmd|definelook|link=no}} is called, it will store its first
argument inside {{cmd|currentlook|link=no}}.
This is supposed to be the name for the new ''look'' macro, so we
define it as a wrapper for the actual functionality with the name as
first argument ({{cmd|currentlook|link=no}} will be expanded along the
way):

<texcode>
\appendtoks
\setuevalue{\currentlook}{\do_look{\currentlook}}
\to \everydefinelook
</texcode>

Where {{cmd|do_look|link=no}} is defined as follows:

<texcode>
\unexpanded\def\do_look#id{%
\edef\currentlook{#id}%
\dosingleempty\do_do_look%
}
</texcode>

&ndash; yet another wrapper? Sure, because this way we get the optional
first argument by means of {{cmd|dosingleempty}}.

After these two layers of packaging we finally reach the point where we
can deploy our ''style handler''.
First of all we need the style changes to be local, hence the grouping.
Then we take care of our optional setup arguments: these enable us to
override the style of a derived look macro whenever we want to.
Needless to say these optional setups are local too so our macros
return safely to their global behavior afterwards.

The macro {{cmd|uselookstyleandcolor|link=no}} takes two arguments, one
for the ''name'' of the parameter that identifies the style, and
another for the color.
This allows for multiple styles for different purposes like for
instance in {{cmd|setuphead}} which respects the ''numberstyle'' and
''textstyle'' keys in addition to the generic ''style'' key.
Our example is much less elaborate so we stick to the ordinary
''style'' and ''color'' (here accessed through their interface
constants {{cmd|c!style|link=no}} and {{cmd|c!color|link=no}}).

The final item inside the group is the mandatory argument
<code>#content</code>, the name of which should be self-explanatory.

<texcode>
\def\do_do_look[#parms]#content{%
\begingroup
\iffirstargument\setupcurrentlook[#parms]\fi
\uselookstyleandcolor\c!style\c!color%
#content%
\endgroup%
}

\protect %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
</texcode>

Now we are ready to define some ''look''s.
The {{cmd|definelook|link=no}} has been automatically created by the
instruction {{cmd|installcommandhandler}} above.
It works just like any ordinary user-level command and supports
inheritance out of the box.

<texcode>
\definelook [mybold] [style=bold]
\definelook [myslant] [style=slanted]
\definelook [myitalics] [style=italic]
\definelook [mybig] [style=bigger]
\definelook [mycaps] [style=smallcaps]
\definelook [complicated] [style={\ssxx\bold\addff{oldstyle}}]
</texcode>

(Note that we restrict ourselves to the ''style'' parameter here but in
theory the ''color'' equivalent should work as well.)

Now these defined macros all reference {{cmd|do_look|link=no}} and
{{cmd|do_look|link=no}} but behave differently according to their
setups.
We can now test if our effort was a success:

<texcode>
\starttext %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Ordinary Text Before
\mybold {bold text}
\myslant {slanted text}
\myitalics {italic text}
\mybig {oversize text}
\mycaps {small capitals}
\complicated {0.5 + 14.134725i}
Ordinary Text After

\hairline

\mycaps[style=bold]{small capitals?} %% an exception using the local setup:
\mycaps {small capitals!} %% back to normal

\stoptext %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
</texcode>

(Complete gist: [https://gist.github.com/3905799].)

== Further Reading ==

* The command handlers are defined in {{src|font-sty.mkvi}}.
* The predefined style alternatives reside in {{src|font-pre.mkiv}}.
* The alternatives for capitalization are defined in {{src|typo-cap.mkiv}}.
* There is a wiki page on [[Font Switching]].
* Generic emphases ({{cmd|definehighlight}}) are probably the simplest mechanism in ConTeXt to support the ''style'' parameter.

[[Category:Fonts]]
188

edits

Navigation menu