Difference between revisions of "Titles"

From ConTeXt wiki
(Your Own Title Styles)
(independently numbered heading)
Line 374: Line 374:
  
 
See the mailing list thread [[http://www.ntg.nl/pipermail/ntg-context/2003/002053.html]] for more on this.
 
See the mailing list thread [[http://www.ntg.nl/pipermail/ntg-context/2003/002053.html]] for more on this.
 +
 +
===Headings numbered independent of chapters or other headings===
 +
 +
If you want a heading that gets numbered without resetting at new sections or chapters (for example, for problems whose numbers increment throughout a book), here is a solution due to Hans [http://archive.contextgarden.net/message/20030415.153451.374310d8.en.html on the mailing list]:
 +
 +
<texcode>
 +
% use a lower level head
 +
 +
\definehead
 +
[Problem]
 +
[subsubsubsection]
 +
 +
% cleaned up
 +
 +
\setuphead
 +
[Problem]
 +
[style=\ss\bf,
 +
color=red,
 +
before=\blank]
 +
 +
% use a label text
 +
 +
\setuplabeltext
 +
[Problem=Problem ]
 +
 +
% use your own numbers
 +
 +
\setuphead
 +
[Problem]
 +
[ownnumber=yes]
 +
 +
% increment and feed
 +
 +
\newcounter\ProblemNumber
 +
 +
\def\problem#1%
 +
{\doglobal\increment\ProblemNumber
 +
\Problem{\ProblemNumber}{#1}}
 +
 +
% nicer here
 +
 +
\setuplist
 +
[Problem]
 +
[alternative=c,
 +
criterium=all,
 +
width=2em,
 +
interaction=all]
 +
 +
% the test
 +
 +
\starttext
 +
 +
List of Problems
 +
 +
\blank[big] \placelist[Problem] \blank[big]
 +
 +
\section{Tufte}
 +
 +
\input tufte
 +
 +
\problem{First problem}
 +
 +
The first problem description.
 +
 +
\section{Another Tufte}
 +
 +
\input tufte
 +
 +
\problem{Second problem}
 +
 +
Second problem description.
 +
 +
\stoptext
 +
</texcode>

Revision as of 18:43, 29 November 2008

< Structurals | Visuals >

Default

Titles define the structure of your document. Decide yourself, how deep you want to nest them:

numberedunnumbered
\part&#151;
\chapter\title
\section\subject
\subsection\subsubject
\subsubsection\subsubsubject
......

The "unnumbered" versions don't appear in a table of contents! But you can switch off the numbering of the "numbered" versions with \setuphead to get unnumbered titles and a toc (confusing?).

Referencing Titles

Every heading command can take an optional parameter as reference:

\title[hasselt-by-night]{Hasselt by night}

The bracket pair is optional and used for internal References. If you want to refer to this header you type for example

\at{page}[hasselt-by-night].

(see \at)

Titling Style

see \setuphead and some enhanced samples below.

A FAQ is how to get a line under the title:

\setupbackgrounds[header][text][bottomframe=on]

To make the section numbers appear as characters, do

\setupsection[section-3][bodypartconversion=Character]

The section-3 corresponds to section. Similarly, section-1 corresponds to part, section-2 corresponds to chapter and so on.

Titling Alignment

Titles should often be left aligned without hyphenation. Try this:

\setuphead[chapter][align={flushleft, nothyphenated, verytolerant}] % "flushleft" is the same as "right"

Using colors in chapters and sections

If you want you may have colors on your document's chapters or sections. To do that you should use \setuphead. Take a look at the following example:

\setupcolors[state=start] % Important. You won't have colors without it.

\setuphead[chapter][header=empty] % Chapter pages won't have headers

\setupheadertexts[][chapter]      %  The header will be the chapter's name

\starttext

\completecontent

\chapter{Black chapter 1}

\setuphead[section][color=darkcyan]  % This is how you change your chapter's color.

\section{Dark cyan section 1.1}

\page
Test.

\setuphead[chapter][color=darkgreen]

\chapter{Dark green chapter}

\page
Test.

\setuphead[chapter][color=red]

\chapter{Red chapter}

\page
Test.

\stoptext 

Your Own Titling Levels

Of course you can define your own titling commands and probably must adapt the default settings.

\definehead[myheader][section]

\setuphead[myheader]
[numberstyle=bold,
textstyle=cap,
before=\hairline\blank,
after=\nowhitespace\hairline]

\myheader[hasselt-ref]{Hasselt makes headlines}

A new header \myheader is defined and it inherits the properties of \section (title, subject, whatever). You can "define" several headers at once!

Formatting Titles with \setuphead

\setuphead accepts a number of parameters which change the style of the heading. At least the following commands are available:

textnumber
textstylenumberstyle
textcommandnumbercommand
deeptextcommanddeepnumbercommand

Quite obviously, the number-commands work on the chapter/section/etc. number, their text counterparts change the text itself. However, there are differences between style, command and deepcommand.

The style variants accept a style (bold, cap, etc.) or a font switch (\em, \tfx, etc). texstyle prepends the text with the associated style. textcommand is a command name which is given the text (with all markup) as a parameter. deeptextcommand is similar to textcommand but it acts only on the text (not on the markup).

So, if we have \title{A story}, the different parameters have the following results:

textstyle=\em=>\em A story
textstyle=\em, textcommand=\uppercase=>\uppercase{\em A story}=>\em A STORY
textstyle=\em, deeptextcommand=\uppercase=>\em\uppercase{A story}=>\em A STORY

What is the difference between textcommand and deeptextcommand, then? With this example there is very little difference, as uppercase knows how to handle markup. Sometimes this is not the case. For example, an almost similar command WORD is not compatible with textcommand, because it wreaks havoc with markup:

textstyle=\em, textcommand=\WORD=>\WORD{\em A story}=>\EM A STORY
textstyle=\em, deeptextcommand=\WORD=>\em\WORD{A story}=>\em A STORY

In this case using deeptextcommand is the correct solution for capitalizing headers. (Using \uppercase works in this specific case, but only if you stick to Latin 1 or Anglo-Saxon characters. Other languages will suffer from lowercase accented characters, like CAFé.)

Truly empty pagebreak before chapters

Using \setuppagenumbering[alternative=doublesided] makes the chapters start on the right page. However, the blank page is not truely empty, it contains headers and footers. To get truely empty pages, use the following

 \definepagebreak
    [mychapterpagebreak]
    [yes,header,right]
 %    [yes,header,left]
 %    [yes,header,footer,right]

 \setuphead
   [chapter]
   [page=mychapterpagebreak]
 %   [page=Mychapterpagebreak,header=empty,footer=empty]

% \setupsectionblock[frontpart][page=chapter]
% \setupsectionblock[bodypart][page=chapter]
% \setupsectionblock[backpart][page=chapter]
% \setupsectionblock[appendix][page=chapter]

 \setuppagenumbering[alternative=doublesided,location={header,margin}]
 \setupheadertexts[{My special headertext}]
 \setupfootertexts[This is a text in the footer]
 % \setupbackgrounds[header][text][bottomframe=on]

 \starttext
     \chapter {testA} \dorecurse{10}{\input tufte }
     \chapter {testB} \dorecurse{10}{\input tufte }
     \chapter {testC} \dorecurse{10}{\input tufte }
 \stoptext

Explaination (provided by Willi Egger on the mailing list)

  • You define a new pagebreak rule. It has the name mychapterpagebreak. The options set read as: pagebreak=yes, placeheader, use a right page.
  • You set options for the header of type chapter and use for the option page the before defined new pagebreak.
  • You might want to experiment with the commented lines and see what happens. For example, the \setupsectionblock commands will give a truly blank page (if needed) after the specified \stop*part command.

Independent Section Numbering

If you want section numbering to be independent of chapter numbering, use

 \setuphead   [chapter][resetnumber=no]
 \setupsection[section][previousnumber=no]


Your Own Title Styles

Sometimes the possibilities of \setuphead aren't enough. You can define your own styling commands, as shown in the following examples.

Expanded chapter titles

This example illustrates expanded chapter titles.

\def\MyChapterCommand#1#2{\framed[frame=off, bottomframe=on, topframe=on]{\vbox{\headtext{chapter} #1\blank#2}}}
% #1 is number, #2 is text
% \vbox is needed for \blank to work

\setuphead[chapter][command=\MyChapterCommand, style={\ss\bfa}]

\setupheadtext[chapter=Chapter] % used by \headtext

so \chapter{My First Chapter} looks like:

Exercise numbers

For a textbook, suppose that you collect the exercises in a section at the end of each chapter, with each exercise a subsection having a short title, and the exercises should be numbered only by the subsection (not 1.6.7 for example, just 7). The usage:

\section{Exercises}

\exercise{Batteries}
What is the cost of energy from a 9V battery?  From a wall socket (the
mains)?

The setup code:

\definehead[exercise][subsection]
\setuphead[exercise][style=italic, 
numbercommand={\determineheadnumber[subsection]\currentheadnumber
\gobbleoneargument}]

A complex graphical element under the chapter title

\setupcolors[state=start]
\startuseMPgraphic{HeaderDeco}
    numeric w, h, repeats;
    path p[];
    w := OverlayWidth ; h := OverlayHeight ;
    repeats := abs(TextWidth/BodyFontSize);
    p[1] := unitsquare xscaled w yscaled h ;
    draw p[1] withcolor white;
    p[2] := fullcircle scaled BodyFontSize;
    p[3] := fullcircle scaled .25BodyFontSize;
    draw p[2] shifted (.5BodyFontSize,0);
    for i = 1 upto repeats:
        if odd i :
            filldraw p[3] shifted (i*BodyFontSize+.5BodyFontSize,0);
        else :
            draw p[2] shifted (i*BodyFontSize+.5BodyFontSize,0);
        fi;
    endfor;
\stopuseMPgraphic

\setuphead[chapter][command=\Myheader]
\setupheadtext
      [chapter=Chapitre]
\defineoverlay[HeaderDeco][\uniqueMPgraphic{HeaderDeco}]

\def\Myheader#1#2{%
    \framedtext
        [width= \overlaywidth,
        height=6\bodyfontsize,
        background={foreground,HeaderDeco},
        offset=0pt,
        strut=no,
        frame=off,
        align=middle]{%
            \headtext{chapter} #1
            \blank[small]
            #2}}

\starttext
   \chapter{Here we go!}
\stoptext

It looks like:

Titles in margin

It is sometimes wanted to place the title of a section/subject etc in the margin next to the section text (instead of above it). This is achieved by the following setup:

\setuphead
   [subject]
   [alternative=text,distance=0pt,command=\MyHead]

\def\MyHead#1#2{\inmargin{#1 #2}}

This (with 'style=\sc' added in setuphead) gives:

See the mailing list thread [[1]] for more on this.

Headings numbered independent of chapters or other headings

If you want a heading that gets numbered without resetting at new sections or chapters (for example, for problems whose numbers increment throughout a book), here is a solution due to Hans on the mailing list:

% use a lower level head

\definehead
[Problem]
[subsubsubsection]

% cleaned up

\setuphead
[Problem]
[style=\ss\bf,
color=red,
before=\blank]

% use a label text

\setuplabeltext
[Problem=Problem ]

% use your own numbers

\setuphead
[Problem]
[ownnumber=yes]

% increment and feed

\newcounter\ProblemNumber

\def\problem#1%
{\doglobal\increment\ProblemNumber
\Problem{\ProblemNumber}{#1}}

% nicer here

\setuplist
[Problem]
[alternative=c,
criterium=all,
width=2em,
interaction=all]

% the test

\starttext

List of Problems

\blank[big] \placelist[Problem] \blank[big]

\section{Tufte}

\input tufte

\problem{First problem}

The first problem description.

\section{Another Tufte}

\input tufte

\problem{Second problem}

Second problem description.

\stoptext