1

Background

Looking to create a stylized chapter heading using the first character of the heading title.

Problem

The stem (descender) distance of text causes the containing frame's offset to alter the location of the emphasized first letter, which is obvious from the following picture:

Influential Stem

The problematic line in the code below is this line, which was added to ensure the title text does not incur a line-break, as suggested from an answer to a previous question:

\scale[maxwidth=\textwidth-(\marginwidth)]{#1}

When the line above is simply:

#1

Then the alignment is perfect, but when the text is too long an undesired line-break ensues.

Code

A minimal working example:

\definecolor[BookColourPrimary][h=A45F62]
\definecolor[BookColourSecondary][h=9A957A]
\definespotcolor[BookColourSecondaryLightest]
  [BookColourSecondary][a=1,t=.2]
\definefont[BookFontChapterHeader][SansBoldItalic scaled 5000]
\definefont[BookStyleFontChapterCharacter][SerifItalic scaled 20000]

\defineframed[BookStyleChapterFramed][
  frame=on,
  align={left,low,nothyphenated,verytolerant},
  width=\dimexpr\textwidth-(.2\marginwidth),
  height=\dimexpr\textheight+0.25\footerheight,
]

% Extract first character
\define[1]\BookStyleChapterChar{\cldcontext{string.sub("#1",1,1)}}

% Use a nested frame to keep the overlain large letter together with
% the chapter title.
\define[1]\BookStyleChapter{%
  \BookStyleChapterFramed{%
    \inframed[align=right, frame=on]{%
      \startoverlay
        % Calculate the large letter's offset using the first character's
        % width. This allows font changes without significantly affecting
        % the distance between the emphasized first character of the
        % chapter title and the complete chapter title.
        \setbox\scratchbox\hbox{%
          \BookStyleFontChapterCharacter\BookStyleChapterChar{#1}}%
        % Shift the first character back relative to the chapter title.
        \hskip-.55\wd\scratchbox%
        \color[BookColourSecondaryLightest]{%
          \BookStyleFontChapterCharacter%
          \BookStyleChapterChar{#1}%
        }
        \vskip-1.9em
        % Ensure the text fits on the page.
        \scale[maxwidth=\textwidth-(\marginwidth)]{#1}
      \stopoverlay
    }
  }
}

% Avoid duplication...
\define[1]\BookChapterText{
  \BookColourPrimary{\BookFontChapterHeader{\BookStyleChapter{#1}}}
}

\starttext
  \BookChapterText{Test-a}
  \page
  \BookChapterText{Test-g}
\stoptext

Question

How do you make ConTeXt ignore the stem height while still scaling the text to fit within the \textwidth margins?

  • 1
    Try \inframed[align=right, frame=on, location=height]{%, the frame will be positioned relative to the baseline (not the depth) of the text. – Philipp Gesang Jan 6 '14 at 21:58
0

The BookStyleChapterFramed needs its height adjusted:

height=\dimexpr\textheight-\footerheight,

The frame needs to be adjusted as per phg's comment:

\inframed[align=right, frame=off, location=height]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.