This answer presents a method that does not need LaTeX but it needs SVG support.
The following Elisp snippet defines a new major mode
txtimg that behaves just like
A babel execution method for
txtimg-mode makes that mode special.
If you type C-c C-c on an
imgtxt source code block and the newline before it the block are covered with an svg image displaying the text.
You can copy the code to your init file if you like it.
;; -*- lexical-binding: t -*-
(defun text-to-img (txt &optional scale-width)
"Create a svg image containing TXT.
The internally calculated text width is only an approximation.
If the image does not have the right width use the float number
SCALE-WIDTH which defaults to 1.0."
(unless scale-width (setq scale-width 1.0))
(let* ((line 0)
(font-family (last (split-string (aref (x-decompose-font-name (frame-parameter (selected-frame) 'font)) 0) "-")))
(lines (split-string txt "\n"))
(line-height (* 1.2 char-height))
(width (ceiling (* scale-width char-width (apply #'max (mapcar #'length lines)))))
(height (* line-height (length lines)))
(svg (svg-create (ceiling width) (ceiling height))))
(lambda (txt) (svg-text svg txt
:font-size (number-to-string char-height)
:y (number-to-string (floor (* line-height (incf line))))))
(define-derived-mode txtimg-mode text-mode "txtimg"
"Helper mode for org-babel.
Just ordinary `text-mode' with another name.")
(define-key txtimg-mode-map (kbd "C-c C-c") #'org-babel-execute:txtimg)
(defun org-babel-execute:txtimg (&optional body params)
"Put an txtimg overlay on the source block with BODY and PARAMS.
If there is already a txtimg overlay remove it.
The txtimg overlay displays the image containing the text."
(let ((ol (cl-loop for ol being the overlays from (point) to (1+ (point))
if (overlay-get ol 'txtimg)
(when (and body params)
(let* ((scale-width (plist-get params :scale-width))
(b (max 1 (1- (save-excursion
(goto-char (org-element-property :begin el))
(goto-char (org-element-property :end el))
(ol (make-overlay b e)))
(overlay-put ol 'txtimg t)
(overlay-put ol 'keymap txtimg-mode-map)
(overlay-put ol 'display
(text-to-img body scale-width)))))))
The the following example org source code is displayed as the image below it after pressing C-c C-c on the
#+BEGIN_SRC txtimg :results none
This text should be displayed to the
right of the image above.
Ideally, this would work for multiple lines,
but if it's just one long wrapped line,
that would be find too.
After the image, text should again take the
full width of the line...
You can transform all
txtimg source blocks to images at opening an Orgmode file with the following
(defun org-imgtxt-hook-fun ()
"Process all imgtxt blocks of an orgmode buffer.
You can put this in `org-mode-hook' if you like."
(when (eq (org-element-type (org-element-context)) 'src-block)
(add-hook 'org-mode-hook #'org-imgtxt-hook-fun)