# Skip to next column if too few lines remain in current column to complete an environment

I'm using

\documentclass[addpoints,answers,12pt]{exam}
\usepackage{multicol}


to generate Multiple Choice Question paper in two columns.

I want to start new question on next column, if less than three lines are remaining in a given column?

Can you please suggest, how can i achieve same?

Sample code used is mentioned below:

    \documentclass[addpoints,answers,12pt]{exam}
\usepackage{multicol}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{nonfloat}
\usepackage{caption}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{textcomp}
\usepackage{ifpdf}
\begin{document}
\begin{multicols}{2}
\begin{questions}
\question If $\displaystyle y = {\sin ^{ - 1}}\left( {\frac{{2\,\,\tan \,\,x}}{{1 - \,\,{{\tan }^2}\,\,x}}} \right),$  then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle \frac{{2\,\,{{\sec }^2}\,\,2x}}{{\sqrt {1 - \,\,{{\tan }^2}\,\,2x} }}$
\choice 2
\choice $\displaystyle \frac{{{{\sec }^2}\,\,2x}}{{\sqrt {1 - \,\,{{\tan }^2}\,\,x} }}$
\choice 1
\end{choices}
\question If $\displaystyle x{e^y} = {y^2}$  then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle \frac{{ - y}}{{x\,\,\left( {2 - y} \right)}}$
\choice $\displaystyle \frac{y}{{x\,\,\left( {2 - y} \right)}}$
\choice $\displaystyle \frac{x}{{y\,\,\,\left( {2 - x} \right)}}$
\choice $\displaystyle \frac{{ - x}}{{y\,\,\,\left( {2 - y} \right)}}$
\end{choices}
\question If $\displaystyle y = {\log _{10}}\left( {\log \,\,x} \right),$  then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle \frac{1}{{x\,\,\log \,\,x\,\,\log \,\,\,10}}$
\choice $\displaystyle \frac{{\log \,\,x}}{{x\,\,\log \,\,10}}$
\choice $\displaystyle \frac{{ - 1}}{{x\,\,\log x\,\log \,\,10}}$
\choice $\displaystyle - \frac{{\log \,\,x}}{{x\,\,\log \,\,10}}$
\end{choices}
\question If $\displaystyle x{y^2} = 1,$  then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle \frac{{{y^2}}}{2}$
\choice $\displaystyle \frac{{ - y}}{{2x}}$
\choice $\displaystyle 2{y^2}$
\choice $\displaystyle - 2{y^2}$
\end{choices}
\question If $\displaystyle x + {\tan ^{ - 1}}x = y + {\tan ^{ - 1}}\,\,y,$  then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle \frac{{{y^2}\left( {{x^2} + 2} \right)}}{{{x^2}\left( {{y^2} + 2} \right)}}$
\choice $\displaystyle \frac{{{x^2} + 2}}{{{y^2} + 2}}$
\choice $\displaystyle \frac{{\left( {{x^2} + 2} \right)\left( {{y^2} + 1} \right)}}{{\left( {{x^2} + 1} \right)\left( {{y^2} + 2} \right)}}$
\choice $\displaystyle \frac{{{y^2} + 2}}{{{x^2} + 2}}$
\end{choices}
\question If $\displaystyle y = \log \,\left( {\sin \,\,x} \right) + \sin \,\,\left( {\log \,\,x} \right),$  then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle \cot x + \frac{{\cos \left( {\log \,\,x} \right)}}{x}$
\choice $\displaystyle \frac{1}{{\sin \,\,x}} + \sin \left( {\frac{1}{x}} \right)$
\choice $\displaystyle \cot x - \frac{{\cos \left( {\log \,\,x} \right)}}{x}$
\choice $\displaystyle \cot x - x\,\,\left( {\log \,\,x} \right)$
\end{choices}
\question If $\displaystyle x = t + \frac{1}{t},y = t - \frac{1}{t}$  Then $\displaystyle \frac{{{d^2}y}}{{d{x^2}}}$  at $\displaystyle t = 2$  is
\begin{choices} \choice $\displaystyle - \frac{{32}}{{27}}$
\choice $\displaystyle \frac{{32}}{{27}}$
\choice $\displaystyle \frac{{16}}{{27}}$
\choice $\displaystyle \frac{8}{{27}}$
\end{choices}
\question If  $\displaystyle y = x + \frac{{\,\,\,\,\,1}}{{x + \frac{1}{{x + \frac{{\,\,\,\,\,\,\,\,\,\,\,\,\,\,1}}{{x + .....\,\,\,\,\,\,\,\,\,\,\,\infty \,\,\,\,\,\,\,}}\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,}}}},$  Then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle \frac{y}{{2y - x}}$
\choice $\displaystyle \frac{{ - y}}{{2y - x}}$
\choice $\displaystyle \frac{x}{{2y - x}}$
\choice $\displaystyle \frac{{ - x}}{{2y - x}}$
\end{choices}
\question If  $\displaystyle {x^m}\,\,{y^n} = {\left( {x + y} \right)^{m + n}}$  Then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle \frac{x}{y}$
\choice $\displaystyle \frac{y}{x}$
\choice $\displaystyle - \frac{y}{x}$
\choice $\displaystyle - \frac{x}{y}$
\end{choices}
\question If $\displaystyle y = \log \left[ {{a^x}\,\,{{\sin }^3}\,x\,\cos \,\,x} \right],$  Then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle 1 + 3\,\cot x - \tan \,\,x$
\choice $\displaystyle \log \,\,a + 3\,\cot x - \tan \,\,x$
\choice $\displaystyle \log \,\,a - 3\,\cot x - \tan \,\,x$
\choice $\displaystyle \log \,\,a + 3\,\cot x - \cot \,\,x$
\end{choices}
\question If $\displaystyle y = {e^{\log \,\left( {\log \,\,x} \right)}},$  Then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle \frac{1}{{x\,\,\log \,\,a}}$
\choice 1
\choice $\displaystyle \frac{1}{x}$
\choice $\displaystyle \frac{{\log \,\,a}}{x}$
\end{choices}
\question If $\displaystyle y = {\tan ^{ - 1}}\left( {\frac{{2x}}{{1 + 15{x^2}}}} \right),$  Then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle \frac{5}{{1 + 25{x^2}}} - \frac{3}{{1 + 9{x^2}}}$
\choice $\displaystyle \frac{1}{{1 + 25{x^2}}} - \frac{1}{{1 + 9{x^2}}}$
\choice $\displaystyle \frac{2}{{1 + {{\left( {\sqrt {15} x} \right)}^2}}}$
\choice $\displaystyle \frac{3}{{1 + {{\left( {\sqrt {15} x} \right)}^2}}}$
\end{choices}
\question If $\displaystyle y = \sin \,\,{\left( {ax + b} \right)^3},$  Then $\displaystyle \frac{{dy}}{{dx}} =$
\begin{choices} \choice $\displaystyle {\text{3}}\,{\text{sin}}\,\,{\left( {ax + b} \right)^2}$
\choice $\displaystyle {\text{3}}\,{\text{sin}}\,\,{\left( {ax + b} \right)^2}\,\cos \,{\left( {ax + b} \right)^2}$
\choice $\displaystyle 3a{\left( {ax + b} \right)^2}\,\cos {\left( {ax + b} \right)^3}$
\choice $\displaystyle - 3\sin {\left( {ax + b} \right)^2}\,\cos \left( {ax + b} \right)$
\end{choices}
\end{questions}
\end{multicols}
\end{document}

-

I want to start new question on next column, if [fewer] than three lines are remaining in a given column.

AFAICT, there's no simple way to "hack" ("modify", if you prefer...) the code of the exam document class to tell it to keep lines generated by \question and lines generated by an associated group of choice possibilities together. Hence, I don't think it's possible to automate the task of preventing splits across columns (or pages) of such material.

You mention that you want to use the multicols environment, provided by the multicol package, to typeset the questions in two-column mode. The multicol package performs some pretty fancy work to balance materials across columns on a page that's not entirely full. A downside of this work -- from your perspective at least -- is that directives such as \needspace{2cm} (or whatever amount of vertical space is needed) won't work properly because their efforts will be undone by the work of multicol.

Fortunately, there's an alternative and reasonably simple approach: Instead of loading the multicol package, you could use the document class option twocolumn and insert \newpage instructions whenever a column break is needed. (In twocolumn mode, \newpage generates a column break, not a page break.) For your setup, it turns out that \newpage instructions are needed before questions 5, 8, and 12.

The code below also applies a few additional edits to your posting. (i) Instead of $\displaystyle\frac$, I'd say it's easier to write $\dfrac$. (ii) The expression \dfrac{dy}{dx} occurs sufficiently often to warrant creating a dedicated macro, such as \newcommand{\dyx}{\dfrac{dy}{dx}}. (iii) Definitely use the macro \cfrac for typesetting continued fractions. (iv) You seem to use way more pairs of curly braces than is necessary. For instance, I don't see any advantage in writing, say, \frac{x}{{2y - x}}; \frac{x}{2y - x} is, I'd argue, simpler to read and debug. (v) Do use whitespace more generously, including between the end of a choices environment and the next \question directive. Providing these additional blank lines in the input file won't create unwanted extra lines in the output. (vi) Don't use \left( and \right) indiscriminately when ( and ) do just as well (or even better).

\documentclass[addpoints,answers,12pt,twocolumn]{exam}
\usepackage{amsmath}
\newcommand{\dyx}{\dfrac{dy}{dx}} % a frequently-used term...
\begin{document}
\begin{questions}
\question
If $y = \sin^{-1}\left( \dfrac{2\tan x}{1 - \tan^2 x} \right)$, then $\dyx=$
\begin{choices}
\choice $\dfrac{2\sec^2 2x}{\sqrt {1 - \tan^2 2x} }$
\choice 2
\choice $\dfrac{ \sec^2 2x}{\sqrt {1 - \tan^2 x} }$
\choice 1
\end{choices}

\question If $x e^y = y^2$, then $\dyx=$
\begin{choices}
\choice $\dfrac{-y}{x(2 - y)}$
\choice $\dfrac{ y}{x(2 - y)}$
\choice $\dfrac{ x}{y(2 - x)}$
\choice $\dfrac{-x}{y(2 - y)}$
\end{choices}

\question If $y = \log_{10}(\log x)$, then $\dyx=$
\begin{choices}
\choice $\dfrac{ 1}{x\log x \log 10}$
\choice $\dfrac{\log x}{x\log 10}$
\choice $\dfrac{ -1}{x\log x \log 10}$
\choice $-\dfrac{\log x}{x\log 10}$
\end{choices}

\question If $x y^2 = 1$, then $\dyx=$
\begin{choices}
\choice $\dfrac{y^2}{2}$
\choice $\dfrac{-y}{2x}$
\choice $2 y^2$
\choice $-2 y^2$
\end{choices}

\newpage  %% force colunn break
\question If $x + \tan^{-1}x = y + \tan^{-1}y$, then $\dyx=$
\begin{choices}
\choice $\dfrac{y^2 (x^2 + 2)}{x^2(y^2 + 2)}$
\choice $\dfrac{x^2 + 2}{y^2 + 2}$
\choice $\dfrac{(x^2+2)(y^2+1)}{(x^2+1)(y^2+2)}$
\choice $\dfrac{y^2+2}{x^2+2}$
\end{choices}

\question If $y = \log(\sin x) + \sin(\log x)$, then $\dyx=$
\begin{choices}
\choice $\cot x + \dfrac{\cos(\log x)}{x}$
\choice $\dfrac{1}{\sin x} + \sin\left( \dfrac{1}{x} \right)$
\choice $\cot x - \dfrac{\cos(\log x)}{x}$
\choice $\cot x - x(\log x)$
\end{choices}

\question If $x = t + \dfrac{1}{t}$,
$y = t - \dfrac{1}{t}$,  then
$\dfrac{d^2 y}{d x^2}$  at $t = 2$  is
\begin{choices}
\choice $-\dfrac{32}{27}$
\choice $\dfrac{32}{27}$
\choice $\dfrac{16}{27}$
\choice $\dfrac{8}{27}$
\end{choices}

\newpage  %% force column break
\question If  $y = x + \cfrac{1}{x + \cfrac{1}{x + \cfrac{1}{x + \cdots }}}$, then $\dyx=$
\begin{choices}
\choice $\dfrac{y}{2y - x}$
\choice $\dfrac{ - y}{2y - x}$
\choice $\dfrac{x}{2y - x}$
\choice $\dfrac{ - x}{2y - x}$
\end{choices}

\question If  $x^m y^n = (x + y)^{m + n}$, then $\dyx=$
\begin{choices}
\choice $\dfrac{x}{y}$
\choice $\dfrac{y}{x}$
\choice $- \dfrac{y}{x}$
\choice $- \dfrac{x}{y}$
\end{choices}

\question If $y = \log \left[a^x \sin^3 x \cos x \right]$, then $\dyx=$
\begin{choices}
\choice $1 + 3\cot x - \tan x$
\choice $\log a + 3\cot x - \tan x$
\choice $\log a - 3\cot x - \tan x$
\choice $\log a + 3\cot x - \cot x$
\end{choices}

\question If $y = e^{\log(\log x)}$, then $\dyx=$
\begin{choices}
\choice $\dfrac{1}{x\log a}$
\choice 1
\choice $\dfrac{1}{x}$
\choice $\dfrac{\log a}{x}$
\end{choices}

\newpage  %% force a column break
\question If $y = \tan^{-1} \left( \dfrac{2x}{1 + 15 x^2} \right)$, then $\dyx=$
\begin{choices}
\choice $\dfrac{5}{1 + 25 x^2} - \dfrac{3}{1 + 9 x^2}$
\choice $\dfrac{1}{1 + 25 x^2} - \dfrac{1}{1 + 9 x^2}$
\choice $\dfrac{2}{1 + \left(\sqrt{15}\,x \right)^2}$
\choice $\dfrac{3}{1 + \left(\sqrt{15}\,x \right)^2}$
\end{choices}

\question If $y = \sin(ax + b)^3$, then $\dyx=$
\begin{choices}
\choice $3\sin(ax + b)^2$
\choice $3\sin(ax + b)^2 \cos(ax + b)^2$
\choice $3a(ax + b)^2 \cos(ax + b)^3$
\choice $-3\sin(ax + b)^2 \cos(ax + b)$
\end{choices}

\end{questions}
\end{document}

-
Thanks Mico for your reply. As suggested, I added needspace directive but it's adding unwanted spaced. – Pawan Mude May 26 '13 at 17:18
I've copied few more exam questions. After adding these questions, If you look at Question#4; it should start on next column. Currently Only one line of Question#4 is appearing in column one. – Pawan Mude May 26 '13 at 17:22
@pawanmude - thanks for providing some extra material. Please see my revised answer for a new strategy, using the document class option twocolumn (instead of the multicols environment) and the directive \newpage to achieve the desired breaks between columns. – Mico May 27 '13 at 2:28
Thanks a lot Sir, This is one of the most valuable feedback I've received. Sir, I'm generating .tex file automatically. So overtime number of questions will be different. To add \newpage I need to find some pattern (Ex: number of characters, x & y coordinates of the page to logically add new column). Thanks again for your help. I’ll work towards finding required pattern. – Pawan Mude May 27 '13 at 8:01
@pawanmude - You're welcome! I fully understand the desirability of having an automated way of forcing column breaks. What makes the automation task so difficult is that the macro \question does not seem to be "patchable" via the tools of the etoolbox and xpatch packages. (I tried to modify the \question macro so that the command \Needspace{5cm} would be executed every time the \question macro is encountered. Unfortunately, these attempts didn't lead to success.) Do ask this: How much extra time will you really have to spend inserting a few \newpage instructions here and there? – Mico May 27 '13 at 13:49

There is a "hack" or rather a method that can be used: help TeX's selection along by providing a sequence of vertical spaces and penalties that favor breaking the column at the penalty:

\newcommand\maybenextcolgeneric[2]{\par\vspace{0pt plus #1\baselineskip}%
\penalty #2
\vspace{0pt plus -#1\baselineskip}}


This will generate a space that can grow without much badness to #1 number of lines, then follows a penalty of #2 how desirable it is to break here and then follows another space that undoes the earlier space (so we are back vertically where we started of).

So if you apply the above, e.g., like this

\newcommand\maybenextcol{\maybenextcolgeneric{1}{-500}}


and you add \maybenextcol before an \question then this will tell TeX that one line of space at the bottom of a column is better than breaking the column after the first line of a question and as a result you get

\newcommand\myquestion{\maybenextcol\question}

and then you this instead of \question thoughout.