# How do I re-use the book's text for different document formats?

Any ideas how I could re-use the book's text that I have written (it's in chapter1.tex, chapter2.tex, etc.) in various document formats (a4, letter, mobile device format, etc). The problem I am facing is that I have placed various \newpages in the chapters so they looked ok for a4 format. But now when I tried to compile the book for letter those \newpages are in the wrong places, so I have to have a different copy of text with \newpages in the right places. What's the general strategy for re-using the text for various formats?

The reason why I am using \newpage throughout my book is that \sections sometimes appear at the bottom of the page, and I want to force them into the new page, so I use \newpage just before that \section that was at the bottom of current page.

Here is a screenshot example:

See how 4.12 is at very bottom and the code example is cut in half? That's why I insert these \newpages to force it on a new page so it looked ok!

I am using memoir document class and the code boxes are done via \begin{lstlisting}...\end{lstlisting}.

-
@Peteris I doubt that using \newline makes something better; I dare say it makes it worse. Do you mean \linebreak, of course, don't you? ;-) –  egreg Jun 3 '11 at 17:49
One of the strategies: don't put commands to force manual formatting. –  xport Jun 3 '11 at 17:52
Sorry I mean \newpage. Oh my. I just corrected my question. –  Peteris Krumins Jun 3 '11 at 17:56
Page 19 of l2short clearly states that \newline and \linebreak should only be used in "special cases". If you haven't read this introduction, make up for it. –  lockstep Jun 3 '11 at 17:57
Yeah, this \newpage business is wrong. I just spent 2 hours putting them in the right places throughout the document. And then I rewrote one paragraph and all the \newpages in that chapter broke. I have to figure out how to get rid of them, so I'll post a new question. Once I get rid of \newpages, I can easily switch document formats. –  Peteris Krumins Jun 3 '11 at 18:26

Even though you seem to have abandoned this question, I am going to answer it. The reason is this: No matter how generic you make your code, you will eventually have to input manual layout tweaks here and there. This should be done sparingly, and as a last resort, but in any manuscript of a given size and complexity, I think it is inevitable that you will need to do so. Now the problem is one of making your tweaks appear conditionally, depending on paper size and possibly other layout parameters. Here is one way to do this:

\documentclass{minimal}

\makeatletter
\def\setlayout#1{\@setlayout#1,\relax,}
\def\@setlayout#1,{\ifx\relax#1\else
\expandafter\def\csname layout@#1\endcsname{}%
\expandafter\@setlayout\fi}
\def\layout #1#{\@layout #1,\relax,}
\def\@layout#1,{\ifx\relax#1\expandafter\@secondoftwo\else
\ifcsname layout@#1\endcsname
\expandafter\expandafter\expandafter\layout@yes\else
\expandafter\expandafter\expandafter\@layout
\fi
\fi}
\def\layout@yes#1#{\@firstoftwo}
\makeatother

% demo
\setlayout{one,two}
\begin{document}
\layout one,four{\message{[the first]}}{\message{[not the first]}}
\layout five,two{\message{[the second]}}{\message{[not the second]}}
\layout six,three{\message{[the third]}}{\message{[not the third]}}
\end{document}


In a real document, you might set, say options a5 and booklet using

\setlayout{a5,booklet}


and then, whenever a particular layout tweak (say, a \newpage) has to be done only for the a5 case or the screen case, you write

\layout a5,screen{\newpage}{}


The second parameter is there in case you need to do something else in the other cases.

-
This was very useful, thanks! –  Peteris Krumins Jun 3 '11 at 22:23