17

following scenario: I want to automatically generate a newsletter with some small articles, which is working fine. (I am generating LaTeX-code with data from a small database). But now I want to include some images to fill the space left (I want to have entirely filled pages), but I don't want to get more pages than needed for the articles.

So: is there a way to check with LaTeX, if there is space left on the last page and how much?

I hope you can understand what I mean.

Forty2
  • 173

2 Answers2

14

I think you could use \pagetotal for this. Subtract it from \textheight to get the amount of space remaining.

\documentclass[10pt]{article}
\newdimen\spaceleft
\spaceleft=\textheight
\usepackage{lipsum}
\begin{document}
\lipsum[1-3]

\advance\spaceleft by -\pagetotal \typeout{Space on last page:} \showthe\spaceleft \end{document}

The result of this is:

Space on last page:

204.0pt.

Ian Thompson
  • 43,767
  • 2
    Instead of multiplying something by -1 twice you can simply write \advance\spaceleft by-\pagetotal. – wipet Jun 05 '17 at 08:40
  • Any idea why if I replace the last two lines with \rule{1mm}{\spaceleft}, the rule is pushed on the next page? There should be enough space for it right? – tobiasBora Sep 30 '21 at 15:49
  • @tobiasBora --- One problem was a missing paragraph break before \advance. That meant the last paragraph wasn't included in page total. With that fixed, you still need to remove 3 or 4pt from the rule height to prevent a page break. I'm not sure why this happens exactly. I'll give it some thought, but you will probably get an answer much more quickly if you post a new question about it (because then someone like egreg or David Carlisle will see it). – Ian Thompson Oct 01 '21 at 14:43
8

A nicer way to realise Ian Thompsons answer is to simply use \dimexpr rather than multiplying by -1 and adding the lengths:

\documentclass{article}
\usepackage{lipsum}

\begin{document}
\lipsum[1-3]

\newdimen\spaceleft
\spaceleft=\dimexpr\textheight-\pagetotal\relax

Remaining space (above this line):
\the\spaceleft
\end{document}
henrikl
  • 195