# Measure the remainder of a page if it has a title and inserting that into a dynamic command

Okay, so I'm currently writing a report (in the article class) which has a section full of chemical spectra. I figured that two spectra per page is a good number, so I write

\RequirePackage{calc}
\newlength{\halfpage}
\setlength{\halfpage}{\textheight/2}
\newcommand{\twoandabitperpage}{height=\halfpage}
\newcommand\mygraphicspage[2][]{\centering\expandafter\includegraphics\expandafter[#1]{#2}}


This means that I can say \mygraphicspage[\twoandabitperpage]{spectrum_1.pdg} and \mygraphicspage[\twoandabitperpage]{spectrum_2.pdg}, and this will give me a nice couple of spectra on one page. (Note that I need the \expandafters or else my code will break. I don't fully understand it, but it's there and I guess it works).

''Great!'' I thought to myself. In practice: not great. My spectra section is as follows:

\part{Spectras!}
\section{Spectrum One}
\mygraphicspage[\twoandabitperpage]{spectrum_1.pdf}
\section{Spectrum Two}
\mygraphicspage[\twoandabitperpage]{spectrum_2.pdf}


Therefore, taking half of the page and making it my image height does not always work when there are sections and parts at play (for their titles make up more than is ideal of the page).

And so I went to Google: I found this

\newlength\remainder
\newcommand\measurepage{\dimexpr\pagegoal-\pagetotal-\baselineskip\relax}
\remainder=\measurepage\relax\the\remainder


So to my understanding (and I may be wrong, as I don't understand TeX very well [I couldn't figure out how to make the \mygraphicspage command...]), what this does is it measures the rest of the page, probably through some black magic—after, of course, defining that the remaining length of the page shall be called the remainder. Then, it prints it? It that what that last line is for?

Anywho, I want to able to define

\newlength{\almosthalfpage}
\setlength{\almosthalfpage}{\textheight-\remainder/2}


Where \remainder is defined by the page in question. This page will be specified when and only when I use the length \almosthalfpage.

For example, say that my textheight=40em, and my part title makes up 4em, and I have two section titles making up 2 em each. Then, the remainder of my page is 32em. Therefore, my individual image heights should be 16em. This remainder of 32em is defined by \remainder, but only when I specify the image height (which is a function of \remainder) for that particular page.

I am really sorry if this makes no sense at all... Any help would be appreciated! And to a lesser degree of importance, I would appreciate anyone commenting on better ways to convey the code I currently have (i.e., should I be using \def instead of \newcommand, and \let? How do I use those commands? etc. etc. ...).

Thank you!

• This seems a bit as if you're using a sledgehammer to crack a nut. Is it correct to say thaty basically you have a document with images and want to make sure that there are never more than two images on the same page? – sheß Apr 1 at 11:32
• @sheß, not necessarily. I want to ensure that there are exactly two images per page, and I want them to fit nicely. I may very well be using a sledgehammer to crack a nut, as it were—; as I say, I am not very good with TeX at the minute. I have no doubt there are better ways to do this. – Christopher Tatlock Apr 1 at 11:44
• Have you had a look at using subcaption or subfig? Alternatively you could use a \rule to make your figures visually unchanged but let them take up the space of half a page? Lastly, if you're willing to give LaTeX a bit more freedom in deciding what to put where, you could follow this post: How can I adjust the number of figures latex will display on a page and also use \usepackage{placeins} and \FloatBarrier to make sure everything stays in the right sections – sheß Apr 1 at 11:51