# Is it possible to define an environment that might not be displayed?

I am currently improving lecture notes for technical computer science. The lecture notes are printed. Chapters in those notes begin on the right side, so it might happen that one page is completely blank. I don't want this waste of space.

My idea was to fill this space with exercises. I would add exercises that could fill two pages with something like enumerate and I would like that LaTeX "deletes" exercises that would go on the right side.

Is this possible?

edit:

As the question seems not be clear enough, I've made an image:

I would like to be able to write something like:

\begin{onlyInsertIfFreeSpace}
\item What is the answer to life the universe and everything?
\item Is it possible to define such an environment in LaTeX?
\item This environment should insert as many items as possible
\item So I might have 20 items, but the document only displays 6 at the
end, because 6 had enough space
\end{onlyInsertIfFreeSpace}

• Does this help you Conditional typesetting / build Aug 10 '13 at 7:14
• @texenthusiast: I don't think so. I have multiple chapters and I don't want to include a switch manually. Also, it should be possible that e.g. the first 5 of 12 items of the list of exercises get inserted (as much as fit). I don't know how to get this with a simple macro. Aug 10 '13 at 8:02
• @moose See the edited answer (again). Aug 11 '13 at 9:12

This is my attempt.

\usepackage{calc}

\newlength{\alength}
\newlength{\blength}
\newif\ifprinted\printedtrue

\newcommand{\myitem}[1]{%
\setlength{\alength}{\textheight-\the\pagetotal-10pt}
\setlength{\blength}{\totalheightof{\parbox{\linewidth}{#1}}}
\ifprinted
\ifodd\value{page}
\ifnum\pagetotal>0
\ifnum\alength<\blength
\clearpage
\item #1
\printedtrue
\else
\item #1
\printedtrue
\fi
\fi
\else
\printedfalse
\ifnum\alength>\blength
\item #1
\printedtrue
\fi
\fi
\fi
}

\newenvironment{myenumerate}{\begin{enumerate}\item[]}{\end{enumerate}}


The new defined command \myitem is to be used instead of \item inside the myenumerate environment in this way:

\myitem{What is the answer to life the universe and everything?}


\item What is the answer to life the universe and everything?


The new environment myenumerate is defined to avoid errors when no \myitems are printed (it contains a "phantom" item \item[])

\myitems are printed only if they are in a non-empty odd page or if there is enough space in an even page.

The following example explains it a bit better:

\documentclass[openright]{book}
\usepackage{lipsum}
\usepackage{calc}

\newlength{\alength}
\newlength{\blength}
\newif\ifprinted\printedtrue

\newcommand{\myitem}[1]{%
\setlength{\alength}{\textheight-\the\pagetotal-10pt}
\setlength{\blength}{\totalheightof{\parbox{\linewidth}{#1}}}
\ifprinted
\ifodd\value{page}
\ifnum\pagetotal>0
\ifnum\alength<\blength
\clearpage
\item #1
\printedtrue
\else
\item #1
\printedtrue
\fi
\fi
\else
\printedfalse
\ifnum\alength>\blength
\item #1
\printedtrue
\fi
\fi
\fi
}

\newenvironment{myenumerate}{\begin{enumerate}\item[]}{\end{enumerate}}

\begin{document}

\chapter{Introduction}
\lipsum[1-6]

\begin{myenumerate}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\myitem{What is the answer to life the universe and everything?}
\end{myenumerate}

\chapter{Some more stuff}
\lipsum

\end{document}


As you can see you have 15 \myitems but the 15th is not printed because there is not enough space in the page.

Playing a little with \lipsum you can see that it works fine in even and odd pages.

The ifprinted has been added to avoid printing subsequent \myitems if the previous has not been printed.

• For a full solution, you can use needspace package for convenience. Aug 10 '13 at 10:27

A quick and naive solution:

% if the chapter ends at odd page (right side)
\ifodd\value{page}
Exercises go here, in one page.
\fi

• This does not answer my question. I've updated it to make it more clear what I would like to get. Aug 10 '13 at 8:14
• @moose: I think I understand your question well. The code here is not elaborate, but shows at least a partial solution. A full solution (select only a subset of the exercises to fit the page) may be much more complex than this. Aug 10 '13 at 8:26
• Please look at the image above. The exercises can be on odd and on even pages. Aug 10 '13 at 8:32