22

It's possible to do this

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.

without writing the following code

\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.

but, for example, something like this?

\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}
5
  • 2
    If you imply that the end of line in the source is the command to force a newline and make a character bold, yes than it's easy enough, if you want to do normal tex linebreaking but make the first character of each line after the linebreak bold, then it's basically not possible in TeX Commented Jul 21, 2014 at 8:39
  • @David Carlisle The first you said!
    – ᴜsᴇʀ
    Commented Jul 21, 2014 at 8:46
  • OK the answer for that's been posted:-) Commented Jul 21, 2014 at 8:48
  • 2
    Is this question related to the topic of acrostics? (i.e., a hidden message using the first character of each line) Commented Jul 21, 2014 at 10:07
  • If this is about acrostics, then tex.stackexchange.com/questions/121440/… is related. Commented Jan 19, 2015 at 4:58

3 Answers 3

29

Assuming you do want fixed line ends:

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

enter image description here

To make a nice professional job of it, you could also add some spacing before and afterwards, and control the indent of each line.

A little explanation

As explained in the TeXbook (p. 352) the \obeylines macro is defined in plain TeX to make the end-of-line character (0x13, or \^^M) an "active" character, and then to make it equivalent to \par. The effect of this is to make TeX treat each line as a separate paragraph.

You can then customise the exact effect in two ways. You can redefine \par to do something different, or you can use the \everypar mechanism, as I have done above. Normally the \everypar token list is empty, but if you define it to be non-empty then all the tokens in the list are copied into the beginning of every paragraph. See pp. 282-283 of the TeXbook.

If you want to redefine \par then bear in mind the following points:

  • You should redefine it before calling \obeylines
  • TeX saves the original meaning of \par as a command called \endgraf, which can be useful
  • You can combine both techniques

Restrictions

These facilities are part of Plain TeX, and therefore only available to LaTeX in formats that are built on top of Plain TeX (which is most of the normal ones). The TeXbook has a number of examples of how to use \obeylines effectively. As defined above the boldfirst environment will only work at the top level of a document - you could not use it as the argument to another command for example.

4
  • 1
    Oooooh, \obeylines is going on the "may be useful" list.
    – Raphael
    Commented Jul 21, 2014 at 9:32
  • 1
    One should be aware of the fact that such a construction is mostly incompatible with the list environments and that this boldfirst environment cannot go as argument to another command such as \parbox.
    – egreg
    Commented Jul 22, 2014 at 8:23
  • @egreg true I'm sure, I'll add to the explanation. Any other restrictions of \obeylines that you are aware of?
    – Thruston
    Commented Jul 22, 2014 at 22:48
  • @Thruston It's not really a supported LaTeX command, of course.
    – egreg
    Commented Jul 22, 2014 at 23:02
24

Here's a more complicated version that tries to preserve kerning between the first two letters, as shown in the last two lines.

As in Thruston's answer, accented first characters must be enclosed in braces.

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

enter image description here

7
  • 1
    Try to add the line "A problem is here." to your environment:-)
    – wipet
    Commented Jul 21, 2014 at 9:33
  • You are implying that Thruston's answer skrews up kerning? I don't think his example displays that (for lack of a "critical" instance).
    – Raphael
    Commented Jul 21, 2014 at 9:34
  • 3
    @Raphael TeX never applies automatic kerning between two letters from different fonts
    – egreg
    Commented Jul 21, 2014 at 9:38
  • @egreg: Is there a way to have the same feature, but without breaking lines in the code — say, \lipsum[1]?
    – Bernard
    Commented Jul 21, 2014 at 10:08
  • 1
    @Bernard No, unless one goes LuaTeX.
    – egreg
    Commented Jul 21, 2014 at 10:22
8

Another simple solution is the parselines package:

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

MWE

A more complex format can be obtained easily replacing \textbf with a macro with 1 parameter:

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

MWE2

2
  • The limitations are the same as for \obeylines, because the mechanism used is activating the endline character. Of course, the possibility of doing something with every line is an added value.
    – egreg
    Commented Jul 23, 2014 at 9:33
  • @egreg, in fact, parselines mean munch more limitations than this (also possibilities, as you said) but it doesn't matter too munch as long as it catches that mouse.
    – Fran
    Commented Jul 23, 2014 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .