As John Kormylo suggested, you can use \vtop
(the internal command \parbox[t]...
use) to prevent from item
's to break between pages. Instead of adding the necessary curly braces, with \vtop
you can wrap the argument with \bgroup
and \egroup
, which might be easier if you are using LyX.
Personally, I wouldn't redefine \item
, as there are more than several environments that use it internally, even some that might surprise you (such as environments defined with \newtheorem
or amsthm
's proof environment).
As mentioned in the comments, you can use LyX's ItemCommand
to replace \item
with the desired code, instead of redefining \item
.
So, basically we want to replace \item
with \item\leavevmode\vtop{<text>}
, but that would be hard, so we can replace it with \item\leavevmode\vtop\bgroup
. To close the group, just prepend \egroup
to \item
, but that would create unbalanced grouping with the first and last use of \egroup\item\leavevmode\vtop\bgroup
, luckily we can fix it with some hooks. We also nee to add \hsize\linewidth\@parboxrestore
after \bgroup
to get the correct width and indentation.
Here is a LyX file that demonstrate the idea:
#LyX 2.3 created this file. For more info see http://www.lyx.org/
\lyxformat 544
\begin_document
\begin_header
\save_transient_properties true
\origin unavailable
\textclass article
\begin_preamble
%% my attempt:
%\let\OldItem\item
%\renewcommand{\item}{\OldItem \ifnum \@listdepth=1 \begin{samepage}\fi}
%\AfterEndEnvironment{enumerate}{\ifnum \@listdepth=1 \end{samepage}\fi}
\AddToHook{cmd/enumerate/after}{\bgroup}
\AddToHook{env/enumerate/end}{\egroup}
\newcommand*\myitem{\egroup\item\leavevmode\vtop\bgroup\hsize\linewidth\@parboxrestore}
\end_preamble
\use_default_options false
\maintain_unincluded_children false
\begin_local_layout
Format 66
Style Enumerate
ItemCommand myitem
End
\end_local_layout
\language english
\language_package none
\inputencoding auto
\fontencoding default
\font_roman "default" "default"
\font_sans "default" "default"
\font_typewriter "default" "default"
\font_math "auto" "auto"
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100 100
\font_tt_scale 100 100
\use_microtype false
\use_dash_ligatures true
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_package amsmath 1
\use_package amssymb 0
\use_package cancel 0
\use_package esint 1
\use_package mathdots 0
\use_package mathtools 0
\use_package mhchem 0
\use_package stackrel 0
\use_package stmaryrd 0
\use_package undertilde 0
\cite_engine basic
\cite_engine_type default
\biblio_style plain
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 0
\use_minted 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\is_math_indent 0
\math_numbering_side default
\quotes_style english
\dynamic_quotes 0
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Enumerate
2 + 2 = ?
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\end_layout
\begin_deeper
\begin_layout Enumerate
1
\end_layout
\begin_layout Enumerate
2
\end_layout
\begin_layout Enumerate
3
\end_layout
\begin_layout Enumerate
4
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
% I do not want to put this page break in manually:
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
%
\backslash
newpage
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\end_deeper
\begin_layout Enumerate
Consider the joint probability distribution below: filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\end_layout
\begin_deeper
\begin_layout Standard
\align center
\begin_inset Tabular
<lyxtabular version="3" rows="3" columns="3">
<features tabularvalignment="middle">
<column alignment="center" valignment="top">
<column alignment="center" valignment="top">
<column alignment="center" valignment="top">
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\begin_inset Formula $X_{2}=0$
\end_inset
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\begin_inset Formula $X_{2}=1$
\end_inset
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\begin_inset Formula $X_{1}=0$
\end_inset
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\begin_inset Formula $\frac{1}{10}$
\end_inset
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\begin_inset Formula $\frac{3}{10}$
\end_inset
\end_layout
\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\begin_inset Formula $X_{1}=1$
\end_inset
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\begin_inset Formula $\frac{2}{10}$
\end_inset
\end_layout
\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" rightline="true" usebox="none">
\begin_inset Text
\begin_layout Plain Layout
\begin_inset Formula $\frac{4}{10}$
\end_inset
\end_layout
\end_inset
</cell>
</row>
</lyxtabular>
\end_inset
\end_layout
\begin_layout Standard
What is
\begin_inset Formula $P(X_{1}=1)$
\end_inset
?
\end_layout
\begin_layout Enumerate
I do not know.
\end_layout
\begin_layout Enumerate
4
\end_layout
\begin_layout Enumerate
17
\end_layout
\begin_layout Enumerate
88
\end_layout
\end_deeper
\begin_layout Enumerate
2 - 2 + 5 = filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\begin_inset Newline newline
\end_inset
filler
\end_layout
\begin_deeper
\begin_layout Enumerate
1
\end_layout
\begin_layout Enumerate
2
\end_layout
\begin_layout Enumerate
3
\end_layout
\begin_layout Enumerate
4
\end_layout
\end_deeper
\end_body
\end_document
And its LateX output is (for all of you worrying about the fact that LyX adds \usepackage[latin9]{inputenc}
to the preamble, it is fixed in LyX 2.4):
%% LyX 2.3.7 created this file. For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass{article}
\usepackage[latin9]{inputenc}
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
%% my attempt:
%\let\OldItem\item
%\renewcommand{\item}{\OldItem \ifnum \@listdepth=1 \begin{samepage}\fi}
%\AfterEndEnvironment{enumerate}{\ifnum \@listdepth=1 \end{samepage}\fi}
\AddToHook{cmd/enumerate/after}{\bgroup}
\AddToHook{env/enumerate/end}{\egroup}
\newcommand*\myitem{\egroup\item\leavevmode\vtop\bgroup\hsize\linewidth\@parboxrestore}
\makeatother
\begin{document}
\begin{enumerate}
\myitem 2 + 2 = ? \\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler
\begin{enumerate}
\myitem 1
\myitem 2
\myitem 3
\myitem 4
\end{enumerate}
% I do not want to put this page break in manually:
% \newpage
\myitem Consider the joint probability distribution below: filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler
\begin{center}
\begin{tabular}{|ccc|}
\hline
& $X_{2}=0$ & $X_{2}=1$\tabularnewline
\hline
$X_{1}=0$ & $\frac{1}{10}$ & $\frac{3}{10}$\tabularnewline
\hline
$X_{1}=1$ & $\frac{2}{10}$ & $\frac{4}{10}$\tabularnewline
\hline
\end{tabular}
\par\end{center}
What is $P(X_{1}=1)$?
\begin{enumerate}
\myitem I do not know.
\myitem 4
\myitem 17
\myitem 88
\end{enumerate}
\myitem 2 - 2 + 5 = filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler\\
filler
\begin{enumerate}
\myitem 1
\myitem 2
\myitem 3
\myitem 4
\end{enumerate}
\end{enumerate}
\end{document}
\item\parbox{\linewidth}{...}
. You might also want to add some glue to\itemsep
.\item
to do what you suggest?ItemCommand
attribute to change the command that LyX inserts inside theenumerate
environment.\item{...}
.\item ...
to use\item{...}
. That is indeed a minimal change but I'm hoping to avoid making any changes. @UdiFogiel indeed I hope to use it with LyX. That's good to know I can change the ItemCommand. But I don't know if I could change it to wrap the contents of\item
in the braces.