Hi,

attached is a patch that will inline images and only apply attributes to
standalone images.
Sorry, but I've lost the original thread to answer it.

Best, /PA

-- 
Fragen sind nicht da, um beantwortet zu werden,
Fragen sind da um gestellt zu werden
Georg Kreisler

Sagen's Paradeiser, write BE!
Year 1 of the New Koprocracy
From b1d7cd0dd210a727e67bc65bed0d621b2387579c Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <paag...@gmail.com>
Date: Thu, 27 Mar 2025 10:24:10 +0100
Subject: [PATCH] Alternative image handling

---


* lisp/ox-latex.el: Use ~org-html-standalone-image-p~ from ox-html to
check whether an image reference is standalone or embedded in text or
tables. If the image is *not* standalone, just generate
\includegraphics{image}

* testing/lisp/test-ox-latex.el: A new test for the \includegraphics{}
generation logic.

 lisp/ox-latex.el              | 100 +++++++++++++++++++---------------
 testing/lisp/test-ox-latex.el |  20 ++++++-
 2 files changed, 73 insertions(+), 47 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index af3ac308b..aaf65494e 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -33,6 +33,7 @@
 (require 'cl-lib)
 (require 'ox)
 (require 'ox-publish)
+(require 'ox-html)

 ;;; Function Declarations

@@ -49,6 +50,14 @@
 (defvar engrave-faces-current-preset-style)
 (defvar engrave-faces-latex-mathescape)

+(defalias 'ox-latex-standalone-image-p 'org-html-standalone-image-p
+  "Reuse org-html-standalone-p to check whether an image is standalone or not.
+
+In ox-html.el:
+  (defun org-html-standalone-image-p (element info))
+
+  Non-nil if ELEMENT is a standalone image.
+  INFO is a plist holding contextual information.")
 
 ;;; Define Backend

@@ -2955,7 +2964,8 @@ used as a communication channel."
           (setq options (concat options ",page=" search-option))))
       (setq image-code
 	    (format "\\includegraphics%s{%s}"
-		    (cond ((not (org-string-nw-p options)) "")
+		    (cond ((not (ox-latex-standalone-image-p link info)) "")
+                          ((not (org-string-nw-p options)) "")
 			  ((string-prefix-p "," options)
 			   (format "[%s]" (substring options 1)))
 			  (t (format "[%s]" options)))
@@ -2974,66 +2984,68 @@ used as a communication channel."
 						   "}"
 						   image-code
 						   nil t))))
-    ;; Return proper string, depending on FLOAT.
-    (pcase float
-      ((and (pred stringp) env-string)
-       (format "\\begin{%s}%s
+    ;; Return proper string, depending on FLOAT **only if standalone**.
+    (if (ox-latex-standalone-image-p link info)
+        (pcase float
+          ((and (pred stringp) env-string)
+           (format "\\begin{%s}%s
 %s%s
 %s%s
 %s\\end{%s}"
-               env-string
-               placement
-               (if caption-above-p caption "")
-               (if center "\\centering" "")
-               comment-include image-code
-               (if caption-above-p "" caption)
-               env-string))
-      (`wrap (format "\\begin{wrapfigure}%s
+                   env-string
+                   placement
+                   (if caption-above-p caption "")
+                   (if center "\\centering" "")
+                   comment-include image-code
+                   (if caption-above-p "" caption)
+                   env-string))
+          (`wrap (format "\\begin{wrapfigure}%s
 %s%s
 %s%s
 %s\\end{wrapfigure}"
-		     placement
-		     (if caption-above-p caption "")
-		     (if center "\\centering" "")
-		     comment-include image-code
-		     (if caption-above-p "" caption)))
-      (`sideways (format "\\begin{sidewaysfigure}
+		         placement
+		         (if caption-above-p caption "")
+		         (if center "\\centering" "")
+		         comment-include image-code
+		         (if caption-above-p "" caption)))
+          (`sideways (format "\\begin{sidewaysfigure}
 %s%s
 %s%s
 %s\\end{sidewaysfigure}"
-			 (if caption-above-p caption "")
-			 (if center "\\centering" "")
-			 comment-include image-code
-			 (if caption-above-p "" caption)))
-      (`multicolumn (format "\\begin{figure*}%s
+			     (if caption-above-p caption "")
+			     (if center "\\centering" "")
+			     comment-include image-code
+			     (if caption-above-p "" caption)))
+          (`multicolumn (format "\\begin{figure*}%s
 %s%s
 %s%s
 %s\\end{figure*}"
-			    placement
-			    (if caption-above-p caption "")
-			    (if center "\\centering" "")
-			    comment-include image-code
-			    (if caption-above-p "" caption)))
-      (`figure (format "\\begin{figure}%s
+			        placement
+			        (if caption-above-p caption "")
+			        (if center "\\centering" "")
+			        comment-include image-code
+			        (if caption-above-p "" caption)))
+          (`figure (format "\\begin{figure}%s
 %s%s
 %s%s
 %s\\end{figure}"
-		       placement
-		       (if caption-above-p caption "")
-		       (if center "\\centering" "")
-		       comment-include image-code
-		       (if caption-above-p "" caption)))
-      ((guard center)
-       (format "\\begin{center}
+		           placement
+		           (if caption-above-p caption "")
+		           (if center "\\centering" "")
+		           comment-include image-code
+		           (if caption-above-p "" caption)))
+          ((guard center)
+           (format "\\begin{center}
 %s%s
 %s\\end{center}"
-	       (if caption-above-p caption "")
-	       image-code
-	       (if caption-above-p "" caption)))
-      (_
-       (concat (if caption-above-p caption "")
-	       image-code
-	       (if caption-above-p caption ""))))))
+	           (if caption-above-p caption "")
+	           image-code
+	           (if caption-above-p "" caption)))
+          (_
+           (concat (if caption-above-p caption "")
+	           image-code
+	           (if caption-above-p caption ""))))
+      image-code))) ;; else return just the \includegraphics{}

 (defun org-latex-link (link desc info)
   "Transcode a LINK object from Org to LaTeX.
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index 04164767c..f4e6a97d1 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -147,12 +147,26 @@ Column & Column \\\\
   "Test inline images."
   (org-test-with-exported-text
       'latex
-      "#+caption: Schematic
-[[https://orgmode.org/worg/images/orgmode/org-mode-unicorn.svg][file:/wallpaper.png]]";
+      "
+This is an inline image [[file:./test.png]].
+
+| [[file:./test.png]]  | [[file:./test.jpg]] |
+
+#+attr_latex: :width 300px
+file:test-img.png
+
+"
     (goto-char (point-min))
     (should
      (search-forward
-      "\\href{https://orgmode.org/worg/images/orgmode/org-mode-unicorn.svg}{\\includegraphics[width=.9\\linewidth]{/wallpaper.png}}";))))
+      "This is an inline image \\includegraphics{./test.png}" nil t))
+    (should
+     (search-forward
+      "\\includegraphics{./test.png} & \\includegraphics{./test.jpg}\\\\" nil t))
+    (should
+     (search-forward
+      "\\includegraphics[width=300px]{test-img.png}" nil t))
+    ))

 (ert-deftest test-ox-latex/num-t ()
   "Test toc treatment for fixed num:t"
--
2.34.1

Reply via email to