1

I have an environment (defined below). I would like to keep the whole environment instance from splitting across pages. One solution I came across is to wrap it in \vbox. So, my problem boils down to creating an environment that wraps another environment's instance in a vbox. Any help greatly appreciated :)

\NewDocumentEnvironment{exercise}{m}
{
} 
{ 
    Write your answer here
    \vskip #1cm
}
  • Are you just looking for a variation on minipage? – Andrew Swann Jun 29 '17 at 6:53
2

You can use \vbox\bgroup at the beginning and \egroup and the end. I don't know about \NewDocumentEnvironment, but normal LaTeX2e environments don't allow parameters where you have the \vskip. Here's an example.

\documentclass{article}
\newenvironment{exercise}[1]{%
    \def\exerciseskipamount{#1cm}%
    \vbox\bgroup
}{%
    \par
    Write your answer here.\par
    \vspace{\exerciseskipamount}%
    \egroup
}

\begin{document}

\begin{exercise}{8}
Here is the first exercise.
\end{exercise}

\begin{exercise}{8}
Here is the second exercise.
\end{exercise}

\begin{exercise}{8}
Here is the third exercise.
\end{exercise}

\end{document}

If you compile, you'll see 2 exercises on the first page and one on the second. Each of them is wrapped in a \vbox.

Of course, there are other packages for writing exercises that may fit your use case without you having to reinvent the wheel. Search TeX SX to find those.

  • thanks! \vbox\bgroup .... \egroup works perfectly :) (even with NewDocumentEnvironment. I will look for existing templates for exercises. – gaurav gupta Jun 29 '17 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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