1

In a book class document (for some reason with article class the problem does not show up) I need to insert a section containing only images (an image gallery). Images are of various size and aspect ratio, but some code helps in sizing them automatically. Depending on images, the vertical space after section title may be too big. I guess the problem is that the space after the section title is stretched too much to avoid blank space at the end of the page. I tried without success to increment the vertical space before title and between “lines” (if I can call them so).

Here is a MWE. How can I increment the vertical space before title and between lines? I'm looking for a generic solution, not tailored on a certain combination of images.

Thanks in advance for any clue.

P.S. My actual document is compiled with XeLaTex, I hope this does not affect solution.

\documentclass{book}
\usepackage{mwe}
\usepackage{xfp}
% add to \includegraphics the option sqrtofarea that sizes image so that it has a given area, regardless of aspect ratio
\makeatletter
\define@key{Gin}{sqrtofarea}{%
    \def\Gin@req@sizes{%
      \edef\Gin@scalex{\fpeval{#1/sqrt(\Gin@nat@height*\Gin@nat@width)}}%
      \let\Gin@scaley\Gin@exclamation
      \Gin@req@height\Gin@scalex\Gin@nat@height
      \Gin@req@width\Gin@scalex\Gin@nat@width
      }%
  \@tempswatrue}
\makeatother

% command helping to insert images in a photo gallery
%usage: \imageofgallery{image file}{square root of image area}{margins of image}
\newcommand{\imageofgallery}[3]{%
\begingroup\setlength{\fboxrule}{0pt}\setlength{\fboxsep}{#3}\setbox0=\hbox{\fbox{\includegraphics[sqrtofarea=#2]{#1}}}\parbox{\wd0}{\box0}\endgroup\hspace{1pt}%
}

\begin{document}
\section{Image gallery}
\begin{center}
\newcommand{\imgsize}{12em} % images will be sized so they have an area of 12x12 sq-em, regardless of aspect ratio
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image-1x1}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\end{center}
\end{document}

enter image description here

  • 1
    book defaults to \flushbottom you probably want \raggedbottom so the extra space comes at the end of the page. – David Carlisle Mar 7 at 22:09
  • I guess I need \flushbottom for the rest of the book, which is an ordinary book containing mainly text. – mmj Mar 7 at 22:16
  • 1
    you can force that page to finish short by using `\clearpage where you want the break – David Carlisle Mar 7 at 22:18
  • 1
    just put\raggedbottom before that \section and go back to \flushbottom after all the images – David Carlisle Mar 7 at 22:28
  • 2
    if you want flush bottom except for one section where you don't, then that isn't a "workaround" it's the intended markup. – David Carlisle Mar 7 at 22:42
2

The main problem with the given code is that the bottom picture doesn't fit and is moved to the next page.

Issue \raggedbottom for the duration of the center environment, with a \clearpage at the end.

\documentclass{book}
\usepackage{mwe}
\usepackage{xfp}
% add to \includegraphics the option sqrtofarea that sizes image
% so that it has a given area, regardless of aspect ratio
\makeatletter
\define@key{Gin}{sqrtofarea}{%
    \def\Gin@req@sizes{%
      \edef\Gin@scalex{\fpeval{#1/sqrt(\Gin@nat@height*\Gin@nat@width)}}%
      \let\Gin@scaley\Gin@exclamation
      \Gin@req@height\Gin@scalex\Gin@nat@height
      \Gin@req@width\Gin@scalex\Gin@nat@width
      }%
  \@tempswatrue}
\makeatother

% command helping to insert images in a photo gallery
%usage: \imageofgallery{image file}{square root of image area}{margins of image}
\newcommand{\imageofgallery}[3]{%
  \begingroup
  \setlength{\fboxrule}{0pt}%
  \setlength{\fboxsep}{#3}%
  \sbox0{\fbox{\includegraphics[sqrtofarea=#2]{#1}}}%
  \parbox{\wd0}{\box0}%
  \endgroup
  \linebreak[0]%
}

\begin{document}

\section{Image gallery}
\begin{center}
\raggedbottom % this is local to center
\newcommand{\imgsize}{12em} % images will be sized so they have an area of 12x12 sq-em, regardless of aspect ratio
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image-1x1}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image-1x1}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image-1x1}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image-1x1}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image-1x1}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\imageofgallery{example-image-1x1}{\imgsize}{1ex}
\imageofgallery{example-image}{\imgsize}{1ex}
\imageofgallery{example-image-16x9}{\imgsize}{1ex}
\imageofgallery{example-image-10x16}{\imgsize}{1ex}
\clearpage
\end{center}

\end{document}

I made a few small changes to the code: \setbox0=\hbox should be \sbox0; instead of \hspace{1pt} use \linebreak[0], so no additional space is inserted, but a line break after each image is allowed nonetheless.

enter image description here

| improve this answer | |
  • Thanks for the improvements to the auxiliary code. I'm accepting the answer, unless a way to "increment the vertical space before title and between lines" show up. – mmj Mar 8 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.