[Tex/LaTex] Automatic dropcaps for the first letter of a chapter

drop-capformattingmacrosparsingsectioning

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

Best Answer

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]{\@tempa}{\@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]{\@tempa}{\@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]{\@tempa}{\@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}
Related Question