我一直在尝试使用 LaTeX 中的首字下沉功能(使用字体和莱特林软件包),并且在使用 dropcaps 之后您经常看到的一种印刷效果是将段落的整个第一行排版为小写字母。很明显,如何通过反复试验获得此效果 - 运行 LaTeX,查看第一行的内容,然后\textsc
重新运行 LaTeX,查看移动的内容,然后重新设置\textsc
相应的文本 - 但这非常麻烦,而且很容易忘记这样做。 有没有一个好的方法可以让 (La)TeX 检测其排版输出中的换行符的位置? 我认为我必须至少运行两次 LaTeX,因为在处理过程中情况可能会发生变化,但使用 LaTeX 就是这样。
编辑1:我也需要它适用于数学模式($...$
)代码——从迄今为止提出的解决方案来看,这不一定是非平凡的。
答案1
Donald Arseneau 的 magaz 包可以帮你完成这个任务。从记忆中看,它使用的想法与 Yiannis 的大致相同——也就是说,只需要一次传递——但我认为它可能更稳健一些。
此解决方案将与内联数学一起使用,前提是数学不会在行尾中断,如下所示:
\documentclass{article}
\usepackage{magaz}
\begin{document}
\FirstLine{There is no just \emph{ground}, therefore, $a+b=c$ for the charge $x\in\{0,1,2\}$ brought against me by certain ignoramuses.}
\end{document}
但将数学放入其中\mbox
似乎可以解决这个问题。
本答案的读者可能希望为第一行选择一种颜色。这需要一种略有不同的方法,David Carlisle 在主题中的回答中对此进行了讨论为什么仅用于第一行排版的颜色(使用 magaz 包)会影响整个文档?
答案2
这是一个 TeXie 解决方案!
\documentclass[11pt]{article}
\begin{document}
\def\firstline{\par\begingroup\Firstlinefont
\def\zzz{}\dimen0\parindent
\setbox0=\hbox{ }\advance\dimen0by-\wd0
\x}
\def\x{\afterassignment\y\z}
\def\z#1 {\xdef\zz{#1\ }}
\def\y{\setbox0=\hbox{\zz}
\advance\dimen0 by\wd0
\ifdim\dimen0<\hsize\let\next=\x
\edef\zzz{\zzz\zz}
\else\let\next=\endgroup
\leavevmode\zzz\unskip\break\aftergroup\zz
\fi
\next}
\let\Firstlinefont=\scshape
\firstline The first line of this paragraph is in small caps font series. You
will need to hack the rest of the code to get it working with the lettrine package!
\end{document}
答案3
(我是从重复的问题中得出这个结论的在 LaTeX 中强调段落的第一行)
Yiannis 的解决方案非常好。这是另一种方法,它使前两行在处理单词间空格的方式上的视觉差异更小。与 Yiannis 相比,我的代码可能总体上导致较差的结果(强调可能延伸到第二行的开头或在第一行结束得有点太短),但我确实建议使用它,因为它比 Yiannis 的复杂宏更简单!此外,它似乎适用于包括内联数学的示例,并允许在第一行末尾使用连字符(但强调适用于整个单词)。
内联数学公式应该以不带空格(例如 $a+b=c$)的形式书写(只要有可能,它就属于强调文本),或者用加倍括号(例如 {{$ a + b = c $}})。然后,在行尾换行就可以正常工作。
代码定义了两个用户命令\decorate
和\Decorate
(以及其他用于决定强调文本样式的命令)。\decorate
用作\decorate{TEXT}
(其中 TEXT 不能包含\par
标记),并且\Decorate
放在段落的开头(因此不能用于例如\parbox
,与小写形式相反)。
两者都允许一个可选参数(默认值为 0.95),该参数大致可以确定要强调的行数。
并且\Decorate
有一个带星号的版本\Decorate*
,它试图激发对其应用的段落的扩展(请参阅代码末尾的比较说明)。虽然无法将其用作\lipsum[1]
参数(即使有nopar
package选项lipsum
),但lipsum@i
没问题。
\documentclass[a4paper]{article}
\makeatletter
\def\addtobufferwithaspace#1#2{\expandafter\def\expandafter#1%
\expandafter{#1 #2}}
\def\deco@step#1{%
\setbox1=\hbox{\deco@buffer{} #1}%
\dimen0\hsize\advance\dimen0-\parindent\relax
#1
\ifdim\wd1>\deco@scale\dimen0
\expandafter\deco@trail
\else
\addtobufferwithaspace\deco@buffer{#1}%
\expandafter\deco@parsei
\fi
}
\def\deco@trail#1\@nil{\deemphasizingcommand #1}
\def\deco@parsei#1{\ifnot@nil{#1}{\deco@parse #1}}
\def\deco@parse #1 {\deco@step{#1}}
\newcommand*{\decorate}[2][.95]{%
\def\deco@scale{#1}%
\def\deco@buffer{\emphasizingcommand}%
\emphasizingcommand
\deco@parse #2 \@nil
\deemphasizingcommand
}
\def\@@Decorate[#1] #2\par{\decorate[#1]{#2}\par}
\def\@@eDecorate[#1] #2\par{%
\def\@decorate{\decorate[#1]}%
\edef\@Decorated{\noexpand\@decorate{#2}}%
\@Decorated\par}
\newcommand*\@Decorate[1][.95]{\@@Decorate[#1] }
\newcommand*\@eDecorate[1][.95]{\@@eDecorate[#1] }
\newcommand*\Decorate{\@ifstar{\@eDecorate}{\@Decorate}}
\makeatother
\newcommand{\emphasizingcommand}{\bfseries}
\newcommand{\deemphasizingcommand}{\normalfont\normalsize}
\usepackage[nopar]{lipsum}
\begin{document}
\Decorate
I am (of course) shocked! and appalled! I have a particular typesetting
task, described below. Trying to find out how to emphasize the line of text
automatically. The issue here is that I have no idea where the line would
break. Ideally it would be start paragraph with slanted shape and then
revert to upright shape upon first line break.
\Decorate[.5] I am (of course) shocked! and appalled! I have a particular
typesetting task, described below. Trying to find out how to emphasize the
line of text automatically.
\renewcommand{\emphasizingcommand}{\bfseries\slshape}
\noindent\parbox{5cm}{\decorate{
I am (of course) shocked! and appalled! I have a particular typesetting
task, described below. Trying to find out how to emphasize the line of text
automatically. The issue here is that I have no idea where the line would
break. }}
\parbox{3cm}{\decorate{
I am (of course) shocked! and appalled! I have a particular typesetting
task, described below. Trying to find out how to emphasize the line of text
automatically. The issue here is that I have no idea where the line would
break. }}
\parbox{2.5cm}{\raggedright\decorate{
I am (of course) shocked! and appalled! I have a particular typesetting
task, described below. Trying to find out how to emphasize the line of text
automatically. The issue here is that I have no idea where the line would
break. }}
\parbox{2.5cm}{\raggedleft\decorate{
I am (of course) shocked! and appalled! I have a particular typesetting
task, described below. Trying to find out how to emphasize the line of text
automatically. The issue here is that I have no idea where the line would
break. }}
\renewcommand{\emphasizingcommand}{\ttfamily\Large\frenchspacing}
\renewcommand{\deemphasizingcommand}{\normalfont\normalsize\nonfrenchspacing}
\begin{center}
\begin{minipage}{0.5\linewidth}
\centering
\Decorate[1.5]
I am (of course) shocked! and appalled! I have a
particular typesetting task, described below. Trying to find out how to
emphasize the line of text automatically. The issue here is that I have no
idea where the line would break. Ideally it would be start paragraph with
slanted shape and then revert to upright shape upon first line break.
\par
\end{minipage}
\end{center}
\renewcommand{\emphasizingcommand}{\scshape}
\Decorate There is no just \emph{ground}, therefore, \mbox{$ a + b = c $} for
the charge $x\in\{0,1,2\}$ brought against me by certain ignoramuses $ x
\in\{ 0,1,2 \}$.
\Decorate Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna.
\Decorate[4] Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna.
\renewcommand{\emphasizingcommand}{\bfseries}
%% \clearpage
\decorate{Let's try with a short sentence.}
\decorate[.2]{And another one.}
\decorate[.2]{And another one.}
\decorate[.2]{And another one.}
\makeatletter
\Decorate* \lipsum@v
\Decorate* \lipsum@vi
\Decorate*[1] \lipsum@vii
\makeatother
\def\tmpa{dolor sit amet, consectetuer
adipiscing elit. Ut purus elit, }
\Decorate[.1] \tmpa \tmpa
\Decorate*[.1] \tmpa \tmpa
\end{document}