XYZ
XYZ

Reputation: 27417

How to make inline images responsive in org-mode?

When I toggle inline image using org-toggle-inline-images which shows the image(s). However, when the image it too large it will overflow the editor's frame (See screenshot below).

enter image description here

How to make the image responsive which means the max size of the image should never exceed the width of window size.

Of course, I can use the code below to fix the size of the image. But what I truly want is some kind of responsive image displaying.

(setq org-mode-actual-width 600)

enter image description here

Thanks for your time viewing this question.

Upvotes: 9

Views: 3617

Answers (2)

William
William

Reputation: 61

The function below re-renders the org-mode inline images whenever the window is resized by using hooks. It shrinks the image only when the window is displaying less than 80 columns (usually 1/3 of the screen). I believe it works better than always rendering in window width.

(defun org-image-resize (frame)
  (when (derived-mode-p 'org-mode)
      (if (< (window-total-width) 80)
      (setq org-image-actual-width (window-pixel-width))
    (setq org-image-actual-width (* 80 (window-font-width))))
      (org-redisplay-inline-images)))

(add-hook 'window-size-change-functions 'org-image-resize)

GIF Demo of the code above

If you want the image just matching the size of your window width:

(defun org-image-resize (frame)
  (when (derived-mode-p 'org-mode)
      (setq org-image-actual-width (window-pixel-width))
      (org-redisplay-inline-images)))

(add-hook 'window-size-change-functions 'org-image-resize)

Or change the (window-pixel-width) into (- (window-pixel-width) 20) if you want some margin on the right side.

Upvotes: 6

Dominik Schrempf
Dominik Schrempf

Reputation: 877

From the documentation of org-image-actual-width:

Documentation:
Should we use the actual width of images when inlining them?

When set to t, always use the image width.

When set to a number, use imagemagick (when available) to set
the image's width to this value.

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.

When set to nil, try to get the width from an #+ATTR.* keyword
and fall back on the original width if none is found.

So you could use the attribute #+ATTR_HTML to specify the width. It has to be specified manually though. Is this responsive enough?

Upvotes: 6

Related Questions