# Broken build if a too big picture is included in a framed environment, how to insert “horizontal slices” rather?

This produces about 5000 pages before crashing:

\documentclass{article}

\usepackage{framed}
\usepackage{graphicx}

\begin{document}
\begin{framed}
\noindent\includegraphics[height=35cm]{example-image-a}\par
% \kern-33cm\penalty0
% \kern10cm\penalty0
% \kern10cm\penalty0
% \kern10cm\penalty0
% \kern3cm
\end{framed}
\end{document}


or to get more real-life case

\documentclass{article}
\usepackage{framed}
\usepackage{graphicx}
\begin{document}
\vspace*{15cm}
\begin{framed}
\noindent\includegraphics[height=20cm]{example-image-a}\par
\end{framed}
\end{document}


Adding breakpoints by de-commenting out the "kern" lines "solves" this, of course the included graphic is too big.

in second example, the compilation succeeds if height is say at most 18cm and framed picture ends up then on page 2, but it fails for 18.55cm (for info \textheight is 19.33cm here.)

I thought, maybe I can use multiple \includegraphics with trim option to insert only say a \baselineskip high horizontal slice of the image. Hoping that the backend pdftex or xetex will include the image file only once for all such successive horizontal slices. (real use case is a png image). Hoping too that there will be no artefact (from anti-aliasing or whats not) in PDF for the list of successive "horizontal slices" allowing suitable pagebreak.

Problem is the "trimming" should be done relative to rescaled picture for a total height of 35cm (forget in this example that it is too wide, too). I looked at the graphicx documentation but failed to see how to do that easily.

What about this problem if using other framing tools tcolorbox, mdframed?

From further testing, my original issue is clearly solved by ensuring that the included graphics has a height reasonably smaller than \textheight as the contents will be shifted to next page if not enough room, and the infinite loop arises if framed does not find enough room on new page.

But my question about "horizontal slices" still stands.

About avoiding the problem with framed and keeping into account the \includegraphics are not manually written but originates elsewhere, I will fix it this way:

\makeatletter
\newbox\my@image@box
\newdimen\my@image@maxheight
\AtBeginDocument{%
\my@image@maxheight\textheight
\advance\my@image@maxheight -2.5\baselineskip % -20pt not enough in my testing
}
\newcommand*{\myincludegraphics}[2][]{%
\gdef\my@includegraphics@options{#1}%
\setbox\my@image@box\hbox{\includegraphics[#1,draft]{#2}}%
\in@false
\ifdim \wd\my@image@box>\linewidth
\in@true
\fi
% no rotation, no need to worry about depth
\ifdim \ht\my@image@box>\my@image@maxheight
\in@true
\fi
\ifin@
\fi
\setbox\my@image@box\box\voidb@x % clear memory
\expandafter\includegraphics\expandafter[\my@includegraphics@options]{#2}%
}
\makeatother


This ensures the width does not exceed \linewidth and the height something a bit smaller than \textheight to allow some room for framed.

@DavidCarlisle code, with a loop added:

\documentclass{article}

\usepackage{framed}

\usepackage{xinttools}
\begin{document}

\vspace*{15cm}

\raggedbottom
\begin{framed}\parskip0pt \lineskip0pt

%\noindent\clipbox{0 30cm 0  0cm}{\includegraphics[height=35cm,width=\linewidth]{temp}}
\xintFor*#1in{\xintdimensions[0pt+\baselineskip]}:
{%
\noindent\clipbox{0 {\dimexpr35cm-\baselineskip-#1} 0 #1}%
{\includegraphics[height=35cm,width=\linewidth]{example-image-a}}\par
\ifdim\dimexpr#1+\baselineskip>35cm
\expandafter\xintBreakFor
\fi
}
\end{framed}
\end{document}


The screenshot is from Adobe Reader. However one sees some separation between the slices.

In Skim.app also I see artefacts: (not very visible here)

• How could be possible to insert a height=35cm image in a standard sized document? – Sigur Jan 11 at 17:30
• @Sigur, how it could be possible is precisely my question! (but I agree my inital posting had bad wording, I hope it got clearer after). – user4686 Jan 11 at 18:02
• you can't just do what framed originally do? save the whole frame in \vbox and then \vsplit? – touhami Jan 11 at 18:30
• @touhami, no (untested) \vsplit can't split something unsplittable. – user4686 Jan 11 at 18:33
• oh yes, my bad. No such problem with tcolorbox. – touhami Jan 11 at 18:36

\documentclass{article}

\usepackage{framed}

\begin{document}

\vspace*{15cm}

\raggedbottom
\begin{framed}
\setlength\parskip{0pt}

\noindent\clipbox{0 30cm 0  0cm}{\includegraphics[height=35cm,width=\linewidth]{example-image-a}}

\noindent\clipbox{0 25cm 0  5cm}{\includegraphics[height=35cm,width=\linewidth]{example-image-a}}

\noindent\clipbox{0 20cm 0 10cm}{\includegraphics[height=35cm,width=\linewidth]{example-image-a}}

\noindent\clipbox{0 15cm 0 15cm}{\includegraphics[height=35cm,width=\linewidth]{example-image-a}}

\noindent\clipbox{0 10cm 0 20cm}{\includegraphics[height=35cm,width=\linewidth]{example-image-a}}

\noindent\clipbox{0  5cm 0 25cm}{\includegraphics[height=35cm,width=\linewidth]{example-image-a}}

\noindent\clipbox{0  0cm 0 30cm}{\includegraphics[height=35cm,width=\linewidth]{example-image-a}}

\end{framed}

\end{document}

• too bad \clipbox switches to LR mode is there some \vbox version so that I could prevent insertion of glue and only add breakpoints? – user4686 Jan 11 at 20:15
• testing with a 150K png (a screenshot) I have nice surprise the PDF weighs only about 100K ! – user4686 Jan 11 at 20:18
• really cool that PDF size does not increase with number of \includegraphics, I tried pdftex and xetex – user4686 Jan 11 at 20:21
• @jfbu \hbox{\clipbox{.... or just set \parskip to 0pt – David Carlisle Jan 11 at 20:23
• nice, I think you could add that to your answer. Of course I will delay the green tick fo after a reasonable period of pondering the pros and cons... :) – user4686 Jan 11 at 20:26