Take the 2-minute tour ×
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.

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
    
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
add comment

migrated from stackoverflow.com Oct 30 '12 at 14:25

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

2 Answers

up vote 2 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
add comment
\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
add comment

Your Answer

 
discard

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