4

I intend to combine the itemize environment with the minipage environment to prevent paragraphs from appearing on separate pages, but as a result the alignment of the bullet and the text changes from
Desired result
to
Actual result
To clarify, I'm looking for a way to combine minipage and itemize/enumerate like the code below to get the same spacing between the bullet and text as in the first example.

\documentclass{article}
\begin{document}
\begin{itemize}
 \item
  \begin{minipage}{\textwidth}
    Lorem\\
    Ipsum
  \end{minipage}
\end{itemize}
\end{document}

EDIT: The option [t] for minipage has been suggested, and generally works, but is not fully compatible with other environments. When pairing it with the array environment as follows, the bullet resets to a centered position.

\documentclass{article}
\begin{document}
\begin{itemize}
 \item
  \begin{minipage}[t]{\textwidth}
    $\begin{array}{rcl}
    x &=& y\\
    y &=& x\\
    2x &\neq& 5y
    \end{array}$
  \end{minipage}
\end{itemize}
\end{document}  

Array doesn't respect option t

Iterniam
  • 143

2 Answers2

4

Using option [t] for minipage does the trick:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{lipsum}
\usepackage{enumerate}
\begin{document}
\begin{itemize}
\item
  \begin{minipage}[t]{\linewidth} % Thanks to Bernard
    \lipsum[1]

    \lipsum[2]
  \end{minipage}
\end{itemize}
\lipsum[3]
\end{document}

enter image description here

Note: The \usepackage[showframe]{geometry} is only for showing margin, you can delete it in actual document.

Edit:

As for the conflict:

The reason for the conflicts is that the whole array environment is considered to be an element, like a letter in the line, which means the array environment will be "centerized" with the first line.

A quick and tricky way to solve this:

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{itemize}
 \item
  \begin{minipage}[t]{\linewidth}
    \raisebox{-\baselineskip}{$\begin{array}{rcl}
    x &=& y\\
    y &=& x\\
    2x &\neq& 5y
    \end{array}$}
  \end{minipage}
\end{itemize}
\end{document} 

enter image description here

A standard solution for the array: We use [t] once again (thanks to koleygr):

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{itemize}
 \item
  \begin{minipage}[t]{\linewidth}
    $\begin{array}[t]{rcl}
    x &=& y\\
    y &=& x\\
    2x &\neq& 5y
    \end{array}$
  \end{minipage}
\end{itemize}
\end{document} 

enter image description here

  • Even with the simpler \begin{minipage}[t]{\textwidth}, it works. – Bernard Mar 04 '19 at 14:10
  • @Bernard Yes, but for the OP's example only. If he/she has a paragraph in the item, we have to do some calculations so that it doesn't get "Overfull hbox...". Warnings are not fatal, but they annoy me ;-) and the output is bad then. –  Mar 04 '19 at 14:11
  • That's right, but check what happens replacing \textwidth with \linewidth. No \dimexpr necessary. – Bernard Mar 04 '19 at 14:15
  • @Bernard Oh it is so simple! Thank you very much! –  Mar 04 '19 at 14:15
  • Thanks for all the help! I have yet one conflict left, and it's with the lstlisting environment from the listings package. Using the same logic with which you solved the array exception, I get a ton of errors I'm not expecting. – Iterniam Mar 04 '19 at 14:34
  • @Iterniam lstlisting, and minted or verbatim are very special environments. However, do you really want to have a lstlisting in an \item? It is very rare! –  Mar 04 '19 at 14:39
  • @JouleV It's for code fragments in homework, for which I use a combination of \section and itemize. I found that for lstlisting the command \vspace{-1.5\baselineskip} before starting the environment works well. – Iterniam Mar 04 '19 at 14:54
  • @Iterniam I'm very glad to hear that you find a solution yourself! Congratulations. –  Mar 04 '19 at 14:55
  • 1
    For the array a \begin{array}[t] should work too – koleygr Mar 04 '19 at 14:56
  • @koleygr Thank you very much! It is very useful. I don't know about this before :) –  Mar 04 '19 at 14:59
  • 1
    welcome... We all learn every day... (You already have my +1) – koleygr Mar 04 '19 at 15:00
2

What I would do is this:

\documentclass{article}
\usepackage{lipsum}
\newsavebox{\mybottombox} % Box to save the text of the command 
\newlength{\mybottomlength} % The length of our text inside the command
\newlength{\availafter} % The available length left on the page after placing our text


% Optional argument is the minimum length after the nobottom text for not pagebreak. Change it to your needs
\newcommand{\mnobreak}[2][0pt]{\savebox{\mybottombox}{\vbox{#2}}\setlength{\mybottomlength}{\ht\mybottombox}%
\setlength{\availafter}{\dimexpr\textheight-\mybottomlength-\pagetotal\relax}\ifdim\availafter<#1%
\pagebreak\noindent\usebox{\mybottombox}%
\else%
\noindent\usebox{\mybottombox}%
\fi%
}%

\begin{document}
\lipsum[1-4]

To clarify, I'm looking for a way to combine minipage and itemize/enumerate like the code below to get the same spacing between the bullet and text as in the first example. To clarify, I'm looking for a way to combine minipage and itemize/enumerate like the code below to get the same spacing between the bullet and text as in the first example.

\begin{itemize}
 \item \mnobreak{Lorem\\Lipsum\\Lorem\\Lipsum\\Lorem}
\end{itemize}
\end{document}

Remove the last Lorem to see that the item would break with it.

Source: my old answer here

Output:

enter image description here

koleygr
  • 20,105