0

I'm working on my thesis dissertations, and I'd like to convert the first word of each section to dropcaps automatically.

I know how to do this manually using the lettrine package. But I would like to automate this. Every section starts with a normal word no quotes or fancy stuff before the text.

\section{What is Lorem Ipsum?}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam vehicula purus ac ligula consectetur porta. Mauris eget condimentum magna. Vivamus tempus lacus dolor, nec pharetra massa porttitor in. Nunc fermentum tellus at orci lobortis gravida. Sed eget quam vel ante scelerisque venenatis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vivamus a quam mauris. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut quam est, molestie eget leo eu, aliquam tincidunt urna. Nulla facilisi.

I've tried to apply this solution Automatic dropcaps for the first letter of a chapter With no success.

Note: I use pdflatex.

This is the macro I tried with no success:

\usepackage{lettrine}
\usepackage{xstring}

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

`

George G.
  • 101
  • Please post the MWE as in executable format, at least mention which class file you are using? – MadyYuvi Oct 06 '21 at 13:56
  • Welcome to [tex.se]! Unfortunately \section is defined in a rather a different way to \chapter, so it is rather hard to patch in a way like this. – Andrew Swann Oct 06 '21 at 14:16
  • I would not do this (given the work gone into a thesis, the work to add the lettrine markup explicitly is insignificant) but to answer your question of how to patch this to \section we would need to know how \section is defined. It is not defined by latex, but by the document class (and possibly redefined by packages) that you are using and have given no information. – David Carlisle Oct 06 '21 at 18:06
  • @MadyYuvi I'm working with the book document class. – George G. Oct 07 '21 at 13:35

1 Answers1

4

I'm not sure this is advisable but section headings are already running code at the start of the next paragraph to control indentation, so you could hook into that...

enter image description here

\documentclass{article}
\usepackage{lettrine}
\makeatletter
\def\@afterheading{%
  \@nobreaktrue
  \everypar{%
    \if@nobreak
      \@nobreakfalse
      \clubpenalty \@M
      \if@afterindent \else
        {\setbox\z@\lastbox}%
      \fi
 \ifdim\@tempskipa=\glueexpr 2.3ex \@plus.2ex\relax % article section spacing (test ignore plus component)
     \expandafter\expandafter\expandafter\zzz
\fi
    \else
      \clubpenalty \@clubpenalty
      \everypar{}%
    \fi}}
\makeatother

\def\zzz#1 {\zzzB#1{} } \def\zzzB#1#2 {\lettrine{#1}{#2} }

\begin{document}

\section{abc}

Once upon a time. Red yellow black blue white green. Red yellow black blue white green. Red yellow black blue white green. Red yellow black blue white green.

\subsection{No lettrine for subsec} Normal text

\section{xyz}

The second section. Red yellow black blue white green. Red yellow black blue white green. Red yellow black blue white green. Red yellow black blue white green.

\subsection{No lettrine for subsec} Normal text

\end{document}

David Carlisle
  • 757,742
  • That's always been my recommendation for doing drop caps at the beginning of a section. There's still the potential gotcha of a one-line paragraph at the beginning of a section, but that can always be dealt with as needed on a case-by-case basis. – Don Hosek Oct 06 '21 at 19:07
  • Thanks! But this do not work with the document class I'm working with, "Book". – George G. Oct 07 '21 at 13:34
  • @GeorgeG. 95% of the code of book and article classes are the same, derived from teh same source file, classes.dtx If you change article to book in the above the lettrine works the same way and you get this output – David Carlisle Oct 07 '21 at 13:48
  • Hi David. Thanks for your patience. I've just found out this package I'm using is interfering \usepackage{titlesec}. I just don't know how to solve it. But if I weren't using it. It would work for me – George G. Oct 08 '21 at 00:50