# Where does whitespace between two consecutive lines come from?

I'm trying to get a "contact sheet" where identically sized boxes are laid out next to each other without any whitespace. As my goal is to use this in a generated document, and as the number of items is unknown in advance, I'd like to avoid tabular-like approaches and rely on native line breaking and page breaking machinery. This is also an experiment to learn more about the internals of tex/latex, so along the way I also played a lot with keywords like badness, emergencystretch, sloppy etc. But in the end, my quest can be summed up in the code below.

My first try below obviously produces some "interword" whitespace between boxes, so it is not desirable.

In my second try (page 2) I explicitely remove this whitespace but this also removes line breaks. I don't really understand what's going on.

In my third try (page 3) I explicitely tell (la)tex that it's ok to break lines, and I get the result I want w.r.t. horizontal whitespace. But still, there is some amount of vertical whitespace between lines, and this whitespace occupies room. As a result, the blue box ends up on page four, when I want it to be on on page three.

This is my question: how do I get rid of that small vertical whitespace between two consecutive lines of text ?

\documentclass[a4paper]{article}

\usepackage[
text={15cm,21cm},
showframe,
]
{geometry}

\usepackage{xcolor}

\setlength{\parindent}{0pt}

\begin{document}

% page 1
\textcolor{red}{\rule{5cm}{7cm}}
\textcolor{green}{\rule{5cm}{7cm}}
\textcolor{red}{\rule{5cm}{7cm}}
\textcolor{green}{\rule{5cm}{7cm}}
\textcolor{red}{\rule{5cm}{7cm}}
\textcolor{green}{\rule{5cm}{7cm}}

\newpage

% page 2

\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%

\newpage

% page 3

\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{blue}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%

\end{document}

-

## Example 1

There are spaces: end-of-lines are like spaces, except when there are two in a row (actually this is a white lie, but for this case it's sufficient).

## Example 2

There's no break point, so TeX can't split the boxes.

## Example 3

\linebreak[0] is equivalent to \penalty0, which introduces a feasible break point.

## Example 4

Each line is higher than the space allowed for the baselineskip. Roughly, if the depth of a line plus the height of the following one plus a tolerance (called \lineskiplimit) exceeds the \baselineskip, TeX inserts \lineskip glue between those lines. Default is 1pt.

Here's a way to avoid that additional space:

\begingroup\lineskip=0pt
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{blue}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\par
\endgroup


\offinterlineskip is a different method: it inhibits interline glue at all. In this particular case they are equivalent.

-
thanks a lot for this answer. not only does it solve the problem at hand, but it also teaches me cool stuff along the way :-) –  Gyom Apr 12 '12 at 7:51

You can use \offinterlineskip to prevent interline glue.

Also, you can use % to remove spurious blank spaces (a carriage return will be taken as a white space):

\documentclass[a4paper]{article}

\usepackage[
text={15cm,21cm},
showframe,
]
{geometry}

\usepackage{xcolor}

\setlength{\parindent}{0pt}

\begin{document}

\offinterlineskip
% page 1
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}
\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}

\newpage

% page 2

\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}
\textcolor{red}{\rule{5cm}{7cm}}%

\newpage

% page 3

\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{blue}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%

\end{document}


Another "less drastic" option ti suppress interline glue is to use \nointerlineskip:

\documentclass[a4paper]{article}

\usepackage[
text={15cm,21cm},
showframe,
]
{geometry}

\usepackage{xcolor}

\setlength{\parindent}{0pt}

\begin{document}

% page 1
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}\par\nointerlineskip
\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}

\newpage

% page 2

\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}\par\nointerlineskip
\textcolor{red}{\rule{5cm}{7cm}}%

\newpage

% page 3

\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\par\nointerlineskip %
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\par\nointerlineskip %
\textcolor{blue}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}%

\end{document}

-