2
\documentclass{article}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{ifthen}
\newlength{\oH}
\newlength{\oW}
\newlength{\rH}
\newlength{\cH}
\newcommand\PrintImage[3]{% width, height, image
  \settototalheight{\oH}{\includegraphics{#3}}%
  \settowidth{\oW}{\includegraphics{#3}}%
  \setlength{\rH}{\oH * \ratio{#1}{\oW}}
  \ifthenelse{\lengthtest{\rH < #2}}{
    \includegraphics[width=#1]{#3}%
  }{%
    \setlength{\cH}{(\rH-#2)*\ratio{\oW}{#1}}%
    \includegraphics[width=#1,clip,trim=0 \cH{} 0 0]{#3}%
  }%
}

\begin{document}
\PrintImage{6cm}{2cm}{yourimage}
\end{document}

Using this code you can create 2x2 inch passport photograph. If one is using a4 paper, how can we arrange the photograph in columns so that multiple passport photographs can be generated on one page and photopaper is not wasted. Just for curiosity and fun.

Vaibhav
  • 6,625
  • 15
  • 49
  • 76

1 Answers1

3

How about using a tabular environment, the precise order depends on the size of the photograph paper?

\documentclass{article}
\usepackage[a4paper,lmargin=0.5cm,rmargin=0.5cm,tmargin=0.2cm,bmargin=0.2cm]{geometry}
\usepackage[demo]{graphicx}
\usepackage{calc}
\usepackage{ifthen}
\newlength{\oH}
\newlength{\oW}
\newlength{\rH}
\newlength{\cH}
\newcommand\PrintImage[3]{% width, height, image
  \settototalheight{\oH}{\includegraphics{#3}}%
  \settowidth{\oW}{\includegraphics{#3}}%
  \setlength{\rH}{\oH * \ratio{#1}{\oW}}
  \ifthenelse{\lengthtest{\rH < #2}}{
    \includegraphics[width=#1]{#3}%
  }{%
    \setlength{\cH}{(\rH-#2)*\ratio{\oW}{#1}}%
    \includegraphics[width=#1,clip,trim=0 \cH{} 0 0]{#3}%
  }%
}

\newlength{\extraspaceforcutting}
\setlength{\extraspaceforcutting}{0.2ex}
\begin{document}
\begin{tabular}{*{3}{@{}c@{}}}
\PrintImage{6cm}{2cm}{yourimage} &\PrintImage{6cm}{2cm}{yourimage} & \PrintImage{6cm}{2cm}{yourimage} \tabularnewline[\extraspaceforcutting] 
\PrintImage{6cm}{2cm}{yourimage} &\PrintImage{6cm}{2cm}{yourimage} & \PrintImage{6cm}{2cm}{yourimage} \tabularnewline[\extraspaceforcutting]
\PrintImage{6cm}{2cm}{yourimage} &\PrintImage{6cm}{2cm}{yourimage} & \PrintImage{6cm}{2cm}{yourimage} \tabularnewline[\extraspaceforcutting]
\PrintImage{6cm}{2cm}{yourimage} &\PrintImage{6cm}{2cm}{yourimage} & \PrintImage{6cm}{2cm}{yourimage} \tabularnewline[\extraspaceforcutting]
\PrintImage{6cm}{2cm}{yourimage} &\PrintImage{6cm}{2cm}{yourimage} & \PrintImage{6cm}{2cm}{yourimage} \tabularnewline[\extraspaceforcutting]
\PrintImage{6cm}{2cm}{yourimage} &\PrintImage{6cm}{2cm}{yourimage} & \PrintImage{6cm}{2cm}{yourimage} \tabularnewline[\extraspaceforcutting]
\PrintImage{6cm}{2cm}{yourimage} &\PrintImage{6cm}{2cm}{yourimage} & \PrintImage{6cm}{2cm}{yourimage} \tabularnewline[\extraspaceforcutting]
\PrintImage{6cm}{2cm}{yourimage} &\PrintImage{6cm}{2cm}{yourimage} & \PrintImage{6cm}{2cm}{yourimage} \tabularnewline[\extraspaceforcutting]
\end{tabular}
\end{document}

enter image description here

Version with a loop

\documentclass{article}
\usepackage[a4paper,lmargin=0.5cm,rmargin=0.5cm,tmargin=0.2cm,bmargin=0.2cm]{geometry}
\usepackage{forloop}%
\usepackage{etoolbox}%
\usepackage[demo]{graphicx}
\usepackage{calc}
\usepackage{ifthen}
\newlength{\oH}
\newlength{\oW}
\newlength{\rH}
\newlength{\cH}
\newcommand\PrintImage[3]{% width, height, image
  \settototalheight{\oH}{\includegraphics{#3}}%
  \settowidth{\oW}{\includegraphics{#3}}%
  \setlength{\rH}{\oH * \ratio{#1}{\oW}}
  \ifthenelse{\lengthtest{\rH < #2}}{
    \includegraphics[width=#1]{#3}%
  }{%
    \setlength{\cH}{(\rH-#2)*\ratio{\oW}{#1}}%
    \includegraphics[width=#1,clip,trim=0 \cH{} 0 0]{#3}%
  }%
}%

\newcounter{rowcounter}
\newcounter{columncounter}

\newcounter{maxcolumns}
\newcounter{maxrows}
\setcounter{maxcolumns}{3}
\setcounter{maxrows}{9}%
\newlength{\extraspaceforcutting}
\setlength{\extraspaceforcutting}{0.2ex}
\begin{document}
\begin{tabular}{*{\value{maxcolumns}}{@{}c@{}}}
  \forloop{rowcounter}{1}{\value{rowcounter} < \numexpr\value{maxrows}}{%
    \forloop{columncounter}{1}{\value{columncounter} < \numexpr\value{maxcolumns}+1}{%
      \PrintImage{6cm}{2cm}{yourimage} \ifnumless{\value{columncounter}}{3}{&}{}%
    }%
    \ifnumless{\value{rowcounter}}{\value{maxrows}-1}{\tabularnewline[\extraspaceforcutting]%
    }{} % no newline 
  }%
\end{tabular}
\end{document}
  • Can't we have something like loop – Vaibhav Dec 13 '14 at 15:12
  • @Vaibhav: Yes, but that means that you either have to compute the names of the images or use the same image all the time? –  Dec 13 '14 at 15:25
  • Normally person require multiple copies of same photograph , so loop should be ideal . If image is 2x2 inches, how can we adjust it on a4 paper? – Vaibhav Dec 13 '14 at 15:46
  • @Vaibhav:See the update please –  Dec 13 '14 at 15:47