如何让每章第一个文本图表的第一个单词自动大写?

如何让每章第一个文本图表的第一个单词自动大写?

梅威瑟:

% !tex=pdflatex
\documentclass[oneside,12pt]{book}
\usepackage{lipsum,lettrine}

\begin{document}
% NOTE1: There have text between Chapter and Section, I just want to the first word be dropcaped like below.
\chapter{Week1}
\lettrine{W}{ith} a drop cap, the initial sits
within the margins and runs several lines deep into the paragraph, pushing some
normal-sized text off these lines.
\section{Day1 of Week1}
\lipsum[1]  
Chapter one end. 

% NOTE2: There have NOT any text between Chapter and Section, but I just want to the first word be drop-caped like below.
\chapter{Week2}
\section{Day1 of Week2}
\lettrine{L}{orem} \lipsum[1]  

% NOTE3: How to do this work like above(just let the first word be drop-caped in every chapter) 
% through the whole book automatically?
\chapter{Week3}
\lipsum[1]  

\end{document}

我能做的如注释1和注释2所示。我只想找到一个最佳解决方案来自动完成这项工作(注释3)。

答案1

下面的解决方案实际上是一个半解决方案,甚至可能是一个蛮力解决方案,它受到这个答案的启发:https://tex.stackexchange.com/a/290567/231952。它需要三个条件。1)后面不能有空行\chapter;2)后面的文本\chapter不能以 开头;3)使用\标准类:book

\documentclass[oneside]{book}
\usepackage{lettrine}
\usepackage{etoolbox}

\def\dolettrine #1#2 { \lettrine{#1}{#2} }    
\makeatletter
  \apptocmd{\@chapter}{\dolettrine}{}{}
\makeatother

\begin{document}
\chapter{Lorem ipsum} 
Lorem ipsum dolor sit amet

\chapter{Dolor sit}
%
Dolor sit amet lorem ipsum

\chapter{Dolor magnum}
% In this case it does not work correctly
\textit{Lorem} ipsum dolor sit amet.

\end{document}

编辑。

第一个限制可以通过以下方式消除:

\apptocmd\@makechapterhead{\endlinechar=32}{}{}
\apptocmd{\@afterheading}{\endlinechar=13}{}{}

该解决方案的灵感来自于https://tex.stackexchange.com/a/16462/231952。第一个命令将 附加\endlinechar=32\@makechapterhead,以便行尾被空格替换。第二个命令恢复默认值13

对于带星号的版本\chapter我们也需要这个:

\apptocmd{\@schapter}{\dolettrine}{}{}
\apptocmd\@makeschapterhead{\endlinechar=32}{}{}

相关内容