Document structure and headlines/Headlines formatting

Unofficial ConTeXt Wiki mirror

Last modified: 2025-10-07

Formatting Titles with \setuphead

For numbering see Section numbering.

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

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.

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

Headline text alignment and hyphenation

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

Empty the headers of pages preceding a new chapter

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 \definepagebreak and \setuphead with the option page=. See the following:

Explanation

When using macro divisions (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]  % HERE
\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

Flushing headline 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.

Demo:

Use the margin

Number in the margin

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

Headline in the margin

Source on tex.stackexchange

The margintext alternative is mentioned in the command reference [1]).

To raise the position of the first line of the margin text you can use the dy key for \setupmargindata (\setupmargindata[margintext:section][dy=-2pt]). – Wolfgang Schuster - Commented Feb 14, 2018

Headline in the margin (2003)

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:

See the mailing list thread (2003) for more on this.

Numbering and text on 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.

Section label in the headline

This example illustrates expanded chapter titles with \headtext. You need to define the text with \setupheadtext or \setuplabeltext as above.

Headline with an enlarged shadow

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

Completely replace the text and chapter number with an image

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

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

archive.contextgarden.net (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]}

archive.contextgarden.net (2010)

More elaborate headline composition


TODO: a more attractive example using similar features (See: To-Do List)

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:

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:

Underlining to the width of last line of the headline

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:

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]

Headline including bold math

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: