3

I'm trying to create an environment where each paragraph

  • gets some stylized text prepended to it (e.g. "Tips: ")
  • is not allowed to be broken up across pages

Based on this answer, I've managed to achieve this using everypar and windowpenalties, but I'm wondering if there's a better way. I've read many times that everypar should be avoided if possible since many packages override it.

While my use of everypar is my main concern, I would also appreciate suggestions around windowpenalties. Is there a better approach?

MWE:

\documentclass[oneside]{book}

\usepackage{lipsum}

\usepackage{parskip}

\raggedbottom

\newenvironment{tips}
    {%
        \widowpenalties 1 10000%
        \everypar{{\fontshape{\itdefault}\fontseries{\bfdefault}\selectfont Tip:\enspace}}%
    }
    {}

\begin{document}

\lipsum[1-10]

\begin{tips}
\lipsum[1-10]
\end{tips}

\lipsum[1-10]

\end{document}
splicer
  • 265
  • 1
    Why not \itshape\bfseries? There is almost never a need to use \selectfont in LaTeX except in defining font commands, but here you just want standard out-of-the-box stuff: italic bold. Why the fuss? – cfr Aug 29 '16 at 02:44
  • @cfr Because I'm a beginner and I don't know any better. Thanks! – splicer Aug 29 '16 at 03:09
  • Oh, I see. If you are using those commands as a beginner, you should complain to your teacher! Or find a better source of information. (There's a question somewhere listing resources for beginners.) I teach beginners LaTeX and I definitely don't tell them to use \selectfont. (I can't remember if I show them this at all, but I certainly don't suggest they use it.) – cfr Aug 29 '16 at 03:52
  • I assumed if you were trying to do this that you knew what you were doing ;). (I've never used \everypar that I recall. I don't remember ever using \widowpenalties either ....) – cfr Aug 29 '16 at 03:57
  • I'm reteaching myself after a decade hiatus, mainly through this website (although I have skimmed a few ctan manuals as well). – splicer Aug 29 '16 at 04:48
  • Could redefining \par within the environment work? – splicer Aug 29 '16 at 04:55
  • 2
    To prohibit page breaks bewtween any two lines of a paragraph, you must set \interlinepenalty = 10000, not \widoowpenalties 1 = 10000. – GuM Aug 29 '16 at 08:03

2 Answers2

4

I don't suggest using this. Stick to \everypar. It will be much safer. However, there is work in the pipeline which will make this easier in the future, thanks to the efforts of the LaTeX 3 developers.

Here's an example, just for the \everypar bit:

\documentclass[oneside]{book}
\usepackage{lipsum}
\usepackage{parskip,l3galley,xparse}
\raggedbottom
\ExplSyntaxOn
\tl_new:N \g_splicer_saved_tl
\NewDocumentEnvironment{tips}{}
{
  \group_begin:
  \tl_gset:NV \g_splicer_saved_tl \g_galley_par_begin_hook_tl
  \tl_gput_right:Nn \g_galley_par_begin_hook_tl
  {
    \textit{ \bfseries Tip: } \enspace
  }
}
{
  \tl_gset:NV \g_galley_par_begin_hook_tl \g_splicer_saved_tl
  \group_end:
}
\ExplSyntaxOff
\begin{document}

\lipsum[1-10]

\begin{tips}
\lipsum[1-10]
\end{tips}

\lipsum[1-10]

\end{document}

L3 tips

cfr
  • 198,882
1

Another, not perfect, option.

\documentclass{scrartcl}

\usepackage{kantlipsum}

\newenvironment{tips}
  {\def\par{\futurelet\tmptoken\dopar}%
   \def\dopar{\ifx\tmptoken\par\else\ifx\tmptoken\endgraf\else\ifx\tmptoken\end\else
       \endgraf\nobreak\textbf{\textit{Tip}:\enspace}\fi\fi\fi}%
   \interlinepenalty=10000 \par}
  {\endgraf}

\begin{document}

\begin{tips}
\kant*[1]

\kant*[2]

\kant*[3]
\end{tips}

\end{document}

However, I think this is much safer (and not less simple):

\newenvironment{tips}
  {\par
   \def\tip{\par\nobreak\textbf{\textit{Tip}:\enspace}}%
   \interlinepenalty=10000 }
  {\par}

and use

\begin{tips}

\tip whatever
\tip you
\tip want
\tip \kant[2]

\end{tips}
Manuel
  • 27,118
  • Thanks Manuel! In your second suggested approach, could you please explain what the extra \par commands do? I thought that having multiple \par in a row would insert a bunch of unwanted vertical space, and I was surprised to find that LaTeX collapses them. – splicer Aug 29 '16 at 18:00
  • 1
    \par ends a paragraph, if you already ended one, and there's nothing more, the next \par doesn't end another paragraph, so, in a way, as you say, TeX collapses them. The extra \pars are there to be safe, to ensure that the paragraph before \begin{tips} is properly ended, and to ensure that the paragraph ends after the last \tip (if not, the next lines of text might be taken as part of that same paragraph). – Manuel Aug 29 '16 at 18:05
  • Got it. I'm going to go with your second approach. Thanks! – splicer Aug 29 '16 at 18:14