[NTG-context] Equivalent to \settowidth

Robin Kirkham robin.kirkham at csiro.au
Sat Mar 17 13:39:50 CET 2007

> From: Hans Hagen <pragma at wxs.nl>
> Wolfgang Schuster wrote:
>> 2007/3/14, Robin Kirkham <robin.kirkham at csiro.au>:
>>> Is there a ConTeXt equivalent to LaTeX \settowidth{}, or better  
>>> still
>>> some macro I can do things like
>>> \setupsomething[
>>>      ...
>>>      width={\widthof{\bf sometext\emspace}},
>>>      ...]

> \def\setwidthof#1\to#2%
> {\bgroup
>  \setbox\scratchbox\hbox{#1}%
>  \expanded{\egroup\def\noexpand#2{\the\wd\scratchbox}}}

Thanks Wolfgang, Chen and Hans for your responses. I used Hans'  
one ... it seems to work.

I'm actually trying to indent tables of contents by level. I want  
each indent to match the start of the heading text of the previous  
level, i.e.,

1  Chapter
    1.1  Section
         1.1.1  Subsection
         1.1.2  Subsection

in other words, the indent is not quite a fixed width per level.

The following complete example sort of does what I want (using Hans'  
\setwidthof) but it will fail when the section numbers have more  
digits than I have allowed for.
I have a feeling I should be using \setupcombinedlist[content] 
[distance=1em] or something, but that puts an em space in front of  
unnumbered chapters (titles) as well.

What I have done seems very ugly, surely there is a "proper" way?


% widthof macro from Hans


% calculate widths of numbers
\setwidthof{\bf 9\emspace}\to\WDA

% calculate margins
\setwidthof{{\bf 9\emspace}}\to\MAB
\setwidthof{{\bf 9\emspace}9.9\emspace}\to\MAC
\setwidthof{{\bf 9\emspace}9.9\emspace 9.9.9\emspace}\to\MAD

\setuplist[chapter]             [margin=0em,  width=\WDA]
\setuplist[section]             [margin=\MAB, width=\WDB]
\setuplist[subsection]          [margin=\MAC, width=\WDC]
\setuplist[subsubsection]       [margin=\MAD, width=\WDD]



\chapter{Unnumbered Chapter}



More information about the ntg-context mailing list