The issue
There is a standing issue of how to adjust all the image size globally in revealjs presentation produced via org-reveal, see Can we add a config to make all images not overflow? · Issue #388 · yjwen/org-reveal. This partially stems from how org-mode handle image link in html export.
how image links are handled with org-mode export
The regular orgmode image export image with <div.figure> and <p> tags around the <img> tag, see also org-mode export images to html as figures, not img – Emacs Stack Exchange.
1 2 3 |
#+ATTR_HTML: :class stretch ,# [[file:./images/hippo1.png]] |
The culprit is the following function from ox-html. Note the suspicious <p> tags, adopted from org-mode export images to html as figures, not img – Emacs Stack Exchange:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
(defun org-html--wrap-image (contents info &optional caption label) "Wrap CONTENTS string within an appropriate environment for images. INFO is a plist used as a communication channel. When optional arguments CAPTION and LABEL are given, use them for caption and \"id\" attribute." (let ((html5-fancy (org-html--html5-fancy-p info))) (format (if html5-fancy "\n<figure%s>%s%s\n</figure>" "\n<div%s class=\"figure\">%s%s\n</div>") ;; ID. (if (org-string-nw-p label) (format " id=\"%s\"" label) "") ;; Contents. (format "\n<p>%s</p>" contents) ;; Caption. (if (not (org-string-nw-p caption)) "" (format (if html5-fancy "\n<figcaption>%s</figcaption>" "\n<p>%s</p>") caption))))) |
You can render org to export <img> in a <figure> tag, instead of a <p> tag with the following code in emacs init file.
1 2 3 4 |
;; Wrap <img> tag in a <figure> tag (setq org-html-html5-fancy t org-html-doctype "html5") |
Solutions
Add r-stretch class to figure tags
First, render org to export <img> in a <figure> tag, instead of a <p> tag with the following.
1 2 3 4 |
;; Wrap <img> tag in a <figure> tag (setq org-html-html5-fancy t org-html-doctype "html5") |
Then add the following code in path-to-the-org-presentation/image-size.js and put #+REVEAL_EXTRA_JS: { src: './image-size.js' } at the top of the org file.
1 2 3 4 5 |
var divs = document.querySelectorAll('figure'); for (var i = 0; i < divs.length; i++) { divs[i].classList.add('r-stretch'); } |
With additional js functions revealjs
Adapted from Can we add a config to make all images not overflow? · Issue #388 · yjwen/org-reveal.
-
define some special functions
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667/*https://github.com/yjwen/org-reveal/issues/388*/function update_image_class() {var imgs = document.getElementsByTagName("img");var imgSrcs = [];for (var i = 0; i < imgs.length; i++) {imgs[i].className = "stretch";}}function getRemainingHeightFigureStretch( element, height ) {height = height || 0;if( element ) {var newHeight, oldHeight = element.style.height;// Change the .stretch element height to 0 in order find the height of all// the other elementselement.style.height = '0px';// In Overview mode, the parent (.slide) height is set of 700px.// Restore it temporarily to its natural height.element.parentNode.parentNode.parentNode.style.height = 'auto';newHeight = height - element.parentNode.parentNode.parentNode.offsetHeight;// Restore the old height, just in caseelement.style.height = oldHeight + 'px';// Clear the parent (.slide) height. .removeProperty works in IE9+element.parentNode.parentNode.parentNode.style.removeProperty('height');return newHeight;}return height;}function layoutSlideContentsFigureStretch( width, height ) {// Handle sizing of elements with the 'stretch' classtoArray( dom.slides.querySelectorAll( 'section > div.figure > p > .stretch' ) ).forEach( function( element ) {// Determine how much vertical space we can usevar remainingHeight = getRemainingHeightFigureStretch( element, height );// Consider the aspect ratio of media elementsif( /(img|video)/gi.test( element.nodeName ) ) {var nw = element.naturalWidth || element.videoWidth,nh = element.naturalHeight || element.videoHeight;var es = Math.min( width / nw, remainingHeight / nh );element.style.width = ( nw * es ) + 'px';element.style.height = ( nh * es ) + 'px';}else {element.style.width = width + 'px';element.style.height = remainingHeight + 'px';}} );} -
append the following line in the body of layoutSlideContents():
12layoutSlideContentsFigureStretch(width, height); -
use the usual org-mode image link
12[[file:/tmp/super_high.png]]NOTES: The above two methods prevent the overflow of images. But stretch css won’t stretch the image to maximum.
Redefine the ox-html function to remove the figure and p tags during export.
org-mode export images to html as figures, not img – Emacs Stack Exchange
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
(defun org-html--wrap-image (contents info &optional caption label) "Wrap CONTENTS string within an appropriate environment for images. INFO is a plist used as a communication channel. When optional arguments CAPTION and LABEL are given, use them for caption and \"id\" attribute." (let ((html5-fancy (org-html--html5-fancy-p info))) (format ;; (if html5-fancy "\n<figure%s>%s%s\n</figure>" ;; "\n<div%s class=\"figure\">%s%s\n</div>") ;; ID. (if (org-string-nw-p label) (format " id=\"%s\"" label) "") ;; Contents. ;;(format "\n<p>%s</p>" contents) ;; Caption. (if (not (org-string-nw-p caption)) "" (format (if html5-fancy "\n<figcaption>%s</figcaption>" "\n<p>%s</p>") caption))))) |
insert image link as follows:
1 2 3 |
#+ATTR_HTML: :class stretch [[file:./images/hippo2.png]] |
NOTES: The images will stretch to maximum with this method.
use html attributes to insert images
I use the following format to embed images with stretch class to achieve a full-size view. The problem is that images are NOT encoded inline in base64 format if you want a single file export, also, the images won’t show in a PDF export. It is only good for revealjs presentation in html format. The inliner tool mentioned above is needed to for single-file convertion.
1 2 3 |
#+REVEAL_HTML: <img class="stretch" src="images/hippo1.png"> #+REVEAL: split |
JUL
About the Author:
Beyond 8 hours - Computer, Sports, Family...