[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
...
Title

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?

Robin

% widthof macro from Hans
\def\setwidthof#1\to#2%
     {\bgroup
         \setbox\scratchbox\hbox{#1}%
         \expanded{\egroup\def\noexpand#2{\the\wd\scratchbox}}}

\setupcombinedlist[content][
     alternative=c,
     aligntitle=yes]
\setuplist[chapter][
     alternative=b,
     style=bold]

% calculate widths of numbers
\setwidthof{\bf 9\emspace}\to\WDA
\setwidthof{9.9\emspace}\to\WDB
\setwidthof{9.9.9\emspace}\to\WDC
\setwidthof{9.9.9.9\emspace}\to\WDD

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


\starttext

\completecontent[criterium=all]
\page

\startfrontmatter
\chapter{Unnumbered Chapter}
\stopfrontmatter

\startbodymatter
\chapter{Chapter}
\section{Section}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\chapter{Chapter}
\section{Section}
\subsection{Subsection}
\subsubsection{Subsubsection}
\chapter{Chapter}
\stopbodymatter

\stoptext



More information about the ntg-context mailing list