TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from stackoverflow.com Oct 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
up vote 3 down vote accepted

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

Below, I placed the \renewcommand in the main document just so that you can see the effect before and after. The \renewcommand below ensures that all subsequent usages of \includegraphics have the width=9cm option applied. Here is a before and after comparrison:

enter image description here

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}
share|improve this answer
    
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 at 20:02
    
@TorbjørnT.: Thanks. Also, nice photography on you flickr page. – Peter Grill Mar 10 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.

share|improve this answer

@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)

share|improve this answer
    
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 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 at 11:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.