# Pandoc-generated PDF contains large images, how can I make them fit into the page?

I am using pandoc to generate pdf output out of my markdown code. So far, it works fine without any warning or error, except the fact that figures are large, so that some figures get cut and don't fit in the page. I need to rescale all figures by say 40% reduction. How can I enforce this? Where do I specify such information?

You can change the default.latex template in order to allow horizontal and vertical scaling.

1. Get the template:

pandoc -D latex > mytemplate.latex

2. Find the section $if(graphics)$ . . . $endif$ and replace its content with:

\usepackage{graphicx}
% Redefine \includegraphics so that, unless explicit options are
% given, the image width will not exceed the width or the height of the page.
% Images get their normal width if they fit onto the page, but
% are scaled down if they would overflow the margins.
\makeatletter
\def\ScaleWidthIfNeeded{%
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\def\ScaleHeightIfNeeded{%
\ifdim\Gin@nat@height>0.9\textheight
0.9\textheight
\else
\Gin@nat@width
\fi
}
\makeatother

\setkeys{Gin}{width=\ScaleWidthIfNeeded,height=\ScaleHeightIfNeeded,keepaspectratio}%

3. Call pandoc with the option --template=mytemplate.latex

• I found a solution which is kind of lame, because I am now specifiying it in the markdown code directly with fig.width=4, fig.height=3 etc. Can you solution automatically fit all figures, so that they are well-organized in the output file? I will try out soon. – Bob Feb 12 '14 at 13:00
• How do you specify the image width and height in markdown code ? I thought that was not possible. github.com/jgm/pandoc/issues/261 – xApple Jul 23 '14 at 13:18
• fig.width, etc, seem to be an options with R Markdown, rather than standard markdown. – Benjohn May 22 '15 at 18:10

Pandoc provides different backend. If you are using ConTeXt, just add

\setupexternalfigures[factor=fit]


to your preamble and figures that are greater that the pagewidth will be automatically scaled to the page width.

If you want to scale all figures to 40% of their original size, add

 \setupexternalfigures[scale=400]

• One quite noob-ish question: where do you specify this? In the markdown itself or elsewhere? – CompEcon Sep 19 '15 at 23:47
• @CompEcon: In the ConTeXt template for pandoc (somewhere before \starttext) – Aditya Sep 20 '15 at 15:02

My pandoc version (1.12.2.1) already has the code in the LaTeX template that ensures that images are no wider than the page. However, it still did not work for me as I loaded the graphicx package in my preamble. Than the code that tweaks the includegraphics command is not executed. I fixed this by copying the code that does the tweak to my preamble.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format: please note that this is not a forum. – Astrinus Oct 20 '15 at 14:35