0

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}
Akaizoku
  • 608
  • I know this question has been asked before...just give a few minutes for someone to find the link. Welcome to the site. – Steven B. Segletes Nov 16 '16 at 15:45
  • 2
    Perhaps this: http://tex.stackexchange.com/questions/6073/scale-resize-large-images-graphics-that-exceed-page-margins. With the adjustbox package, \includegraphics[max width=\linewidth]{<image file name>} – Steven B. Segletes Nov 16 '16 at 15:50
  • @Steven B. Segletes I had stumbled upon that thread but since it had it backward, I thought I would not delve into the seemingly complicated answers to try and reverse the process.

    However, 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

0 Answers0