Resize all images in Latex to a percentage width

I am auto-generating multiple tex files using pandoc (one tex file for each of my chapters). Pandoc generates this tex code for each image.

\begin{figure}[htbp]
\centering
\includegraphics{images/flock_of_birds.JPG}
\caption{A flock of storks rush to fill orders during the Baby Boom.}
\end{figure}


How can I set the width of all images to a percentage value without directly modifying the generated code? I have attempted to use \renewcommand\includegraphics but my LaTex knowledge falls short.

migrated from stackoverflow.comOct 30 '12 at 14:25

This question came from our site for professional and enthusiast programmers.

• Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. – Werner Oct 31 '12 at 6:21

You can redefine the existing \includegraphics macro to apply any options you desire.


Notes:

• Normally one could use the \let\OldIncludegraphics{\includegraphics} syntax, but since \includegraphics has an optional parameter we need to use LetLtxMacro from the letltxmacro package A detailed description of \LetLtxMacro can be found at this question about a closed square root symbol.

• The [demo] option is used so as to place a black box where the figure would go for demo purposes, in your real usage (when you actually have the figures available), you need to remove this option.

Code:

\documentclass{article}
\usepackage[demo]{graphicx}% Remove [demo] option in real usage.
\usepackage{letltxmacro}

\LetLtxMacro{\OldIncludegraphics}{\includegraphics}

\begin{document}
\begin{figure}[htbp]
\centering
\includegraphics{images/flock_of_birds.JPG}
\caption{A flock of storks rush to fill orders during the Baby Boom.}
\end{figure}

\renewcommand{\includegraphics}[2][]{\OldIncludegraphics[width=9cm, #1]{#2}}

\begin{figure}[htbp]
\centering
\includegraphics{images/flock_of_birds.JPG}
\caption{A flock of storks rush to fill orders during the Baby Boom.}
\end{figure}

\end{document}

• Actually \includegraphics does not require \LetLtxMacro, but using it doesn't hurt. :) – egreg Oct 30 '12 at 21:06
• Just a quick note: The new answer below is for you. – Torbjørn T. Mar 10 '16 at 20:02
• @TorbjørnT.: Thanks. Also, nice photography on you flickr page. – Peter Grill Mar 10 '16 at 23:10
\documentclass{article}
\usepackage{graphicx}
\makeatletter\def\Gin@i{\Gin@ii[scale=0.2]}\makeatother

\begin{document}
\includegraphics{tiger}
\end{document}


scales every image down to 20%. You can also define width=0.3\linewidth or something else.

@Peter Grill:

• In order to overwrite width=42something it width should be the last argument: \renewcommand{\includegraphics}[2][]{\OldIncludegraphics[#1, width=9cm]{#2}}
• Note: Method does not scale the image to a percentage but to 9cm. Useful anyhow.

(Disclaimer: Not allowed to make comments in tex.stackexchange)

• Can you add an explanation as to why it should be last. I didn't put it as the last option so that it can be overridden on a per use case, if desired. – Peter Grill Mar 10 '16 at 23:09
• Your point is valid. Indeed, for me I wanted to override the size settings to something small for print where I focused on the text. Of course, one could replace all images and their captions all-together via something \LetLtxMacro{\OldSubfloat}{\subfloat} \renewcommand{\subfloat}[2][]{somePicAndCaption} – Lorin Mar 17 '16 at 11:13