\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}
使用此代码,您可以创建 2x2 英寸的护照照片。如果使用 A4 纸,我们如何将照片排列成列,以便在一页上生成多张护照照片,而不会浪费相纸。只是为了好奇和好玩。
答案1
使用表格怎么样environment
,精确的顺序取决于相纸的大小?
\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}
带循环的版本
\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}