Problem
I would like to automatically switch between width=\textwidth and scale=1 arguments depending on the actual width of the image I am trying to insert in order to prevent it from being stretched if it is smaller than the text width.
Edit / Solution
The following code is adapted from one of the proposed solution in this thread.
\documentclass[draft]{article}
\usepackage{graphicx}
\makeatletter
\def\autoscale{
\ifdim\Gin@nat@width<\linewidth
\Gin@nat@width
\else
\linewidth
\fi
}
\makeatother
\let\oldincludegraphics\includegraphics % Resize small figures
\renewcommand\includegraphics[2][]{
\oldincludegraphics[width=\autoscale]{#2}
}
\begin{document}
\begin{figure}
\centering
\includegraphics[]{img/test.png}
\end{figure}
\end{document}
adjustboxpackage,\includegraphics[max width=\linewidth]{<image file name>}– Steven B. Segletes Nov 16 '16 at 15:50However, I did go back and ended up implementing the following solution: \makeatletter \def\autoscale{ \ifdim\Gin@nat@width<\linewidth \Gin@nat@width \else \linewidth \fi } \makeatother \let\oldincludegraphics\includegraphics % Resize small figures \renewcommand\includegraphics[2][]{ \oldincludegraphics[width=\autoscale]{#2} }
– Akaizoku Nov 16 '16 at 16:39