Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to setup a new environment which typesets the first paragraph in boldface, the following paragraphs in normal text, and with (possibly) no pagebreaks between the first paragraph and the remaining text. How can I do?

For example, something like the following:

\begin{question_and_answer}
This is a question, which should be highlited in bold 
(or by changing font, or by another method) and preceded by a progressive number.

This is the answer, in normal font.

The answer is composed by one or more paragraph. There shouldn't be a 
pagebreak between the first paragraph and the second.
\end{question_and_answer}

Or maybe something like this:

\begin{question}
This is the question, as the previous example
\end{question}
% no pagebreak here
\begin{answer}
This is the answer, as the previous example
\end{answer}
share|improve this question
    
Do you want a special formation of all paragraphs? Normally you can put the first paragraph in a minipage. The special minipage can be defined in the header. –  Marco Daniel Sep 25 '11 at 8:29
    
The first paragraph should have a progressive number, and the text in bold. The following paragraphs are "normal". –  zar Sep 25 '11 at 8:33
add comment

2 Answers

up vote 7 down vote accepted

If your paragraphs have only normal text, you can define

\newenvironment{highlightnobreak}
  {\par\bfseries
   \def\par{\widowpenalty=10000 \endgraf\penalty10000 }}
  {\par}

Use it like

... some text before.

\begin{highlightnobreak}
The first paragraph here will be typeset in bold face.
\end{highlightnobreak}

And this text, which is in the paragraph following the
environment will be in normal type.

The highlighted paragraph will never end along with a page and will be always accompanied by at least one line of the following paragraph on a page. A page break can however happen before the second-to-last line of the highlighted paragraph.

More customization is possible. For example, a vertical space after the highlighted paragraph can be inserted modifying the definition into

\newenvironment{highlightnobreak}
  {\par\bfseries
   \def\par{\widowpenalty=10000 \endgraf\penalty10000 \medskip}}
  {\par}

We can improve it with a progressive number and also a vertical space before it, to match the one after:

\newcounter{highlightpar}
\newenvironment{highlightnobreak}
  {\medskip\par
   \def\par{\widowpenalty=10000 \endgraf\penalty10000 \medskip}%
   \stepcounter{highlightpar}\bfseries\thehighligthpar. \ignorespaces}
  {\par}

TeXnical note

It's important that the \par that closes the environment does more things than usual: here we order it to set \widowpenalty to a no-break-before-the-last-line value, then to finish off the paragraph and to tell TeX no-pagebreak-here.

The relevant items TeX will see are then

〈last line of the highlighed paragraph〉
〈penalty 10000〉
\medskipamount glue〉
\parskip glue〉
\baselineskip glue〉
〈first line of the following paragraph〉

so that, by rule, it will not break pages at any of the glue items, because they are not preceded by nondiscardable items. Only the penalty would be a feasible page break point, but it's "infinitely big" and so TeX won't consider it.

share|improve this answer
    
I would like to insert a \medspace after the highlighted paragraph. If I substitute your definition with \newenvironment{highlightnobreak} {\par\bfseries \def\par{\widowpenalty=10000 \endgraf\penalty10000 }} {\par\medskip} then a page break could be inserted. How can I do? –  zar Sep 25 '11 at 10:32
    
(And the various paragraphs could have some math material) –  zar Sep 25 '11 at 10:50
    
I would advise you against this kind of highlighting: long parts in boldface are hard to read. Can you add to your question a sample text? –  egreg Sep 25 '11 at 10:56
    
The first paragraph is always short, one or two lines (it is a "question"). The following paragraphs are the "answer". Anyway, I could change font, instead of using a blodface one, or emphasize the question in another way. –  zar Sep 25 '11 at 12:30
    
Will I have some problem, if I put the text following the highlighted paragraph into another block environment? –  zar Sep 25 '11 at 12:42
show 1 more comment

You can redefine \par locally to reset the font and use \@par which resets \par to its normal definition. I added the penalties given by egreg as well which should avoid a pagebreak right between the two paragraphs. If you want to have no page break at all in all included paragraphs than you can wrap the whole thing in a minipage.

\documentclass{article}

\makeatletter
\newenvironment{myblock}{%
    \par\bfseries
    \def\par{%
        \widowpenalty=10000
        \@par\normalfont
        \penalty10000
    }%
}{%
    \par
}
\makeatother

\begin{document}

\begin{myblock}
    text text text text
    text text text text
    text text text text
    text text text text

    further text further text
    further text further text
    further text further text
    further text further text

    further text further text
    further text further text
    further text further text
    further text further text
\end{myblock}

\end{document}

Result

share|improve this answer
    
How can I insert a little unbreakable space between the first and the second paragraph? –  zar Sep 25 '11 at 10:49
    
I considered that, but realized that only the boldfaced paragraph needs to be in the environment, so the redefinition of \par can be achieved simply by closing the environment. Therefore the \widowpenalty setting will hold only for that paragraph and not through all the environment (it's a minor point, I know, but for other settings it might be important). –  egreg Sep 25 '11 at 11:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.