如何讓圖片不超過一定的寬度?

如何讓圖片不超過一定的寬度?

我從未在文件中使用過外部圖像,但今天我必須查找一些內容。我遇到過,graphicx但我想知道如何透過縮小(如果需要)來適應這個約束,使所有圖像的寬度不超過 300 像素。

感謝您的協助。

答案1

檢查 內寬度的自動化\hbox

\documentclass{article}
\usepackage{graphicx}
\newsavebox{\mybox}
\let\oldincludegraphics\includegraphics
\xdef\maxwidth{0.9\textwidth}
\renewcommand{\includegraphics}[2][]{%
  \savebox{\mybox}{%
    \hbox{\oldincludegraphics[#1]{#2}}}%
\ifdim\wd\mybox>\maxwidth
  \oldincludegraphics[width=\maxwidth,keepaspectratio]{#2}%
\else
  \oldincludegraphics[#1]{#2}%
\fi}
\begin{document}
\includegraphics{image1.png}

\includegraphics[width=0.3\textwidth]{image2.png}

\includegraphics[width=1.4\textwidth]{image3.png}


\end{document}

PS:不確定是否推薦,但您可以嘗試為 image1、2 和 3 使用不同的尺寸和寬度並檢查。

相關內容