1

I've defined a shortcut to create boxed questions like this:

\usepackage{enumitem}
\usepackage{mdframed}
\newcommand{\questionlabel}{Question }
\newcommand{\restorequestionlabel}{\renewcommand{\questionlabel}{Question }}
\newenvironment{questions}
    {\begin{enumerate}[align=left,label=\textbf{\questionlabel\arabic*.}, wide]}
    {\end{enumerate}}
\newcommand{\itembr}{\item\mbox{}}
\newcommand{\question}[1]{\itembr\begin{mdframed}#1\end{mdframed}}

When used like this usually produces what I want:

\begin{questions}

\question{Lorem ipsum dolor sit amet?}
consectetur adipiscing elit

\question{sed do eiusmod tempor}
 incididunt ut labore et dolore

\end{questions}

Questions displaying properly

Unfortunately, sometimes the page break occurs between the label Question ?. and the top of the box. I'm using mdframed and I do want the boxes to split across the pages, but not before the first line.

I've tried putting \nopagebreak in different places but it didn't work. I've seen other posts suggesting to use minipage but is not ideal since it wouldn't allow page breaks at any other line.

1 Answers1

0

I followed this answer and used the package needspace to require a certain amount of space without a pagebreak in each of your question commands:

\documentclass{article}
\usepackage{lipsum}
\usepackage{needspace}
\usepackage{enumitem}
\usepackage{mdframed}
\newcommand{\questionlabel}{Question }
\newcommand{\restorequestionlabel}{\renewcommand{\questionlabel}{Question }}
\newenvironment{questions}
    {\begin{enumerate}[align=left,label=\textbf{\questionlabel\arabic*.}, wide]}
    {\end{enumerate}}
\newcommand{\itembr}{\needspace{6\baselineskip}\item\mbox{}}
\newcommand{\question}[1]{\itembr\begin{mdframed}#1\end{mdframed}}

\begin{document}
\lipsum[1-4]
\begin{questions}
\question{Lorem ipsum dolor sit amet?}
consectetur adipiscing elit
\question{sed do eiusmod tempor}
 incididunt ut labore et dolore
\end{questions}
\end{document}

To use 6\baselineskip was just by try-and-error, there might be a better way of getting the needed size.

Tiuri
  • 7,749