解决方案发布在这里缩放(调整大小)超出页边距的大型图像(图形)效果很好。总结一下,Dave Jarvis 建议
\usepackage{graphicx}
% Determine if the image is too wide for the page.
\makeatletter
\def\ScaleIfNeeded{%
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
% Resize figures that are too wide for the page.
\let\oldincludegraphics\includegraphics
\renewcommand\includegraphics[2][]{%
\oldincludegraphics[width=\ScaleIfNeeded]{#2}
}
但不幸的是,这会破坏你在 Lyx 中对图像应用的任何缩放。例如,我有一张图片被调整为
\includegraphics[scale=0.5]{nicecats}
上述解决方案中建议的宏删除了我的 0.5 缩放比例。我想知道是否有办法让宏先应用任何用户指定的缩放比例,然后检查宽度?或者检查任何预先指定的缩放比例,如果存在则不调整大小。非常感谢。
答案1
以这种方式使用它,无需\ScaleIfNeeded
\newsavebox\IBox
\let\IncludeGraphics\includegraphics
\renewcommand\includegraphics[2][]{%
\sbox\IBox{\IncludeGraphics[#1]{#2}}%
\ifdim\wd\IBox>\linewidth\resizebox{\linewidth}{!}{\usebox\IBox}\else\usebox\IBox\fi}
首先应用你的参数,然后测试是否大于\linewidth
答案2
没有必要重新定义\includegraphics
。默认宽度可以全局设置,如手册第 4.6 节所述全局设置按键:
\documentclass{article}
\usepackage{graphicx}
% Determine if the image is too wide for the page.
\makeatletter
\def\ScaleIfNeeded{%
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
\setkeys{Gin}{width=\ScaleIfNeeded}
\begin{document}
\begin{minipage}{2cm}
\includegraphics{smiley}
\end{minipage}
\begin{minipage}{10cm}
\includegraphics{smiley}
\end{minipage}
% Overwrite global width setting:
\includegraphics[width=.5\textwidth]{smiley}
% Overwrite global width setting: '!' = normal width
\includegraphics[scale=.2,width=!]{smiley}
\end{document}
如果需要,您可以覆盖默认宽度设置。请注意scale
和width
不能很好地协同工作,因此您必须决定使用哪一个。
如果您确实想要混合width
,则scale
必须\includegraphics
手动将其包装到\resizebox
(也由graphics/x
包提供)。请注意此处的不同\ScaleIfNeeded
宏,无论如何都可以内联,因为它仅使用一次。
\documentclass{article}
\usepackage{graphicx}
% Determine if the image is too wide for the page.
\def\ScaleIfNeeded{%
\ifdim\width>\linewidth
\linewidth
\else
\width
\fi
}
\let\origincludegraphics\includegraphics
\renewcommand*\includegraphics[2][]{%
\resizebox{\ScaleIfNeeded}{!}{\origincludegraphics[#1]{#2}}%
}
\begin{document}
\begin{minipage}{2cm}
\includegraphics[scale=10]{smiley}
\end{minipage}
\begin{minipage}{10cm}
\includegraphics{smiley}
\end{minipage}
\includegraphics[width=.5\textwidth]{smiley}
\includegraphics[scale=.2]{smiley}
\end{document}