7

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:

enter image description here

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}
3
  • Does this help you Conditional typesetting / build Aug 10, 2013 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, 2013 at 8:02
  • @moose See the edited answer (again). Aug 11, 2013 at 9:12

2 Answers 2

7

This is my attempt.

Add this to your preamble:

\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?}

instead of

\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.

enter image description here

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.

1
  • For a full solution, you can use needspace package for convenience.
    – Leo Liu
    Aug 10, 2013 at 10:27
2

A quick and naive solution:

% if the chapter ends at odd page (right side)
\ifodd\value{page}
   Exercises go here, in one page.
\fi
3
  • This does not answer my question. I've updated it to make it more clear what I would like to get. Aug 10, 2013 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.
    – Leo Liu
    Aug 10, 2013 at 8:26
  • Please look at the image above. The exercises can be on odd and on even pages. Aug 10, 2013 at 8:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .