我有一个盒子,其边长记录为
\newlength{\imageWidth} \setlength{\imageWidth}{300mm}
\newlength{\imageHeight} \setlength{\imageHeight}{200mm}
我正在尝试创建一个命令,该命令以图片的文件名作为参数,并将图片放置在框内,以保留纵横比并完全填充框(如果图像的纵横比宽于框的纵横比,则应剪切左侧、右侧和侧面)。
为此,我首先需要比较长宽比。我正在尝试的是
\settowidth{\imageFileWidth}{\includegraphics{#1}}
\settoheight{\imageFileHeight}{\includegraphics{#1}}
\ifnum\ratio{\imageFileWidth}{\imageFileHeight}>\ratio{\imageWidth}{\calHeight}
\includegraphics[height=\imageHeight]{#1}
\else
\includegraphics[width=\imageWidth]{#1}
\fi
使用该calc
包。但是它不起作用,我不断收到Illegal unit of measure (pt inserted)
错误Missing = inserted for \ifnum
。
有什么建议吗?
更新感谢出色的回答解决了贝尔特我现在正在使用此代码进行简约的照片日历类光度计
答案1
如果比率较小,则可以使用\fpeval
来生成 0,如果比率较大,则使用 1。我在 的分支中添加了一些额外的描述信息,您可以根据实际使用情况将其删除。我还\ifnum
减少了 ,因此图像不会超出页面,至少在文章类的尺寸范围内不会超出页面。\imageWidth
\imageHeight
\documentclass{article}
\usepackage{graphicx}
\newlength{\imageWidth}
\setlength{\imageWidth}{60mm}
\newlength{\imageHeight}
\setlength{\imageHeight}{40mm}
\newlength{\imageFileWidth}
\newlength{\imageFileHeight}
\newcommand{\custompic}[1]{%
\settowidth{\imageFileWidth}{\includegraphics{#1}}%
\settoheight{\imageFileHeight}{\includegraphics{#1}}%
\ifnum\fpeval{\the\imageFileWidth/\the\imageFileHeight > \the\imageWidth/\the\imageHeight}=1
Greater:
\fpeval{ceil(\the\imageFileWidth/\the\imageFileHeight,2)}%
\textgreater
\fpeval{ceil(\the\imageWidth/\the\imageHeight,2)}\par
\includegraphics[height=\imageHeight]{#1}%
\else
Less:
\fpeval{ceil(\the\imageFileWidth/\the\imageFileHeight,2)}%
\textless
\fpeval{ceil(\the\imageWidth/\the\imageHeight,2)}\par
\includegraphics[width=\imageWidth]{#1}%
\fi
}
\begin{document}
\custompic{example-image-duck}
\custompic{example-image-4x3}
\end{document}
只是为了好玩,这是一个 expl3 实现。欢迎提出改进建议。
\documentclass{article}
\usepackage{graphicx}
\ExplSyntaxOn
\dim_const:Nn \c_julian_width_dim { 60mm }
\dim_const:Nn \c_julian_height_dim { 40mm }
\fp_new:N \l_julian_fixedratio_fp
\fp_set:Nn \l_julian_fixedratio_fp
{
\dim_ratio:nn { \c_julian_width_dim } { \c_julian_height_dim }
}
\fp_new:N \l_julian_tmpratio_fp
\box_new:N \l_julian_image_box
\cs_new:Nn \julian_custom_pic:n
{
\hbox_set:Nn \l_julian_image_box { \includegraphics{#1} }
\fp_set:Nn \l_julian_tmpratio_fp
{
\dim_ratio:nn { \box_wd:N \l_julian_image_box } { \box_ht:N \l_julian_image_box }
}
\fp_compare:nNnTF { \l_julian_tmpratio_fp } > { \l_julian_fixedratio_fp }
{
Greater:~
\fp_eval:n { ceil( \l_julian_tmpratio_fp, 2 ) }
\textgreater
\fp_eval:n { ceil( \l_julian_fixedratio_fp, 2 ) }\par
\includegraphics[height=\c_julian_height_dim]{#1}%
}
{
Less:~
\fp_eval:n { ceil( \l_julian_tmpratio_fp, 2 ) }
\textless
\fp_eval:n { ceil( \l_julian_fixedratio_fp, 2 ) }\par
\includegraphics[width=\c_julian_width_dim]{#1}%
}
}
\NewDocumentCommand { \custompic } { m } { \julian_custom_pic:n { #1 } }
\ExplSyntaxOff
\begin{document}
\custompic{example-image-duck}
\custompic{example-image-4x3}
\end{document}
再次为了好玩,这里有一个显示剪辑区域的版本。
\documentclass{article}
\usepackage{graphicx,xcolor}
\ExplSyntaxOn
\dim_const:Nn \c_julian_width_dim { 60mm }
\dim_const:Nn \c_julian_height_dim { 40mm }
\fp_new:N \l_julian_fixedratio_fp
\fp_set:Nn \l_julian_fixedratio_fp
{
\dim_ratio:nn { \c_julian_width_dim } { \c_julian_height_dim }
}
\fp_new:N \l_julian_tmpratio_fp
\box_new:N \l_julian_image_box
\cs_new:Nn \julian_custom_pic:n
{
\hbox_set:Nn \l_julian_image_box { \includegraphics{#1} }
\fp_set:Nn \l_julian_tmpratio_fp
{
\dim_ratio:nn { \box_wd:N \l_julian_image_box } { \box_ht:N \l_julian_image_box }
}
\fp_compare:nNnTF { \l_julian_tmpratio_fp } > { \l_julian_fixedratio_fp }
{
\includegraphics[height=\c_julian_height_dim]{#1}%
}
{
\includegraphics[width=\c_julian_width_dim]{#1}%
}
}
\NewDocumentCommand { \custompic } { m } { \julian_custom_pic:n { #1 } }
\ExplSyntaxOff
\setlength{\unitlength}{1mm}
\linethickness{1pt}
\newcommand{\showclip}[1]{%
\begin{picture}(60,40)
\color{red}
\put(0,0){#1}
\put(0,0){\line(0,1){40}}
\put(60,0){\line(0,1){40}}
\put(0,0){\line(1,0){60}}
\put(0,40){\line(1,0){60}}
\end{picture}
}
\begin{document}
\showclip{\custompic{example-image-duck}}
\vspace{1cm}
\showclip{\custompic{example-image-4x3}}
\end{document}