Browse Source

ox-latex: Allow toggling centering of images

* lisp/ox-latex.el (latex): Introduce new
  property :latex-images-centered.
(org-latex-images-centered): New variable.
(org-latex--inline-image): Handle new attribute :center, in addition to
variable above.
* doc/org.texi (Images in @LaTeX{} export): Document new behaviour
(Publishing options): Reference :latex-images-centered.
Nicolas Goaziou 3 years ago
parent
commit
096f4287a6
3 changed files with 66 additions and 35 deletions
  1. 12 2
      doc/org.texi
  2. 4 1
      etc/ORG-NEWS
  3. 50 32
      lisp/ox-latex.el

+ 12 - 2
doc/org.texi

@@ -12217,8 +12217,17 @@ To modify the placement option of any floating environment, set the
 [[./img/hst.png]]
 @end example
 
-If the @code{:comment-include} attribute is set to a non-@code{nil} value,
-the @LaTeX{} @code{\includegraphics} macro will be commented out.
+@vindex org-latex-images-centered
+@cindex center image (@LaTeX{} export)
+@cindex image, centering (@LaTeX{} export)
+Images are centered by default.  However, one can disable this behavior by
+setting @code{:center} attribute to @code{nil}.  To prevent any image from
+being centered throughout a document, set @code{org-latex-images-centered}
+instead.
+
+Eventually, if the @code{:comment-include} attribute is set to
+a non-@code{nil} value, the @LaTeX{} @code{\includegraphics} macro will be
+commented out.
 
 @node Plain lists in @LaTeX{} export
 @subsection Plain lists in @LaTeX{} export
@@ -14419,6 +14428,7 @@ however, override everything.
 @item @code{:latex-image-default-height}       @tab @code{org-latex-image-default-height}
 @item @code{:latex-image-default-option}       @tab @code{org-latex-image-default-option}
 @item @code{:latex-image-default-width}        @tab @code{org-latex-image-default-width}
+@item @code{:latex-images-centered}            @tab @code{org-latex-images-centered}
 @item @code{:latex-inactive-timestamp-format}  @tab @code{org-latex-inactive-timestamp-format}
 @item @code{:latex-inline-image-rules}         @tab @code{org-latex-inline-image-rules}
 @item @code{:latex-link-with-unknown-path-format} @tab @code{org-latex-link-with-unknown-path-format}

+ 4 - 1
etc/ORG-NEWS

@@ -204,7 +204,10 @@ point for the SRC/EXAMPLE block.
     (message "This is line 32")
   ,#+END_SRC
 #+END_SRC
-
+**** Allow toggling center for images in LaTeX export
+With the global variable ~org-latex-images-centered~ or the local
+attribute ~:center~ it is now possible to center an image in LaTeX
+export.
 *** Babel
 **** Support for SLY in Lisp blocks
 See ~org-babel-lisp-eval-fn~ to activate it.

+ 50 - 32
lisp/ox-latex.el

@@ -127,6 +127,7 @@
     (:latex-image-default-height nil nil org-latex-image-default-height)
     (:latex-image-default-option nil nil org-latex-image-default-option)
     (:latex-image-default-width nil nil org-latex-image-default-width)
+    (:latex-images-centered nil nil org-latex-images-centered)
     (:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format)
     (:latex-inline-image-rules nil nil org-latex-inline-image-rules)
     (:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format)
@@ -681,6 +682,14 @@ The function result will be used in the section format string."
 
 ;;;; Links
 
+(defcustom org-latex-images-centered t
+  "When non-nil, images are centered."
+  :group 'org-export-latex
+  :version "25.1"
+  :package-version '(Org . "9.0")
+  :type 'boolean
+  :safe #'booleanp)
+
 (defcustom org-latex-image-default-option ""
   "Default option for images."
   :group 'org-export-latex
@@ -2269,13 +2278,12 @@ used as a communication channel."
 		  (cond ((string= float "wrap") 'wrap)
 			((string= float "sideways") 'sideways)
 			((string= float "multicolumn") 'multicolumn)
+			((and (plist-member attr :float) (not float)) 'nonfloat)
 			((or float
 			     (org-element-property :caption parent)
 			     (org-string-nw-p (plist-get attr :caption)))
-			 (if (and (plist-member attr :float) (not float))
-			     'nonfloat
-			   'figure))
-			((and (not float) (plist-member attr :float)) nil))))
+			 'figure)
+			(t 'nonfloat))))
 	 (placement
 	  (let ((place (plist-get attr :placement)))
 	    (cond
@@ -2284,6 +2292,9 @@ used as a communication channel."
 	     ((eq float 'figure)
 	      (format "[%s]" (plist-get info :latex-default-figure-position)))
 	     (t ""))))
+	 (center
+	  (if (plist-member attr :center) (plist-get attr :center)
+	    (plist-get info :latex-images-centered)))
 	 (comment-include (if (plist-get attr :comment-include) "%" ""))
 	 ;; It is possible to specify width and height in the
 	 ;; ATTR_LATEX line, and also via default variables.
@@ -2334,8 +2345,8 @@ used as a communication channel."
       (setq image-code
 	    (format "\\includegraphics%s{%s}"
 		    (cond ((not (org-string-nw-p options)) "")
-			  ((= (aref options 0) ?,)
-			   (format "[%s]"(substring options 1)))
+			  ((string-prefix-p "," options)
+			   (format "[%s]" (substring options 1)))
 			  (t (format "[%s]" options)))
 		    path))
       (when (equal filetype "svg")
@@ -2348,46 +2359,53 @@ used as a communication channel."
 						   image-code
 						   nil t))))
     ;; Return proper string, depending on FLOAT.
-    (cl-case float
-      (wrap (format "\\begin{wrapfigure}%s
-%s\\centering
+    (pcase float
+      (`wrap (format "\\begin{wrapfigure}%s
+%s%s
 %s%s
 %s\\end{wrapfigure}"
-		    placement
-		    (if caption-above-p caption "")
-		    comment-include image-code
-		    (if caption-above-p "" caption)))
-      (sideways (format "\\begin{sidewaysfigure}
-%s\\centering
+		     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 "")
-			comment-include image-code
-			(if caption-above-p "" caption)))
-      (multicolumn (format "\\begin{figure*}%s
-%s\\centering
+			 (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 "")
-			   comment-include image-code
-			   (if caption-above-p "" caption)))
-      (figure (format "\\begin{figure}%s
-%s\\centering
+			    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 "")
-		      comment-include image-code
-		      (if caption-above-p "" caption)))
-      (nonfloat
+		       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)))
-      (otherwise image-code))))
+      (_
+       (concat (if caption-above-p caption "")
+	       image-code
+	       (if caption-above-p caption ""))))))
 
 (defun org-latex-link (link desc info)
   "Transcode a LINK object from Org to LaTeX.