自动为每行第一个字符添加粗体

自动为每行第一个字符添加粗体

这是可能的

大号orem ipsum dolor sit amet,

Consectetur adipisci elit, sed eiusmod

劳动者发生冲突

t 多洛雷·玛格纳·阿里夸。乌特恩尼姆

Ad 最低限度的赔偿,即他人的赔偿

干燥剂 ullamco laboris nisi ut aliquid

x ea 商品劣势。谁也没有

谴责在欲望中

sse cillum dolore eu fugiat nulla pariatur。

xcepteur sint obcaecat cupiditat non proident,

s不履行职责

ollit anim id est laborum。

无需编写以下代码

\textbf{L}orem ipsum dolor sit amet, \\
\textbf{c}onsectetur adipisci elit, sed eiusmod \\
\textbf{t}empor incidunt ut labore \\
\textbf{e}t dolore magna aliqua. Ut enim \\
\textbf{a}d minim veniam, quis nostrud \\
\textbf{e}xercitation ullamco laboris nisi ut aliquid \\
\textbf{e}x ea commodi consequat. Quis aute \\
\textbf{i}ure reprehenderit in voluptate velit \\
\textbf{e}sse cillum dolore eu fugiat nulla pariatur. \\
\textbf{E}xcepteur sint obcaecat cupiditat non proident, \\
\textbf{s}unt in culpa qui officia deserunt \\
\textbf{m}ollit anim id est laborum.

但是,例如这样的事情?

\begin{bold_first}
Lorem ipsum dolor sit amet, \\
consectetur adipisci elit, sed eiusmod \\
tempor incidunt ut labore \\
et dolore magna aliqua. Ut enim \\
ad minim veniam, quis nostrud \\
exercitation ullamco laboris nisi ut aliquid \\
ex ea commodi consequat. Quis aute \\
iure reprehenderit in voluptate velit \\
esse cillum dolore eu fugiat nulla pariatur. \\
Excepteur sint obcaecat cupiditat non proident, \\
sunt in culpa qui officia deserunt \\
mollit anim id est laborum.
\end{bold_first}

答案1

假设您确实想要固定线路末端:

\documentclass{article}
\newcommand\dob[1]{\textbf{#1}}
\newenvironment{boldfirst}{\obeylines\everypar{\dob}}{}
\begin{document}
\begin{boldfirst}
Lorem ipsum dolor sit amet,
consectetur adipisci elit, sed eiusmod
tempor incidunt ut labore
et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquid
ex ea commodi consequat. Quis aute
iure reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint obcaecat cupiditat non proident,
sunt in culpa qui officia deserunt
mollit anim id est laborum.
\end{boldfirst}
\end{document}

在此处输入图片描述

为了使其更专业,您还可以在前后添加一些间距,并控制每行的缩进。

一点解释

正如 TeXbook (第 352 页) 中所述,该\obeylines宏在纯 TeX 中定义为使行尾字符 (0x13 或\^^M) 成为“活动”字符,然后使其等同于\par。这样做的效果是让 TeX 将每一行视为一个单独的段落。

然后,您可以通过两种方式自定义确切的效果。您可以重新定义\par以执行不同的操作,也可以使用机制\everypar,就像我上面所做的那样。通常,\everypar标记列表是空的,但是如果您将其定义为非空,则列表中的所有标记都会复制到每个段落的开头。请参阅 TeXbook 第 282-283 页。

如果您想重新定义\par,请记住以下几点:

  • 你应该重新定义它呼叫\obeylines
  • TeX 将 的原始含义保存\par为名为 的命令\endgraf,这可能很有用
  • 您可以结合这两种技术

限制

这些功能是 Plain TeX 的一部分,因此仅适用于在 Plain TeX 之上构建的格式的 LaTeX(大多数是常规格式)。TeXbook 有许多如何\obeylines有效使用的示例。如上所述,boldfirst环境仅在文档的顶层起作用 - 例如,您不能将其用作另一个命令的参数。

答案2

这是一个更复杂的版本,它尝试保留前两个字母之间的字距,如最后两行所示。

正如 Thruston 的回答一样,重音的第一个字符必须用括号括起来。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{environ,expl3}

\ExplSyntaxOn

% with the help of \NewEnviron, boldfirst absorbs the
% entire contents of the environment and stores it in \BODY
\NewEnviron{boldfirst}{%
  \begin{verse}
  \user_boldfirst:V \BODY % we pass the contents to \user_boldfirst:V
  \end{verse}
}

% some variables for later usage    
\seq_new:N \l__user_lines_seq
\seq_new:N \l__user_aline_seq
\tl_new:N \l__user_firstword_tl

% we define the function that processes the environment's text
\cs_new_protected:Npn \user_boldfirst:n #1
 {
  % the sequence \l__user_lines_seq will contain the lines
  \seq_set_split:Nnn \l__user_lines_seq { \\ } { #1 }
  % now we pass each line to be processed by \__user_bolden_line:n
  \seq_map_function:NN \l__user_lines_seq \__user_bolden_line:n
 }

% \user_boldfirst:n expects a braced argument
% so we generate a variant that expects a token list
% variable as argument
\cs_generate_variant:Nn \user_boldfirst:n { V }

% now we process a line
\cs_new_protected:Npn \__user_bolden_line:n #1
 {
  % split the line at spaces
  \seq_set_split:Nnn \l__user_aline_seq { ~ } { #1 }
  % save the first word in a token list variable
  % and pop it off the sequence of words
  \seq_pop_left:NN \l__user_aline_seq \l__user_firstword_tl
  % apply the “make the first letter bold” function and
  % and print the first word
  \__user_print_first:V \l__user_firstword_tl
  % use the rest of the word restoring spaces between them
  % and end the line
  \seq_use:Nn \l__user_aline_seq { ~ } \\
 }

% we bolden the first letter and print the word
\cs_new_protected:Npn \__user_print_first:n #1
 {
  \int_compare:nTF { \tl_count:n { #1 } == 1 }
   {% if the first word is a single letter just print it bold
    \textbf{#1} ~ % A space goes here
   }
   {% otherwise we need to get the first two letters to restore kerning
    \__user_bfkern:nnw #1 \q_stop
   }
 }

% as before, \__user_print_first:n expects a braced argument
% but we want to pass it a token list variable
\cs_generate_variant:Nn \__user_print_first:n { V }

% we know that there are at least two items in the token list
% so we save the first as #1, the second as #2 and the rest as #3    
\cs_new_protected:Npn \__user_bfkern:nnw #1 #2 #3 \q_stop
 {
  % make two boxes: the difference in width will be the kern to apply
  % one with the two letters next to each other
  \hbox_set:Nn \l_tmpa_box { #1 #2 }
  % one with a zero kern between them
  \hbox_set:Nn \l_tmpb_box { #1 \kern0pt #2 }
  % now we bolden #1
  \textbf{#1}
  % apply the kern
  \kern\dim_eval:n { \box_wd:N \l_tmpa_box - \box_wd:N \l_tmpb_box } 
  % print the rest of the word and a space
  #2 #3 ~ %
 }

\ExplSyntaxOff

\begin{document}

\begin{boldfirst}
Lorem ipsum dolor sit amet, \\
consectetur adipisci elit, sed eiusmod \\
tempor incidunt ut labore \\
et dolore magna aliqua. Ut enim \\
ad minim veniam, quis nostrud \\
exercitation ullamco laboris nisi ut aliquid \\
ex ea commodi consequat. Quis aute \\
iure reprehenderit in voluptate velit \\
esse cillum dolore eu fugiat nulla pariatur. \\
{\^{E}}xcepteur sint obcaecat cupiditat non proident, \\
sunt in culpa qui officia deserunt \\
mollit anim id est laborum. \\
T{}o show kern \\
To show kern
\end{boldfirst}

\end{document}

在此处输入图片描述

答案3

另一个简单的解决方案是parselines包:

\documentclass{article}
\usepackage{parselines}
\begin{document}
\begin{parse lines}[]{\textbf #1  \par}
Lorem ipsum dolor sit amet, 
consectetur adipisci elit, sed eiusmod 
tempor incidunt ut labore 
\end{parse lines}
\end{document}

平均能量损失

可以轻松获得更复杂的格式,\textbf只需用 1 个参数的宏替换即可:

\documentclass{article}
\usepackage{parselines}
\newcommand\FirstChar[1]{\textbf{\large\sffamily#1}}
\begin{document}
\begin{parse lines}[]{\FirstChar #1 \par}
Lorem ipsum dolor sit amet, 
consectetur adipisci elit, sed eiusmod 
tempor incidunt ut labore 
\end{parse lines}
\end{document}

微波辐射计

相关内容