我正在为毕业论文编写 LaTeX 样式,并希望自动将章节的第一个单词转换为首字下沉。使用包lettrine
和相应的命令手动执行此操作很简单(请参阅下面的代码示例和示例输出)。但是,我想要的是,
- 自动解析出章节的第一个单词,
- 将第一个字母与第一个单词分开,
- 将结果令牌传递给
\lettrine{...}{...}
- 将结果插回到 token 流中
具体来说,我想跳过任何环境(例如前导引号),以便只删除章节主体的首字母。
我不熟悉 LaTeX 中的标记解析,因此如果能得到一些指导我将非常感谢。
样品输入(带手册\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}
答案1
我认为您问题中“特别是...”后面的部分实际上表示一个单独问题的开始(值得在 TeX.SX 上提出)。但是,关于问题的主要部分,您可以\@chapter
按如下方式修补宏。
\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}
您可能还想首先检查第一个字母是否是实际字母,并且\lettrine
仅在这种情况下:
\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
}
解决在章节开始后立即处理替代输入的问题的一个可能方法是定义一个chapterpreamble
环境(如@Werner 在 OP 的评论中提出的那样)。在 Difference 中,我会将环境放在相应章节之前——然后它的使用将是“强制”可选的。然后,环境会将其内容收集到令牌寄存器中,重新定义的宏\@chapter
将负责输出其内容:
\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}