이미지가 특정 너비를 초과하지 않도록 만드는 방법은 무엇입니까?

이미지가 특정 너비를 초과하지 않도록 만드는 방법은 무엇입니까?

문서에 외부 이미지를 사용한 적이 없는데 오늘은 뭔가를 찾아봐야 했습니다. 나는 달려갔지만 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}

추신: 권장되는지는 확실하지 않지만 이미지 1, 2, 3에 대해 서로 다른 크기와 너비를 사용해 보고 확인할 수 있습니다.

관련 정보