11

I'm in the process of writing a LaTeX style for thesis dissertations, and would like to automatically transform the first word of a chapter to dropcaps. This is trivial to do manually using the lettrine package and corresponding command (see the code sample and example output following). What I would like however, is to,

  1. automatically parse out the first word of a chapter,
  2. separate the first letter from the first word,
  3. pass the resulting tokens to \lettrine{...}{...}
  4. insert the result back into the token stream

In particular, I would like to skip any environments (such as the leading quote) so that only the first letter of the chapter body is dropped.

I'm unfamiliar with token parsing in LaTeX, so some guidance would be really appreciated.

Sample Input (with manual \lettrine)

\begin{document}

\chapter{Introduction}
\begin{quote}[Katie Makkai][flushright]
``The word pretty is unworthy of everything you will be. And no child of mine
will be contained in five letters. You will be pretty intelligent, pretty
creative, pretty amazing. But you will never be merely pretty.''
\end{quote}

\lettrine[nindent=0em]{W}{hen} I was just a little girl, I asked my mother,
``What will I be? Will I be pretty, will I be pretty, will I be pretty?!''
What comes next? Oh right, will I be rich -- which is almost pretty depending
on where you shop. And the pretty question infects from conception passing
blood and breathe into cells. The word hangs from our mothers hearts in a
shrill fluorescent floodlight of worry. Will I be wanted? Worthy? Pretty? 

\end{document}

Manual lettrine example

  • Will all your chapters always follow the same pattern? That is, each chapter will have a quote? – Werner Oct 9 '14 at 20:48
  • @Werner, not necessarily. Each chapter may begin with an environment of some sort. Potentially multiple quotes. See the current full source here: https://github.com/hbristow/qutthesis – hbristow Oct 9 '14 at 20:51
  • @hbristow: It may be difficult to automate things if the structure keeps changing. If you conform to some fixed standard, then automation becomes easier, or at least more probable. – Werner Oct 9 '14 at 20:56
  • @Werner: I'm happy to abandon this idea if it's too hard. After all, a seven chapter thesis will only require seven dropcaps, which really isn't a whole lot of work for the user. I just thought it would be an interesting exercise in getting to understand LaTeX a little more. Is there a way to recognize an environment and defer the current macro expansion? – hbristow Oct 9 '14 at 21:03
  • 1
    @hbristow: Yes, if you're consistent. For example, one could wrap the entire quote (whether it contains one or more, or perhaps none) in a different environment called chapterpreamble. Then you consistently use chapterpreamble after \chapter with the requirement that the drop-cap always follows the chapterpreamble environment. This consistency will allow you to capture the contents and do some magic. – Werner Oct 9 '14 at 21:05
5

I think the part after "In particular..." in your question really denotes the begin of a separate question (that is worth to be asked here on TeX.SX). However, regarding the main part of your question you could patch the \@chapter macro as follows.

\documentclass{report}
\usepackage{ebgaramond}
\usepackage{lettrine}
\usepackage{xstring}

\makeatletter
\let\ltx@@chapter\@chapter
\def\@chapter[#1]#2 #3 {\ltx@@chapter[#1]{#2}\lettrine[nindent=0em]{\StrLeft{#3}{1}}{\@gobble#3}\ }
\makeatother

\begin{document}
\chapter{Introduction}
When I was just a little girl, I asked my mother,
``What will I be? Will I be pretty, will I be pretty, will I be pretty?!''
What comes next? Oh right, will I be rich -- which is almost pretty depending
on where you shop. And the pretty question infects from conception passing
blood and breathe into cells. The word hangs from our mothers hearts in a
shrill fluorescent floodlight of worry. Will I be wanted? Worthy? Pretty?
\end{document}

output

You also might want to check first if the first letter is an actual letter and \lettrine only if this is the case:

\def\@chapter[#1]#2 #3 {%
  \ltx@@chapter[#1]{#2}
  \StrLeft{#3}{1}[\@tempa]
  \ifcat\@tempa a
    \lettrine[nindent=0em]{\StrLeft{#3}{1}}{\@gobble#3}
  \else
    #3
  \fi
}

A possible workaround for the problem of dealing with alternative input right after the start of the chapter could be to define a chapterpreamble environment (like @Werner proposed in the comments of the OP). In Difference, I would put the environment right before the corresponding chapter -- its use would be then optional "by force". The environment would then collect its content into a token register and the redefined \@chapter macro would take care of outputing its content:

\documentclass{report}
\usepackage{ebgaramond}
\usepackage{lettrine}
\usepackage{xstring}
\usepackage{environ}

\makeatletter
\let\ltx@@chapter\@chapter
\def\@chapter[#1]#2 #3 {%
  \ltx@@chapter[#1]{#2}
  \the\ch@pterpreamble
  \ch@pterpreamble{}
  \StrLeft{#3}{1}[\@tempa]
  \ifcat\@tempa a
    \lettrine[nindent=0em]{\StrLeft{#3}{1}}{\@gobble#3}
  \else
    #3
  \fi
}
\newtoks\ch@pterpreamble
\NewEnviron{chapterpreamble}{\global\ch@pterpreamble=\expandafter{\BODY}}
\makeatother

\begin{document}
\begin{chapterpreamble}
  \begin{quote}
    ``The word pretty is unworthy of everything you will be. And no child of mine
    will be contained in five letters. You will be pretty intelligent, pretty
    creative, pretty amazing. But you will never be merely pretty.''
  \end{quote}
\end{chapterpreamble}

\chapter{Introduction}
%{``T}o be or not to be.''
When I was just a little girl, I asked my mother,
``What will I be? Will I be pretty, will I be pretty, will I be pretty?!''
What comes next? Oh right, will I be rich -- which is almost pretty depending
on where you shop. And the pretty question infects from conception passing
blood and breathe into cells. The word hangs from our mothers hearts in a
shrill fluorescent floodlight of worry. Will I be wanted? Worthy? Pretty?
\end{document}
  • Of course, the problem with automation are the exceptions to the rule. What if the chapter starts with a double-backtick quote: ``To be or not to be'' ? – Steven B. Segletes Jan 11 '17 at 13:23
  • @StevenB.Segletes {``T}o be or not to be'' might work – yo' Jan 11 '17 at 13:38
  • @StevenB.Segletes -- totally right! This is of course an issue. Thanks for pointing that out. I added a remark on this how I think that could be met. – Ruben Jan 11 '17 at 13:48
  • @Ruben I wouldn't risk automating this, honestly. – yo' Jan 11 '17 at 14:25
  • @yo' -- I could add a disclaimer: "automation at own risk!" :) – Ruben Jan 11 '17 at 14:32

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.