Hi

full image inlining patch with tests

Best, /PA

On Thu, 6 Mar 2025 at 19:39, Ihor Radchenko <yanta...@posteo.net> wrote:

> Pedro Andres Aranda Gutierrez <paag...@gmail.com> writes:
>
> > good to know. So, is there any other case apart that we should look at
> > before deciding if the quick patch I prepared is good enough? Or should I
> > start preparing the test case?
>
> I can't think of other special cases.
> Which quick patch are you talking about?
>
> --
> Ihor Radchenko // yantar92,
> Org mode maintainer,
> Learn more about Org mode at <https://orgmode.org/>.
> Support Org development at <https://liberapay.com/org-mode>,
> or support my work at <https://liberapay.com/yantar92>
>


-- 
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 7fac86c69cbfe66e458ea11fa9a9d7e0ddd911fa Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <paag...@gmail.com>
Date: Fri, 7 Mar 2025 07:59:56 +0100
Subject: [PATCH] Fix image inlining

---

* lisp/ox-latex.el: (org-latex--inline-image): Don't add extra formatting
when image reference has no attributes attached.

* testing/lisp/test-ox-latex.el: (image-inline): Add images without attributes
standalone, in a text and in a table to test new behaviour.

 etc/ORG-NEWS                  | 26 ++++++++++++
 lisp/ox-latex.el              |  6 ++-
 testing/lisp/test-ox-latex.el | 79 ++++++++++++++++++++++++++++++-----
 3 files changed, 98 insertions(+), 13 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 332586f4f..b126d501a 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -133,6 +133,32 @@ to your configuration.
 The new behaviour follows general babel backend rules (auto-detecting
 result type), but may affect the existing usage.

+*** Inlined images are imported *as is* by the LaTeX and derived exporters
+
+Take this line in an org file:
+
+~This is an inlined file:image.png image~
+
+Previously, the Latex exporter would take the image, place it in a
+~center~ environment and scale its width to 90% of the text width,
+generating the following code:
+
+#+BEGIN_SRC latex
+This is an inlined \begin{center}
+  \includegraphics[width=9.\textwidth]{image.png}
+  \end{center} image
+#+END_SRC
+
+In the new behaviour, it will just generate a plain
+
+#+begin_export LaTeX
+This is an inlined \includegraphics{image.png} image
+#+end_export
+
+In this case, the responsibility of providing an image with the
+correct size lies on you.
+
+
 ** New features

 # We list the most important features, and the features that may
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index af3ac308b..95692d972 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2955,7 +2955,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 ((null attr) "") ;; this is a plain image with no attrs
+                          ((not (org-string-nw-p options)) "")
 			  ((string-prefix-p "," options)
 			   (format "[%s]" (substring options 1)))
 			  (t (format "[%s]" options)))
@@ -3023,7 +3024,8 @@ used as a communication channel."
 		       (if center "\\centering" "")
 		       comment-include image-code
 		       (if caption-above-p "" caption)))
-      ((guard center)
+      ((guard (and (not (null attr)) center))
+       ;; if we have attributes that demand centering
        (format "\\begin{center}
 %s%s
 %s\\end{center}"
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index 04164767c..90c445995 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -143,17 +143,6 @@ Column & Column \\\\
       "\\hline\\multicolumn{2}{r}{Continued on next page} \\\\
 \\endfoot"))))

-(ert-deftest test-ox-latex/inline-image ()
-  "Test inline images."
-  (org-test-with-exported-text
-      'latex
-      "#+caption: Schematic
-[[https://orgmode.org/worg/images/orgmode/org-mode-unicorn.svg][file:/wallpaper.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}}";))))
-
 (ert-deftest test-ox-latex/num-t ()
   "Test toc treatment for fixed num:t"
   (org-test-with-exported-text
@@ -272,5 +261,73 @@ is suppressed
       (should (search-forward "}
 \\addcontentsline{toc}{section}{Section 3}")))))

+(ert-deftest test-ox-latex/image-inline ()
+  "test new \\includegraphics{} treatment"
+  (org-test-with-exported-text
+   'latex
+   "* Intro
+
+This is a simple document to reflect how we want to treat images in
+~ox-latex.el~.
+
+** Paragraph without anything but a reference to an image
+
+file:test-img.png
+
+** Paragraph with inlined image
+
+This is a paragraph with file:test-small.png inlined
+
+** Table with inlined images
+
+This would be a table with inlined images:
+
+| file:test-small.png | file:test-small.png |
+
+And this would be a more sophisticated table, with attributes that should *not*
+impact on how the images are inlined:
+
+#+ATTR_LATEX: :environment longtable :align l|p{3cm}|l
+|          | Image 1             | Image2              |
+|----------+---------------------+---------------------|
+| Complex: | file:test-small.png | file:test-small.png |
+
+** Image with LaTeX attributes
+
+#+attr_latex: :width 300px
+file:test-img.png
+"
+   (goto-char (point-min))
+   (should (search-forward
+            "\\begin{document}" nil t))
+   (should (search-forward "
+\\subsection{Paragraph" nil t))
+   (should (search-forward "
+\\includegraphics{test-img.png}" nil t))
+   (should (search-forward "
+\\subsection{Paragraph" nil t))
+   (should (search-forward
+            "a paragraph with \\includegraphics{test-small.png} inlined" nil t))
+   (should (search-forward
+            "\\subsection{Table with inlined images}" nil t))
+   (should (search-forward
+            "\\begin{center}
+\\begin{tabular}{ll}
+\\includegraphics{test-small.png} & \\includegraphics{test-small.png}\\\\
+\\end{tabular}
+\\end{center}" nil t))
+   (should (search-forward
+            "
+\\begin{longtable}{l|p{3cm}|l}
+ & Image 1 & Image2\\\\
+\\hline" nil t))
+   (should (search-forward "\\hline
+Complex: & \\includegraphics{test-small.png} & \\includegraphics{test-small.png}\\\\
+\\end{longtable}" nil t))
+   (should (search-forward "\\begin{center}
+\\includegraphics[width=300px]{test-img.png}
+\\end{center}
+\\end{document}" nil t))))
+
 (provide 'test-ox-latex)
 ;;; test-ox-latex.el ends here
--
2.34.1

Reply via email to