How do I adjust stretching between groups of images?

197 Views Asked by At

I am trying to dynamically generate a large PDF (1000+ pages) with lots of entries—each consisting of a caption and one or more images. I want the images for the same caption to be grouped, i.e. if there is a caption and two images, I want both to stay on the same page (if they can fit), which I have achieved (without testing what happens with large images.

However, my problem is that interline glue is very odd here. I would expect the space between grouped images to be smaller than between groups of images, which I tried doing by controlling \vspace between them and manipulating stretch factor (so that inter-entry spaces stretch more than inter-image spaces), and different modifications to \vbox layout, but it produced poor results. I can either make them grouped, or align them properly. Note: this code is generated dynamically, so I cannot manually adjust it.

Here is an example of good page: Good page layout

Here are examples of pages with layout problems: Bad layout 1: Bad layout 2 Bad layout 3

And here is what I am trying to avoid using \vbox and nobreak=true: Bad page split

And here's the a fragment of code:

\documentclass[12pt, a4paper, twoside]{article}

\usepackage[margin=2.5cm, bindingoffset=1cm, headheight=15pt]{geometry}

\usepackage{fontspec}
\newfontfamily{\normaltitle}{Exo 2}
\newfontfamily{\boldtitle}[UprightFont={* Bold}]{Exo 2}
\newfontfamily{\footerheaderfont}{Exo 2}

\usepackage[dvipsnames]{xcolor}
\definecolor{CustomGray}{HTML}{909090}

\usepackage{mdframed}

\usepackage{graphicx}
\graphicspath{ {D:/imgdir} }

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\footerheaderfont\small{Author}}
\fancyhead[LO,RE]{\footerheaderfont\small{Title}}


\fancyfoot[C]{}
\fancyfoot[LE]{\footerheaderfont\small\thepage}
\fancyfoot[RO]{\footerheaderfont\small\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}


\usepackage{titlesec}
\titlelabel{\thetitle.\quad}
\titleformat*{\section}{\fontsize{24}{36}\boldtitle}
\titlespacing*{\section}{0pt}{0pt}{12pt}

\setlength{\parindent}{0pt}
\setmainfont[Ligatures=TeX]{Times New Roman}

\newcommand\textbox[1]{%
    \parbox[t][][t]{\textwidth}{#1}%
}

\newcommand\dictentry[3]{
    \large
    \hangindent=0cm
    \noindent
    \textcolor{CustomGray}{#1}\textbf{#2} \hfill \small{#3}
}

\tolerance=8000



\begin{document}
    \begin{titlepage}
        \begin{center}
            \vspace*{3cm}
            
            \fontsize{36}{40}\selectfont
            
            \boldtitle{Aneks F:}
            
            \vspace{0.5cm}
            
            \normaltitle{Fotokorpus haseł \emph{SW}}
            
            \vfill
        \end{center}
    \end{titlepage}

    \setcounter{page}{202}



    \vbox{%
        \parbox{15cm}{
            \dictentry{†}
            {Fałbuła}
            {t. I (A-G), lit. F, str. 715, kol. R, ak. 32}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{"page-0715, col-2, par-32 fin.png"}
        \end{mdframed}
    }

        
    \vspace{2em}%


    \vbox{%
        \parbox{15cm}{
            \dictentry{}
            {Fałd}
            {t. I (A-G), lit. F, str. 715, kol. R, ak. 33 – str. 716, kol. L, ak. 01}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{"page-0715, col-2, par-33 fin.png"}
        \end{mdframed}
    }

    \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
        \includegraphics[width=\linewidth]{"page-0716, col-1, par-01 fin.png"}
        \unskip
    \end{mdframed}
            
            
    \vspace{2em}%


    \vbox{%
        \parbox{15cm}{
            \dictentry{}
            {Fałszywy}
            {t. I (A-G), lit. F, str. 717, kol. L, ak. 18 – str. 717, kol. R, ak. 01}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{"page-0717, col-1, par-18 fin.png"}
        \end{mdframed}
    }
            
    \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
        \includegraphics[width=\linewidth]{"page-0717, col-2, par-01 fin.png"}
        \unskip
    \end{mdframed}
                
                    
    \vspace{2em}%


    \vbox{%
        \parbox{15cm}{
            \dictentry{}
            {Fama}
            {t. I (A-G), lit. F, str. 717, kol. R, ak. 02}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{"page-0717, col-2, par-02 fin.png"}
        \end{mdframed}
    }
        
    
    \vspace{2em}%


    \vbox{%
    \parbox{15cm}{
            \dictentry{}
            {Fenacetyna}
            {t. I (A-G), lit. F, str. 731, kol. L, ak. 24}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{"page-0731, col-1, par-24 fin.png"}
        \end{mdframed}
    }

        
    \vspace{2em}%


    \vbox{%
        \parbox{15cm}{
            \dictentry{}
            {Fenakistoskop}
            {t. I (A-G), lit. F, str. 731, kol. L, ak. 25}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{"page-0731, col-1, par-25 fin.png"}
        \end{mdframed}
    }

        
    \vspace{2em}%
    

    \vbox{%
        \parbox{15cm}{
            \dictentry{}
            {Fenantren}
            {t. I (A-G), lit. F, str. 731, kol. L, ak. 26}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{"page-0731, col-1, par-26 fin.png"}
        \end{mdframed}
        }

        
        \vspace{2em}%


        \vbox{%
            \parbox{15cm}{
                \dictentry{†}
                {Fendel}
                {t. I (A-G), lit. F, str. 731, kol. L, ak. 27 – str. 731, kol. R, ak. 01}
            }
            \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
                \includegraphics[width=\linewidth]{"page-0731, col-1, par-27 fin.png"}
            \end{mdframed}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{"page-0731, col-2, par-01 fin.png"}
            \unskip
        \end{mdframed}
                    

\end{document}

Edit: `\raggebottom' doesn't produce the desired effect – it seems to stretch the in-group vertical spaces more than inter-group vertical spaces, plus the problem from page 285 still persists: Raggedbottom

1

There are 1 best solutions below

2
samcarter_is_at_topanswers.xyz On BEST ANSWER

You can use \raggedbottom to avoid the content of the page to be stretched to fill the entire height:

\documentclass[12pt, a4paper, twoside]{article}

\usepackage[margin=2.5cm, bindingoffset=1cm, headheight=15pt]{geometry}

%\usepackage{fontspec}
%\newfontfamily{\normaltitle}{Exo 2}
%\newfontfamily{\boldtitle}[UprightFont={* Bold}]{Exo 2}
%\newfontfamily{\footerheaderfont}{Exo 2}

\newcommand{\boldtitle}{}
\newcommand{\normaltitle}{}
\newcommand{\footerheaderfont}{}

\usepackage[dvipsnames]{xcolor}
\definecolor{CustomGray}{HTML}{909090}

\usepackage{mdframed}

\usepackage{graphicx}
\graphicspath{ {D:/imgdir} }

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\footerheaderfont\small{Author}}
\fancyhead[LO,RE]{\footerheaderfont\small{Title}}


\fancyfoot[C]{}
\fancyfoot[LE]{\footerheaderfont\small\thepage}
\fancyfoot[RO]{\footerheaderfont\small\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}


\usepackage{titlesec}
\titlelabel{\thetitle.\quad}
\titleformat*{\section}{\fontsize{24}{36}\boldtitle}
\titlespacing*{\section}{0pt}{0pt}{12pt}

\setlength{\parindent}{0pt}
%\setmainfont[Ligatures=TeX]{Times New Roman}

\newcommand\textbox[1]{%
    \parbox[t][][t]{\textwidth}{#1}%
}

\newcommand\dictentry[3]{
    \large
    \hangindent=0cm
    \noindent
    \textcolor{CustomGray}{#1}\textbf{#2} \hfill \small{#3}
}

\tolerance=8000



\begin{document}

\raggedbottom

    \begin{titlepage}
        \begin{center}
            \vspace*{3cm}
            
            \fontsize{36}{40}\selectfont
            
            \boldtitle{Aneks F:}
            
            \vspace{0.5cm}
            
            \normaltitle{Fotokorpus haseł \emph{SW}}
            
            \vfill
        \end{center}
    \end{titlepage}

    \setcounter{page}{202}



    \vbox{%
        \parbox{15cm}{
            \dictentry{†}
            {Fałbuła}
            {t. I (A-G), lit. F, str. 715, kol. R, ak. 32}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{example-image-duck}
        \end{mdframed}
    }

        
    \vspace{2em}%


    \vbox{%
        \parbox{15cm}{
            \dictentry{}
            {Fałd}
            {t. I (A-G), lit. F, str. 715, kol. R, ak. 33 – str. 716, kol. L, ak. 01}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{example-image-duck}
        \end{mdframed}
    }

    \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
        \includegraphics[width=\linewidth]{example-image-duck}
        \unskip
    \end{mdframed}
            
            
    \vspace{2em}%


    \vbox{%
        \parbox{15cm}{
            \dictentry{}
            {Fałszywy}
            {t. I (A-G), lit. F, str. 717, kol. L, ak. 18 – str. 717, kol. R, ak. 01}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{example-image-duck}
        \end{mdframed}
    }
            
    \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
        \includegraphics[width=\linewidth]{example-image-duck}
        \unskip
    \end{mdframed}
                
                    
    \vspace{2em}%


    \vbox{%
        \parbox{15cm}{
            \dictentry{}
            {Fama}
            {t. I (A-G), lit. F, str. 717, kol. R, ak. 02}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{example-image-duck}
        \end{mdframed}
    }
        
    
    \vspace{2em}%


    \vbox{%
    \parbox{15cm}{
            \dictentry{}
            {Fenacetyna}
            {t. I (A-G), lit. F, str. 731, kol. L, ak. 24}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{example-image-duck}
        \end{mdframed}
    }

        
    \vspace{2em}%


    \vbox{%
        \parbox{15cm}{
            \dictentry{}
            {Fenakistoskop}
            {t. I (A-G), lit. F, str. 731, kol. L, ak. 25}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{example-image-duck}
        \end{mdframed}
    }

        
    \vspace{2em}%
    

    \vbox{%
        \parbox{15cm}{
            \dictentry{}
            {Fenantren}
            {t. I (A-G), lit. F, str. 731, kol. L, ak. 26}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{example-image-duck}
        \end{mdframed}
        }

        
        \vspace{2em}%


        \vbox{%
            \parbox{15cm}{
                \dictentry{†}
                {Fendel}
                {t. I (A-G), lit. F, str. 731, kol. L, ak. 27 – str. 731, kol. R, ak. 01}
            }
            \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
                \includegraphics[width=\linewidth]{example-image-duck}
            \end{mdframed}
        }
        \begin{mdframed}[linewidth=2pt, linecolor=CustomGray, nobreak=true]
            \includegraphics[width=\linewidth]{example-image-duck}
            \unskip
        \end{mdframed}
                    

\end{document}

enter image description here