# Macros just for certain number of lines

I want to write a macro that will do certain things with a fixed number of lines coming after this macro.

For example, how to apply \rightskip=2cm to the first four lines of a paragraph

How to write a macro that will know some of the commands like \rightskip,\bfseries and the number of lines which it should affect with these commands?

\documentclass{article}
\usepackage{lipsum}

\begin{document}
\lipsum[1-2]
\end{document}

-
you need to be a lot more specific about what you need. You can affect the length of the lines for initial lines in a paragraph using \parshape. You can not change \rightskip in the way you suggest as that is a parameter set for the whole paragraph based on the value at the end. TeX does not break lines line-by-line it does a least cost optimisation of linebreaking over a paragraph after all macros in that paragraph have been expanded. – David Carlisle Mar 19 at 8:03

# Shape of paragraph

The geometry of a paragraph can be configured with \parshape, e.g.:

\documentclass{article}
\usepackage{lipsum}

\begin{document}
\parshape 5 % five pairs of settings for the left and right indent of a line
0pt \dimexpr\linewidth-2cm\relax % first line
0pt \dimexpr\linewidth-2cm\relax
0pt \dimexpr\linewidth-2cm\relax
0pt \dimexpr\linewidth-2cm\relax % fourth line
0pt \linewidth                   % remaining lines
\lipsum[1]
\end{document}


See also the answers for question How to define a parshape command with LaTeX? for more comfortable solutions.

# Font changes

There isn't an easy solution for font changes. There is a problem with circular dependencies: The paragraph builder needs to know the fonts in order to know the width of the text components to break the paragraph into lines. However, making the first four lines bold is not possible anymore at this stage; also the width would change and the lines would be too long.

## Manual solution

First the paragraph is set in bold, the document compiled and the result is inspected to get the break point between the forth and fifth line. With the hyphenation pattern for latin, its after the word Pellentesque. When the font is changed for the text of the first four lines, it might be possible, that the paragraph builder might find different break points. Therefore the the following example closes the paragraph, forcing the last line to fill the full line width. Then a new paragraph is added without additional vertical space and suppressed paragraph indentation.

\documentclass{article}
\usepackage{lipsum}
\usepackage[latin]{babel}

\begin{document}
\begingroup
\bfseries
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. Donec
vehicula augue eu neque. Pellentesque%
\setlength{\parfillskip}{0pt}%
\par
\setlength{\parskip}{0pt}%
\noindent
\endgroup
habitant morbi tristique
senectus et netus et malesuada fames ac turpis egestas. Mauris ut
leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna
fringilla ultrices.  Phasellus eu tellus sit amet tortor gravida
placerat. Integer sapien est, iaculis in, pretium quis, viverra ac,
nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus.
Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla.
Curabitur auctor semper nulla. Donec varius orci eget risus. Duis
nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis
eget orci sit amet orci dignissim rutrum.
\end{document}


-
I knew about \parshape in this way like you did it. I am talking about such ideal command like \doit{\parshape 1 0pt \dimexpr\linewidth-2cm\relax}{4}, where 4 - number of lines... But thanks to you. Now I guess that it is impossible for my desire to be true. – Dida Mar 19 at 8:24
– Heiko Oberdiek Mar 19 at 8:35
I saw it. Unfortunally, all tryings to copypaste it lead to the following: shot.qip.ru/00PIKb-5oHGoulrt – Dida Mar 19 at 9:03
@Dida The answers explain the method. Of course, you have to adjust the parameters to your needs. – Heiko Oberdiek Mar 19 at 13:21