emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] [PATCH] ox-latex: Support tikz images, :width, and :height


From: Aaron Ecay
Subject: [O] [PATCH] ox-latex: Support tikz images, :width, and :height
Date: Tue, 5 Mar 2013 23:04:42 -0500

* ox-latex.el:
(org-latex-image-default-option): Change default value to ""
(org-latex-image-default-width)
(org-latex-image-default-height): Add variables
(org-latex-inline-image-rules): Make .tikz files as exportable with
latex
(org-latex--inline-image): Support tikz images.  Also support separate
:width and :height parameters for images.

* ob-R.el (org-babel-R-construct-graphics-device-call): Change file
extension of tikz graphics files to .tikz

Tikz graphics should be exported to LaTeX by \include, not as a link.
This commit changes the file extension used for tikz graphics from .tex
to .tikz, and inserts code for including such images.  The :options for
tikz graphics are passed as an optional argument to a tikzpicture
environment.

Also provide :width and :height ATTR_LATEX entries for images.  For tikz
graphics, these are implemented with \resizebox; for other image types
they are inserted in the optional arguments to \includegraphics.
---
 lisp/ob-R.el     |   2 +-
 lisp/ox-latex.el | 111 ++++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 83 insertions(+), 30 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 8db0853..9875f81 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -237,7 +237,7 @@ current code buffer."
         '((:bmp . "bmp")
           (:jpg . "jpeg")
           (:jpeg . "jpeg")
-          (:tex . "tikz")
+          (:tikz . "tikz")
           (:tiff . "tiff")
           (:png . "png")
           (:svg . "svg")
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 8a5b6a6..47b8bb1 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -77,15 +77,19 @@
 ;; (i.e. "inparaenum").  The second one allows to specify optional
 ;; arguments for that environment (square brackets are not mandatory).
 ;;
-;; Images accept `:float', `:placement', `:comment-include', and
-;; `:options' as attributes.  `:float' accepts a symbol among `wrap',
-;; `multicolumn', and `figure', which defines the float environment
-;; for the table (if unspecified, an image with a caption will be set
-;; in a "figure" environment).  `:comment-include' is a boolean that
-;; toggles whether to comment out the \includegraphics
-;; call.  `:placement' is a string that will be used as argument for
-;; the environment chosen.  `:options' is a string that will be used
-;; as the optional argument for "includegraphics" macro.
+;; Images accept `:float', `:placement', `:comment-include', `:width',
+;; and `:height', and `:options' as attributes.  `:float' accepts a
+;; symbol among `wrap', `multicolumn', and `figure', which defines the
+;; float environment for the image (if unspecified, an image with a
+;; caption will be set in a "figure" environment).  `:comment-include'
+;; is a boolean that toggles whether to comment out the code which
+;; actually includes the image. `:placement' is a string that will be
+;; used as argument for the environment chosen.  `:width' and
+;; `:height' control the width and height of the image.  `:options' is
+;; a string that will be used as the optional argument for
+;; "includegraphics" macro or (in the case of tikz images), used as
+;; the optional argument for a `tikzpicture' environment which will
+;; surround the "\input" picture code.
 ;;
 ;; Special blocks accept `:options' as attribute.  Its value will be
 ;; appended as-is to the opening string of the environment created.
@@ -472,18 +476,28 @@ which format headlines like for Org version prior to 8.0."
 
 ;;;; Links
 
-(defcustom org-latex-image-default-option "width=.9\\linewidth"
+(defcustom org-latex-image-default-option ""
   "Default option for images."
   :group 'org-export-latex
   :type 'string)
 
+(defcustom org-latex-image-default-width ".9\\linewidth"
+  "Default width for images."
+  :group 'org-export-latex
+  :type 'string)
+
+(defcustom org-latex-image-default-height ""
+  "Default height for images."
+  :group 'org-export-latex
+  :type 'string)
+
 (defcustom org-latex-default-figure-position "htb"
   "Default position for latex figures."
   :group 'org-export-latex
   :type 'string)
 
 (defcustom org-latex-inline-image-rules
-  '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
+  '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\)\\'"))
   "Rules characterizing image files that can be inlined into LaTeX.
 
 A rule consists in an association whose key is the type of link
@@ -1751,6 +1765,7 @@ used as a communication channel."
         (path (let ((raw-path (org-element-property :path link)))
                 (if (not (file-name-absolute-p raw-path)) raw-path
                   (expand-file-name raw-path))))
+        (filetype (file-name-extension path))
         (caption (org-latex--caption/label-string parent info))
         ;; Retrieve latex attributes from the element around.
         (attr (org-export-read-attribute :attr_latex parent))
@@ -1768,32 +1783,70 @@ used as a communication channel."
                   (format "[%s]" org-latex-default-figure-position))
                  (t ""))))
         (comment-include (if (plist-get attr :comment-include) "%" ""))
-        ;; Options for "includegraphics" macro. Make sure it is
-        ;; a string with square brackets when non empty.  Default to
-        ;; `org-latex-image-default-option' when possible.
-        (options (let ((opt (format "%s"
-                                    (or (plist-get attr :options)
-                                        org-latex-image-default-option))))
-                   (cond ((string-match "\\`\\[.*\\]" opt) opt)
-                         ((org-string-nw-p opt) (format "[%s]" opt))
-                         ((eq float 'float) "[width=0.7\\textwidth]")
-                         ((eq float 'wrap) "[width=0.48\\textwidth]")
-                         (t "")))))
+        ;; It is possible to specify width and height in the
+        ;; ATTR_LATEX line, and also via default variables.
+        (width (format "%s" (or (plist-get attr :width)
+                                (cond
+                                 ((eq float 'float) "0.7\\textwidth")
+                                 ((eq float 'wrap) "0.48\\textwidth")
+                                 (t org-latex-image-default-width)))))
+        (height (format "%s" (or (plist-get attr :height)
+                                 org-latex-image-default-height)))
+        (options (or (plist-get attr :options)
+                     org-latex-image-default-option))
+        image-code)
+    (if (not options)
+       (setq options "")
+      (setq options (format "%s" options)))
+    (when (string-match "\\`\\[\\(.*\\)\\]\\'" options)
+      (setq options (match-string 1 options)))
+    (if (equal filetype "tikz")
+       ;; For tikz images:
+       ;; - use \input to read in image file
+       ;; - if options are present, wrap in a tikzpicture environment
+       ;; - if width or height are present, use \resizebox to change
+       ;;   the image size
+       (progn
+         (setq image-code (format "\\input{%s}" path))
+         (when (and (org-string-nw-p options))
+           (setq image-code (format 
"\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
+                                    options
+                                    image-code)))
+         (when (or (and (org-string-nw-p width) (org-not-nil width))
+                   (and (org-string-nw-p height) (org-not-nil height)))
+           (setq image-code (format "\\resizebox{%s}{%s}{%s}"
+                                    (if (and (org-string-nw-p width) 
(org-not-nil width))
+                                        width
+                                      "!")
+                                    (if (and (org-string-nw-p height) 
(org-not-nil height))
+                                        height
+                                      "!")
+                                    image-code))))
+      ;; For other images:
+      ;; - add width and height to options
+      ;; - include the image with \includegraphics
+      (when (and (org-string-nw-p width) (org-not-nil width))
+       (setq options (concat options ",width=" width)))
+      (when (and (org-string-nw-p height) (org-not-nil height))
+       (setq options (concat options ",height=" height)))
+      (when (= (aref options 0) ?,)
+       (setq options (substring options 1)))
+      (setq image-code (format "\\includegraphics[%s]{%s}" options path)))
     ;; Return proper string, depending on FLOAT.
     (case float
       (wrap (format "\\begin{wrapfigure}%s
 \\centering
-%s\\includegraphics%s{%s}
-%s\\end{wrapfigure}" placement comment-include options path caption))
+%s%s
+%s\\end{wrapfigure}" placement comment-include image-code caption))
       (multicolumn (format "\\begin{figure*}%s
 \\centering
-%s\\includegraphics%s{%s}
-%s\\end{figure*}" placement comment-include options path caption))
+%s%s
+%s\\end{figure*}" placement comment-include image-code caption))
       (figure (format "\\begin{figure}%s
 \\centering
-%s\\includegraphics%s{%s}
-%s\\end{figure}" placement comment-include options path caption))
-      (t (format "\\includegraphics%s{%s}" options path)))))
+%s%s
+%s\\end{figure}" placement comment-include image-code caption))
+      (t image-code))))
 
 (defun org-latex-link (link desc info)
   "Transcode a LINK object from Org to LaTeX.
-- 
1.8.1.5




reply via email to

[Prev in Thread] Current Thread [Next in Thread]