Difference between revisions of "Style Alternatives"

From Wiki
Jump to navigation Jump to search
(new article, beginner-level introduction to the omnipresent ''style''-parameter)
 
Line 417: Line 417:
  
 
Now these defined macros all reference {{cmd|do_look|link=no}} and
 
Now these defined macros all reference {{cmd|do_look|link=no}} and
{{cmd|do_look|link=no}} but behave differently according to their
+
{{cmd|do_do_look|link=no}} but behave differently according to their
 
setups.
 
setups.
 
We can now test if our effort was a success:
 
We can now test if our effort was a success:

Revision as of 20:43, 17 October 2012

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 bold and slanted which should suffice in common situations. Additionally, raw font switching by means of low-level macros, e.g. \WORD and \bi, is supported to accommodate more complicated requirements.

Usage Examples

Headings

Structural elements all have a style key, so this works with all titles from \part to \subsection and, of course, their respective clones. Additionally the number and the title string can be styled independently through the parameters numberstyle and textstyle, respectively.

%% 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

Header and Footer

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

%% 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

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 style=bold 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 \tf regular
italic \it italics
bold \bf bold
bolditalic \bi bold italic
type, mono \tt non-proportional
slanted \sl slanted (oblique)
boldslanted \sl bold slanted
\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

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.

\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

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
\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

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)
\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

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 (\hw, \cg).

\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

Defining a Style Alternative

The governing macro generator is \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:

%% 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

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:

\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
]
...

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. To override this behavior you can specify what happens in case of nested styles by setting the third parameter.

\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
]
...

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 \installstyleandcolorhandler or implicitly with \installcommandhandler and other helpers. (Cf. 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 \define...
a setup handler like \getparameters, only better
the style and color handler the point of this exercise.
% macros=mkvi
\unprotect %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\installnamespace {look}
\installcommandhandler \????look {look} \????look

Now we can to do something with the command: Whenever \definelook is called, it will store its first argument inside \currentlook. 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 (\currentlook will be expanded along the way):

\appendtoks
  \setuevalue{\currentlook}{\do_look{\currentlook}}
\to \everydefinelook

Where \do_look is defined as follows:

\unexpanded\def\do_look#id{%
  \edef\currentlook{#id}%
  \dosingleempty\do_do_look%
}

– yet another wrapper? Sure, because this way we get the optional first argument by means of \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 \uselookstyleandcolor 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 \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 \c!style and \c!color).

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

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

\protect %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Now we are ready to define some looks. The \definelook has been automatically created by the instruction \installcommandhandler above. It works just like any ordinary user-level command and supports inheritance out of the box.

\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}}]

(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 \do_look and \do_do_look but behave differently according to their setups. We can now test if our effort was a success:

\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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

(Complete gist: [1].)

Further Reading