# How to translate this scenario to LaTeX looping macro?

I want to slice an image into 8 parts, i.e., 2 rows by 4 columns. Each part is on a single page as follows. The gray frame is the Acrobat Reader background, so don't think it is a part of the output I want to have.

\documentclass{minimal}
\usepackage[margin=2cm,a6paper]{geometry}
\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics{bald}}
\newdimen\width
\newdimen\height
\width=\wd\IBox
\height=\ht\IBox
\parindent=0bp
\usepackage{multido}
\begin{document}
\centering%
%first row
\fbox{\includegraphics[height=\textheight,viewport={0\width} {0.5\height} {0.25\width} {\height},clip]{bald}}
\fbox{\includegraphics[height=\textheight,viewport={0.25\width} {0.5\height} {0.5\width} {\height},clip]{bald}}
\fbox{\includegraphics[height=\textheight,viewport={0.5\width} {0.5\height} {0.75\width} {\height},clip]{bald}}
\fbox{\includegraphics[height=\textheight,viewport={0.75\width} {0.5\height} {\width} {\height},clip]{bald}}
%second row
\fbox{\includegraphics[height=\textheight,viewport={0\width} {0\height} {0.25\width} {0.5\height},clip]{bald}}
\fbox{\includegraphics[height=\textheight,viewport={0.25\width} {0\height} {0.5\width} {0.5\height},clip]{bald}}
\fbox{\includegraphics[height=\textheight,viewport={0.5\width} {0\height} {0.75\width} {0.5\height},clip]{bald}}
\fbox{\includegraphics[height=\textheight,viewport={0.75\width} {0\height} {\width} {0.5\height},clip]{bald}}
\end{document}


If you need the image to be sliced, here it is.

I have attempted to translate it using nested \multido macros as follows but it did work. I think it is because I have no enough knowledge in doing length manipulation in (La)TeX.

\documentclass{minimal}
\usepackage[margin=2cm,a6paper]{geometry}
\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics{bald}}
\newdimen\width
\newdimen\height
\width=\wd\IBox
\height=\ht\IBox
\parindent=0bp
\usepackage{multido}
\begin{document}
\centering
\multido{\nx=0+0.25}{4}
{%
\multido{\ny=1+-0.5}{2}
{%
\fbox{\includegraphics[height=\textheight,viewport={\nx\width} {\ny\height-0.5\height} {\nx\width+0.25\width} {\ny\height},clip]{bald}}%
}%
}
\end{document}

-
See my answer to Breaking pictures across multiple pages where I do something very similar, just the cutting is done vertically not horizontally. –  Martin Scharrer Jul 12 '11 at 12:10

\documentclass{minimal}
\usepackage[margin=2cm,a6paper]{geometry}
\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics{bald}}
\newdimen\width
\newdimen\height
\width=\wd\IBox
\height=\ht\IBox
\parindent=0bp
\usepackage{multido}
\begin{document}
\centering
\multido{\ry=0.5+-0.5, \rY=1.0+-0.5}{2}{%
\multido{\rx=0.00+0.25, \rX=0.25+0.25}{4}{%
\fbox{\includegraphics[%height=\textheight, makes no sense without width=...
viewport={\rx\width} {\ry\height} {\rX\width} {\rY\height},clip]{bald}}%
}\\%
}
\end{document}


-
height without width is used to keep the aspect ratio. –  xport Jul 12 '11 at 13:18
However, it makes no sense. Your paper dimensions are not proportional to the image dimensions –  Herbert Jul 12 '11 at 13:29
@xport: I know keepaspectratio do you? –  Martin Scharrer Jul 12 '11 at 13:54
@Herbert: This code can be compiled with neither xelatex nor xetex. –  xport Aug 3 '11 at 15:49
@xport: clipping is broken in the xtex driver of graphicx. –  Herbert Aug 3 '11 at 15:52