71

Is there a general way to define the size, in percent or pixels, for an image that is linked in org-mode?

Say I have the following link in my .org file:

[[~/images/example.jpg]]

This JPG is way too large, so if I export it to HTML or LaTeX or open it in org-mode with C-c C-o i will only see a fraction of the image.

7 Answers 7

93

As of Org 8.0, "Attribute lines now take plists" :

#+attr_html: :width 100px
#+attr_latex: :width 100px
[[~/images/example.jpg]]
0
63

As per Jacobo's comment, add the following to your init.el file:

(setq org-image-actual-width nil)

Then in org-mode, you can precede each JPG, PNG, and SVG as shown in the following example to adjust the size of each inlined image.

#+ATTR_ORG: :width 100
[[~/images/example.svg]]

If you want to size this for both inline previews and html output:

#+ATTR_HTML: width="100px"
#+ATTR_ORG: :width 100
[[~/images/example.svg]]
8
  • 1
    Adam, I would assume that Scalable Vector Graphics (SVG) images do not have an inherent size, as they are designed to be scalable. Jan 14, 2022 at 10:38
  • 1
    Hi @NiclasBörlin it looks like this was a bug in my version of Emacs (but has since been resolved). I can confirm that as of Emacs 27.2 (Railwaycat Homebrew edition of Mitsuharu's Emacs) this works for SVGs as well.
    – Adam
    Jan 14, 2022 at 11:36
  • @Adam I accidentally down-voted your answer, and stack overflow does not permit me to adjust it. Due to that I upvoted your comment. Sorry for the inconvenience Feb 14, 2022 at 14:49
  • 1
    @EmmanuelGoldstein the height should adjust itself proportionately, when you set the width. So as long as your are not looking to purposefully distort your image, you only need to set one of these.
    – Adam
    Feb 23, 2022 at 14:17
  • 1
    I want to purposefully "distort" my image because by default the data points look too clustered, so I'd like to have a different ratio. Thanks. Feb 23, 2022 at 18:08
14
#+ATTR_HTML: width="100px"
[[~/images/example.jpg]]
3
  • 1
    Thank you for your answer bzg! First of all, I was hoping for an option that is not specific to html but would also work for LaTex and viewing in Emacs itself. Anyway, even though your suggestion appears to be correct according to the official documentation [1][2] it does somehow not work for me. I'm using emacs 24.1.1 without any customization but when I try your code, the image is still in its original, gigantic size. This is true for Chrome and Firefox. [1] orgmode.org/worg/org-tutorials/images-and-xhtml-export.html [2] w3schools.com/tags/tag_img.asp Jul 28, 2012 at 18:50
  • I've also tried #+ATTR_HTML: width="0.3" Which, if I understand correct, should scale by a factor of 0.3 (or 0.003 = 0.3% ?) but this has no effect either. :-/ Jul 28, 2012 at 18:50
  • 10
    In order for this to work, you need to set this in your .emacs file: (setq org-image-actual-width nil). Other possible values and behaviours are explained in this post to org-mode's mailing list Jan 17, 2013 at 11:47
14

This is a sample on how to resize an image using percentages (Org mode version 9.0.5):

#+CAPTION: Weight space                                                                                                                                     
#+ATTR_HTML: :alt neural network :title Neural network representation :align right                                                                          
#+ATTR_HTML: :width 50% :height 50%                                                                                                                         
https://i.stack.imgur.com/nzHSl.jpg
3
  • other answers didn't work for me for images based on a URL
    – ealfonso
    Jul 18, 2019 at 3:42
  • Does this work with inline previews as well?
    – HappyFace
    Sep 1, 2021 at 12:57
  • @HappyFace yes it does
    – alper
    May 20 at 20:21
8

Here is a way to resize images in emacs Org mode for preview (not exporting). Typically,

  1. We want to set an image to a specific width like 249px when we need to.
  2. We want to set a default image width, so that we don't need to specify +attr_html for every image - that would be tedious.

This can be achieved by configuring org-image-actual-width like follows:

(setq org-image-actual-width (list 550))

Then, in your .org file, if you have

#+attr_html :width 249
[[~/images/example1.jpg]]

then the image will be displayed in preview at width 249px. For another image, where no +attr_* is specified, the default width of 550px will be applied.

[[~/images/example2.jpg]]

You can see this behavior from the documentation in org-mode source code:

When set to a number in a list, try to get the width from any
#+ATTR.* keyword if it matches a width specification like
  #+ATTR_HTML: :width 300px
and fall back on that number if none is found.

I found it hard to understand what does "a number in a list mean", so I looked at the implementation, and indeed, something like (list 550) works.

3

For LaTeX, to remove the default width=.9\linewidth, set the org-latex-image-default-width to empty string. By this way, the image will have its natural size.

To do that on the fly use the set-variable emacs command. Or to set this variable permanently, add the following line in your init.el : (setq org-latex-image-default-width "")

1
  • 1
    and to set it to the full linewidth (or any other value), use (setq org-latex-image-default-width "1.0\\linewidth")
    – lab
    Aug 31, 2021 at 14:16
0

For export to LaTeX, if you hope to 'scale' the image (a la Alioth's response for HTML), there are a few ways to go.

Scaling relative to the image size:

#+ATTR_LATEX: :scale 1.2

Scaling relative to what LaTeX thinks is going on with the typesetting:

#+ATTR_LATEX: :width 0.40\linewidth

#+ATTR_LATEX: :width 0.5\textwidth

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.