忽略文档层次结构命令后的换行符

忽略文档层次结构命令后的换行符

我正在为某人准备一份非常简单但也非常长的文档,他为我提供了数十个使用 包含的 tex 文件\input{}。部分地,为了让原作者仍然有干净的 tex 文件,我试图利用一些以前发布的代码来自动化包的操作lettrine

自动将章节首字母首字下沉

它们的内容非常简单,所以我并不太担心异常。

\chapter{}不幸的是,我无法使用我在以前的问题中找到的自动化代码,因为我有太多的文件在 tex 文件中和文本段落之间有换行符。

我尝试了几种方法(在下面的 MWE 中注释掉),但都无济于事。要使此文档正确编译,只需删除章节后的行,或插入 %。

有没有办法重置下面的代码\chapter{}来做到这一点?

MWE 带有尚未起作用的 gobble 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}%

%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}

答案1

最好真的调用一个修改过的\@afterheading,然后将 lettrine 处理挂接到现有的\everypar处理程序中,以抑制标题后的段落缩进,但从你所在的位置开始,我认为最简单的方法是暂时将行尾设为正常空格,从而禁用两个行尾的转换为\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}

在此处输入图片描述

相关内容