0

I'm preparing a very simple, but also very long document for someone who is providing me with many dozens of tex files being included using \input{}. In part, so the original author still has clean tex files, I am trying to make use of some previously posted code to automate the action of the lettrine package.

Automatic dropcaps for the first letter of a chapter

Their content is very simple so I'm not too worried about exceptions.

Unfortunately I'm unable to use the automation code I've found in previous questions because I have so many files with line breaks in the tex files in between the \chapter{} and the paragraph of text.

I've tried a few approaches (commented out in the MWE below) to no avail. To get this document to compile properly, simply remove the line after the chapter, or insert a %.

Is there anyway to reset the code following \chapter{} to do this?

MWE with gobble par code that isn't yet working:

\documentclass{report}

%https://tex.stackexchange.com/questions/205364/automatic-dropcaps-for-the-first-letter-of-a-chapter
%https://tex.stackexchange.com/questions/769/how-can-i-create-documents-in-latex-using-a-calligraphic-first-letter-for-chapte

\usepackage[x11names]{xcolor} 
\usepackage{lipsum}
\usepackage{environ}
\usepackage{xstring}
\usepackage{lettrine}
\usepackage{GoudyIn}

%\usepackage[x11names]{xcolor} 
\renewcommand{\LettrineFontHook}{\color{VioletRed4}\GoudyInfamily{}}
\LettrineTextFont{\itshape}
\setcounter{DefaultLines}{3}%

%http://phaseportrait.blogspot.ca/2011/08/using-gobblepars-to-prevent-latex-from.html
\makeatletter
\newcommand\gobblepars{%
    \@ifnextchar\par%
    {\expandafter\gobblepars\@gobble}%
    {}}
\makeatother


%https://tex.stackexchange.com/questions/179016/ignore-spaces-and-pars-after-an-environment
\def\useignorespacesandallpars#1\ignorespaces\fi{%
    #1\fi\ignorespacesandallpars}

\makeatletter
\def\ignorespacesandallpars{%
    \@ifnextchar\par
    {\expandafter\ignorespacesandallpars\@gobble}%
    {}%
}
\makeatother

\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{\StrLeft{#3}{1}}{\@gobble#3}
    \else
        #3
    \fi
}
\newtoks\ch@pterpreamble
\NewEnviron{chapterpreamble}{\global\ch@pterpreamble=\expandafter{\BODY}}
\makeatother

%\let\unskippedchapter\chapter
%\renewcommand{\chapter}[1]{\unskippedchapter{#1}\gobblepars}

\begin{document}

\chapter{Demo}
%
The quick brown dog jumped over the lazy brown fox.
The quick brown dog jumped over the lazy brown fox.
The quick brown dog jumped over the lazy brown fox.
The quick brown dog jumped over the lazy brown fox.
The quick brown dog jumped over the lazy brown fox.
The quick brown dog jumped over the lazy brown fox.

\end{document}
  • @cfr all the link URLs are in the code as comments, I will add each as a hyperlink with more complete descriptions next time I'm at a computer. – EngBIRD May 5 '17 at 2:08
  • Sorry. Missed that somehow. – cfr May 5 '17 at 2:18
1

It would be better really to call a modified \@afterheading and so hook the lettrine processing into the existing \everypar handler that suppresses paragarph indentation after the heading, but starting from where you are, I think the easiest thing is to temporarily make end of line a normal space so disabling the conversion of two end of lines to \par

\documentclass{report}

%https://tex.stackexchange.com/questions/205364/automatic-dropcaps-for-the-first-letter-of-a-chapter
%https://tex.stackexchange.com/questions/769/how-can-i-create-documents-in-latex-using-a-calligraphic-first-letter-for-chapte

\usepackage[x11names]{xcolor} 
\usepackage{lipsum}
\usepackage{environ}
\usepackage{xstring}
\usepackage{lettrine}
\usepackage{GoudyIn}

%\usepackage[x11names]{xcolor} 
\renewcommand{\LettrineFontHook}{\color{VioletRed4}\GoudyInfamily{}}
\LettrineTextFont{\itshape}
\setcounter{DefaultLines}{3}%



\makeatletter
\let\ltx@@chapter\@chapter
\def\@chapter{\endlinechar=32 \@lettrine@chapter}
\def\@lettrine@chapter[#1]#2 #3 {%
\endlinechar=13
    \ltx@@chapter[#1]{#2}%
    \the\ch@pterpreamble
    \ch@pterpreamble{}
    \ifcat a\expandafter\noexpand\@car#3\relax\@nil
        \lettrine{\@car#3\@nil}{\@cdr#3\@nil}%
    \else
        #3%
    \fi
}
\newtoks\ch@pterpreamble
\NewEnviron{chapterpreamble}{\global\ch@pterpreamble=\expandafter{\BODY}}
\makeatother

%\let\unskippedchapter\chapter
%\renewcommand{\chapter}[1]{\unskippedchapter{#1}\gobblepars}

\begin{document}

\chapter{Demo}

The quick brown dog jumped over the lazy brown fox.
The quick brown dog jumped over the lazy brown fox.
The quick brown dog jumped over the lazy brown fox.
The quick brown dog jumped over the lazy brown fox.
The quick brown dog jumped over the lazy brown fox.
The quick brown dog jumped over the lazy brown fox.

\chapter{zzzz}

\section{bbb}
sssss aaaa

\end{document}

enter image description here

  • Thanks, one small add on. Reducing to a MWE that throws errors make it hard to fully represent the scope of the intended problem. If the chapter is immediately followed by a \section and \subsection instead of text this doesn't work. In general where would I put conditionals to address my exceptions when they occur? If I'm understanding this block correctly, it would be after the \ifcat\@tempa a line but before the \lettrine{\StrLeft{#3}{1}}{\@gobble#3~}%. Specifically for the section subsection logic, I'm thinking of nesting similar blocks to this whole thing. – EngBIRD May 5 '17 at 14:28
  • @EngBIRD yes that's why I said at the top that it's the wrong approach really, I assumed that you were only using it in a context where the following text is simple text. the standard chapter code already correctly handles blank lines etc, which is broken by this, so adding it into \@afterheading would be better. But if you want to support the chapter text starting with \section what do you want to do, just do nothing (about lettrine?) – David Carlisle May 5 '17 at 14:32
  • @EngBIRD but anyway I just updated removing the xstring code that was tripping over \section so now it accepts that just taking the existing "non letter2 branch in that case. – David Carlisle May 5 '17 at 14:42

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.