Titles

From Wiki
Jump to navigation Jump to search

Default

Titles define the structure of your document. Decide yourself how deep you want to nest them, there are 12 levels of depth predefined:

Numbered Unnumbered
\part
\chapter \title
\section \subject
\subsection \subsubject
\subsubsection \subsubsubject
... ...


The unnumbered versions don’t appear in a table of contents! To change that, see the section on Unnumbered titles in the table of contents.

Also note that in the front matter of the document, all titles are unnumbered by default. The titles from the 'numbered' category still show up in the table of contents, though, and the titles from the 'unnumbered' category still don't.

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.

Note that reference labels are limited to ASCII characters in traditional TeX. This limitation has been removed in ConTeXt MkIV.

start/stop sectioning

Besides the traditional \chapter, \section etc. you can also use the more verbose, but also more flexible version of sectioning commands, these support tagged-PDF, XML and ePUB output better and also allow to insert code at the end of these sections.

Traditional Start/Stop
\part \startpart
\chapter \startchapter
\section \startsection
\subsection \startsubsection
\subsubsection \startsubsubsection
\title \starttitle
\subject \startsubject
... ...

Each of these start-commands has a corresponding stop-command to be used at the end of the section. For example:

\setuppapersize[A5]

\section[sec:old]{Old Section}

This is an old-style \ConTeXt\ section.

\startsection
   [
        title={New Section},
        reference=sec:new,
   ]

This is a new-style section.
\stopsection

produces

The following keys are available for the start-commands (see also \setupsection):

Key Description
reference= The reference key for cross-referencing (with \at, \in etc.)
title= The title of the section
list= The title to show in the table of contents, if different
bookmark= The title to show in the PDF bookmarks, if different
marking= The title to use in the section marking, if different
label= ?


Section Variable

You can set custom variables per section, and retrieve them with the \structureuservariable command:

\setuppapersize[A5]

\startchapter[title={Foo Bar}][foo=bar] % extra brackets!

foo is \quote{\structureuservariable{foo}}

\stopchapter

produces

Beware, if you want to reference these in your ToC setup, you need \structurelistuservariable.

Titling Style

see \setuphead and some enhanced samples below.

A FAQ is, how to get a line under the pageheader:

\setupheader[after=\hrule]

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

Using colors in chapters and sections

For colorful chapters or sections refer to \setuphead. Take a look at the following example:

\setupcolors[state=start]

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

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

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

\starttext

\completecontent

\chapter{Black chapter 1}

\section{Dark cyan section 1.1}

\page
Test.

\setuphead[chapter][color=darkgreen]
% This works, but avoid changing setups in the middle of your document,
% better define different versions of chapter, see next section

\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 want to 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 or setup several headers at once!

Formatting Titles with \setuphead

The following parameters to \setuphead change the style of the heading.

Text Number Both
textstyle numberstyle style
textcommand numbercommand command
deeptextcommand deepnumbercommand

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). textstyle 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é.)

Use \setuphead[part][conversion=Romannumerals], for example, to employ an alternative numbering scheme.

Truly empty pagebreak before chapters

Using \setuppagenumbering[alternative=doublesided] makes the chapters start on the right page. However, the blank page is not truly empty, it contains headers and footers. To get truly 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

Explanation

  • 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 afore 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.

When using front, body, back matters and appendices

When using *frontmatter, *bodymatter, *backmatter and *appendices, you need to remove the page they create to get the page before a new chapter or part with no headers and footers.

\setupsectionblock[frontpart][page=]
\setupsectionblock[bodypart] [page=]
\setupsectionblock[backpart] [page=]
\setupsectionblock[appendix] [page=]
\definepagebreak[firstpagebreak][yes,header,footer,right]
\setuphead[chapter][page=firstpagebreak]
\setuppagenumbering[alternative=doublesided]
\starttext
\startfrontmatter
\chapter{front}
\stopfrontmatter
\startbodymatter
\chapter{body}
\stopbodymatter
\startappendices
\chapter{appendix}
\stopappendices
\startbackmatter
\chapter{back}
\stopbackmatter
\stoptext

Independent Section Numbering

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

\defineresetset[default][1,1,0][1] % reset part, chapter, but not section
\setuphead[sectionresetset=default]

The MkII way was:

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


Unnumbered titles in table of contents

Sometimes one wants an unnumbered chapter, say introduction, the following might do that trick:

\setuppapersize[A5]

\definehead     [intro] [chapter]
\setuphead      [intro] [number=no]

\definecombinedlist     [content][list={intro,chapter,section}]
\setuplist      [intro] [headnumber=no]

\starttext
\completecontent
\startfrontmatter
\intro{No number}
\stopfrontmatter

\startbodymatter
\chapter{Has number}
\stopbodymatter
\stoptext

Note that just putting the introduction in the frontmatter already is enough, since frontmatter switches off chapter numbering.

An alternative approach:

\setuphead[title][
    incrementnumber=yes, % keep an internal title counter+list
    number=no]           % don't display the counter

% Manually specify the composition of the section and subsection
% numbers, so subsection 1.1 in the second title won't appear as
% Subsection 2.1.1
\setuphead[section][sectionsegments=section]
\setuphead[subsection][sectionsegments=section:subsection]   

% Redefine the toc list to include titles
\definecombinedlist[content][list={title,section}]

Usually you don’t want this. It also includes implicit titles (from ToC or Index) in the ToC.

Complete Section Numbering

You might have documents in which your sections don’t follow the structure, such as in:

\section{A}
%\subsection{B}
\subsubsection{C}

In that case, you would get 1 A 1 C. To enable complete section numbering, you would need in this case:

\setuphead[subsection][criterium=all]

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.

Start the title in the margin

This very simple example shows how to start a title within the margin, rather than at the text’s edge

\setuppapersize[A5]
\setuphead[chapter][alternative=margin]

\chapter{Sample chapter}

Number is in margin, not at text edge.

Chapter number and titles in separate lines

To have the chapter title simply in a new line, a new command needs to be defined that takes care of this. It needs to be wrapped in \framed.

\setuppapersize[A5]
\setuplabeltext [en] [chapter=Chapter~]
\define[2]\MyChapter
  {\framed[frame=off,width=broad,align=flushleft]{#1\\#2}}
\setuphead [chapter] [command=\MyChapter]
\chapter{Foo Bar}

Expanded chapter titles

This example illustrates expanded chapter titles.

\setuppapersize[A5]
\define[2]\MyChapterCommand%
  {\framed[frame=off,bottomframe=on,topframe=on]
     {\vbox{\headtext{chapter} #1\blank#2}}}

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

\chapter{My First Chapter}

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

   \setuppapersize[A6]
   \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}]

   \define[2]\Myheader{%
       \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

Title page with special background

If you want the title page of every chapter to have a special background (different from the background of normal pages), here’s a very effective trick that Hans has provided on the mailing list. In addition, this will give every chapter its own color scheme:

\startMPinclusions
    numeric MyTitlePageDone[] ;
\stopMPinclusions

\startuseMPgraphic{background:normal}
  StartPage ;
    fill (topboundary Page -- cycle) enlarged(0,5mm) 
    shifted (0,-5mm) withcolor blue ;
  StopPage ;
\stopuseMPgraphic

\defineoverlay
  [background:normal]
  [\useMPgraphic{background:normal}]

\startuseMPgraphic{background:title}
  if unknown MyTitlePageDone.\namedstructureuservariable{chapter}{mycolor} :
    StartPage ;
       fill ((topboundary Page --cycle) enlarged(0,5mm)) shifted (0,-20mm)
            withcolor \MPcolor{color:title:\namedstructureuservariable{chapter}{mycolor}} ;
    StopPage ;
    MyTitlePageDone.\namedstructureuservariable{chapter}{mycolor} := 1 ;
  fi ;
\stopuseMPgraphic

\defineoverlay
  [background:title]
  [\useMPgraphic{background:title}]

\setupbackgrounds
  [page]
  [background={background:normal,background:title}]

\definecolor [color:title:one]      [red]
\definecolor [color:title:two]      [blue]

\starttext

\startchapter[title=One][mycolor=one] \input tufte \page \input knuth \stopchapter
\startchapter[title=Two][mycolor=two] \input tufte \page \input knuth \stopchapter

\stoptext

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:

\setuppapersize[A5]
\setuphead
   [subject]
   [alternative=text,distance=0pt,command=\MyHead,style=\sc]

\define[2]\MyHead{\inmargin{#1 #2}}

\starttext

\subject{First Subject}
\input weisman

\subject{Second Subject}
\input ward

\stoptext

See the mailing list thread (2003) 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 (2003):

% 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

Chapter Headings

This is an example of chapter headings with an enlarged shadow behind them, based on a solution by Hans (2004).

\setuppapersize[A5]

\useencoding[ffr]
\mainlanguage[fr]

\setupcolors[state=start]

\definefont[BigFontOne][Regular sa 3] %RegularSlanted sa 3(ori)%Sans sa 3:don't work (pb accents)
\definefont[BigFontTwo][RegularBold sa 2]   %%RegularSlanted

\definecolor[BigColorOne][r=.86,g=.850,b=.54]
\definecolor[BigColorTwo][r=.43,g=.425,b=.27]

\setuphead
   [chapter]
   [textcommand=\MyChapterText,
    color=BigColorTwo,
    style=\BigFontTwo]

\def\MyChapterText#1{\rlap{\BigFontOne\BigColorOne#1}\kern15pt#1}

\starttext

\chapter[chap:ques]{Question}

\stoptext

Figures in Headings

This example shows how to completely replace the chapter text and number by an image:

\setuppapersize[A5]
\setupexternalfigures[location=default] % needed only for the wiki to the find the figure

\useexternalfigure[section-1][cow][width=5cm,height=1cm]
\useexternalfigure[section-2][mill.png][width=5cm,height=1cm]

\setuphead
   [section]
   [command=\MySectionCommand]
\setuphead
   [subsection]
   [command=]

\define[2]\MySectionCommand%
   {\externalfigure[section-\currentheadnumber]}

\starttext

\section{One}
\subsection{First subsection of Section One}
\section{Two}

\stoptext

There are a few snags though. MkII "forgets" to put the chapter in the TOC. So you have to set the deeptextcommand and deepnumbercommand separately instead (thus retaining the command that puts the chapter in the TOC), rather than setting the overall command, if you need a complete TOC.

\setuphead
    [chapter]
    [deeptextcommand=\MyChapterTextCommand,
     deepnumbercommand=\gobbleoneargument]      % hide chapter number

\def\MyChapterTextCommand#1%
    {\externalfigure[chapter-\currentheadnumber][height=5cm]}

[1] (2005)

Users of MkIV can safely rely on command and don't have to bother with the deep...command variants. However, \currentheadnumber does not seem to return anything but 0. Thus, the following code is required to define \MyChapterCommand when using MkIV:

\def\MyChapterCommand#1#2%
   {\externalfigure[chapter-\namedheadnumber{chapter}][height=5cm]}

[2] (2010)

Chapter head with absolute positioning of following text

No matter how many lines the chapter head runs to, the text will always start, e.g. 4cm below the top of the heading:

\setuppapersize[A6][A6]
\setupbodyfont[8pt]
\def\MyChapterCommand#1#2%
  {\vbox to 4cm\bgroup
     {#1\hskip.75em #2}
     \vss
     \egroup}
\setuphead[chapter][header=nomarking, command=\MyChapterCommand]
\starttext
\chapter{test} \input tufte
\stoptext

Section head with underlining to width of last line

A style called for section headings to be followed by a rule which extends only the width of the text. More tricky was the need to make sure that if the text ran onto another line, it was the length of the last line that was to be followed.

Hans came up with this cleverness, showing two alternate ways of doing it:

\setuppapersize[A5]

% ---- First solution ----

\def\FirstSolution#1#2%
  {\vbox\bgroup
   \setbox0\hbox{#1}
   \hsize.4\textwidth
   \beginshapebox#2\endshapebox
   \gdef\SetLastLineWidth{\xdef\LastLineWidth{\the\wd\scratchbox}}%
   \reshapebox
     {\setbox\scratchbox\hbox{\unhbox\shapebox}%
      \SetLastLineWidth
      \globallet\SetLastLineWidth\relax
      \box\scratchbox}%
   \scratchdimen\dimexpr\wd0+.25em+\LastLineWidth\relax
% \setbox2\ruledvbox   {\innerflushshapebox} 
% why is correction needed
   \setbox2\vbox        {\vskip-\lineskip\innerflushshapebox}
   \setbox0\vbox to \ht2{\box0}
   \hbox{\box0\hskip.25em\box2}
   \offinterlineskip
   \vskip.25ex
   \blackrule[width=\scratchdimen,height=1pt]
   \egroup}

% ---- Second solution ----

\doglobal\newcounter\NextLinePos

\def\SecondSolution#1#2%
  {\vbox\bgroup
   \hsize.4\textwidth
   \doglobal\increment\NextLinePos
   \setbox\scratchbox\hbox{#1}
   \hbox{\vtop{\copy\scratchbox}\hskip.25em\vtop{#2}}%
   \offinterlineskip
   \scratchdimen\dimexpr\wd\scratchbox+.25em+
     \MPx{e:npl:\NextLinePos}-\MPx{b:npl:\NextLinePos}\relax
   \vskip.25ex
   \blackrule[width=\scratchdimen,height=1pt]
   \egroup}

\def\DoSecondSolution#1%
  {\bpos{npl:\NextLinePos}#1\epos{npl:\NextLinePos}}

% ---- Trying it out solution ----

% \showstruts

\starttext
\setuphead[section][command=\FirstSolution]
    \section{is this nice or not}
    \section{is this nice or not nice, that's the question}

\setuphead[section][command=\SecondSolution,deeptextcommand=\DoSecondSolution]
    \section{is this nice or not}
    \section{is this nice or not nice, that's the question}
\stoptext

As if that wasn't enough, Taco pointed out that \lastlinewidth is the easy route to finding the width of the last line. Use something like:

\optimizedisplayspacingtrue\setlastlinewidth % core-mat macro
\global\advance\lastlinewidth-\hangindent\par % adjust
\blackrule[width=\lastlinewidth,height=1pt]

Flushing section head data without typesetting them in the text

The following is probably a very special case. Hoever suppose you need to place section information like the title e.g. in the topspace area. You want that the section is still displayed in the TOC, but you do not want that at the spot where \startsection[title=...,list=...] resides there is any trace of it. The command to be used to hide the section title is:

\setuphead
   [section]
   [placehead=hidden] 

Be aware that with this setting indeed the section title is not shown and no white space is inserted. However in this way also the list entry for the TOC is gone. What you can do in such cases is to use the following command in connection with the above mentioned setting:

\setuptexttexts[{\placerawheaddata[section]} 

What happens is that the information in the \startsection[title=...,list=...] command is passed to the texttexts, which are flushed at every page. In order to see when it is flushed you might place e.g. !!! in front of the \placerawheaddata[section].

The result is a complete TOC without any trace of the section heading in the text.

Test:

\setuphead
   [section]
   [placehead=hidden,
    page=yes]

\setuphead
   [subsection]
   [placehead=yes,
    page=yes,
    continue=yes,
    style=\it]

\setuptexttexts[{\placerawheaddata[section]}]

\starttext

\placelist[section,subsection] \page

\startsection[title=First section]

   \startsubsection[title=First subsection]
      Lorem ipsum \dots
   \stopsubsection

   \startsubsection[title=second subsection]
      Lorem ipsum \dots
   \stopsubsection

\stopsection

\startsection[title=Second section]

   \startsubsection[title=First subsection]
      Lorem ipsum \dots
   \stopsubsection

\stopsection

\stoptext

Section head without chapter number

If you only want the section number in the section title you add

\setuphead[section][sectionsegments=section]
\starttext
\chapter{Eric}
\section{Foo 1}
\subsection{Bar one}
\section{Foo 2}
\stoptext

But as once you changed one you have to change the lower ones. So you have to add the definition of sectionsegments in subsection, subsubsection, ...

\setuphead[section][sectionsegments=section]
\setuphead[subsection][sectionsegments=section:subsection]
\setuphead[subsubsection][sectionsegments=section:subsubsection]

\starttext
\chapter{Eric}
\section{Foo 1}
\subsection{Bar one}
\subsubsection{Barbar one }
\subsubsection{Barbar two}
\subsection{Bar two}
\subsubsection{Barbar one}
\subsubsection{Barbar two}
\section{Foo 2}
\subsection{Bar one}
\subsubsection{Barbar one}
\subsubsection{Barbar two}
\stoptext

It's not written here but you can also change the conversion of the numbering with

\definestructureconversionset[mySet][A,R,n,n,a,r][n] 
\setuphead[chapter,section,subsection,subsubsection,subsubsubsection][sectionconversionset=mySet]

The parameters in definestructureconversionset are

the set name

the codes for conversion (one for each level)

the default conversion for the missing level

You can find the conversions codes at [3]

Editing Bold Math in Section Titles

When a section title is formatted in boldface text, this doesn't automatically typeset embedded math in bold. This is probably a good thing, as many math fonts do not include all characters in boldface, and often authors use boldface to distinguish between different meanings (for instance, scalar and vector variables). However, in some cases, it is desirable to format the math in the section titles in boldface.

Here a very simple example:

\definetypeface[boldmath][mm][boldmath][latin-modern][default]
\setuphead[section][style=\boldmath\rm\bfa]
\starttext
\section{Bold $a^2+b^2=c^2$ {\em bla}}
text: not bold $a^2+b^2=c^2$ {\em bla}
\stoptext