]> code.delx.au - gnu-emacs/blobdiff - lisp/org/ox-latex.el
Update copyright year to 2016
[gnu-emacs] / lisp / org / ox-latex.el
index 3609881b690c9ce3f827f566cc67bcd0f5199412..2734f90db06f19e07e3b30ff1991140aa12e8410 100644 (file)
@@ -1,6 +1,6 @@
 ;;; ox-latex.el --- LaTeX Back-End for Org Export Engine
 
-;; Copyright (C) 2011-201 Free Software Foundation, Inc.
+;; Copyright (C) 2011-2016 Free Software Foundation, Inc.
 
 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
 ;; Keywords: outlines, hypermedia, calendar, wp
 If #+LATEX_CLASS is set in the buffer, use its value and the
 associated information.  Here is the structure of each cell:
 
-  \(class-name
+  (class-name
     header-string
-    \(numbered-section . unnumbered-section)
+    (numbered-section . unnumbered-section)
     ...)
 
 The header string
@@ -281,11 +281,11 @@ section string and will be replaced by the title of the section.
 Instead of a cons cell (numbered . unnumbered), you can also
 provide a list of 2 or 4 elements,
 
-  \(numbered-open numbered-close)
+  (numbered-open numbered-close)
 
 or
 
-  \(numbered-open numbered-close unnumbered-open unnumbered-close)
+  (numbered-open numbered-close unnumbered-open unnumbered-close)
 
 providing opening and closing strings for a LaTeX environment
 that should represent the document section.  The opening clause
@@ -524,7 +524,7 @@ When nil, no transformation is made."
   :package-version '(Org . "8.0")
   :type '(choice
          (string :tag "Format string")
-         (const :tag "No formatting")))
+         (const :tag "No formatting" nil)))
 
 
 ;;;; Text markup
@@ -589,20 +589,20 @@ The function must accept six parameters:
 The function should return the string to be exported.
 
 For example, the variable could be set to the following function
-in order to mimic default behaviour:
+in order to mimic default behavior:
 
-\(defun org-latex-format-inlinetask \(todo type priority name tags contents\)
+\(defun org-latex-format-inlinetask (todo type priority name tags contents)
 \"Format an inline task element for LaTeX export.\"
-  \(let ((full-title
-        \(concat
-         \(when todo
-            \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
-         \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
+  (let ((full-title
+        (concat
+         (when todo
+            (format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
+         (when priority (format \"\\\\framebox{\\\\#%c} \" priority))
          title
-         \(when tags
-            \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
-                    \(mapconcat 'identity tags \":\")))))
-    \(format (concat \"\\\\begin{center}\\n\"
+         (when tags
+            (format \"\\\\hfill{}\\\\textsc{:%s:}\"
+                    (mapconcat \\='identity tags \":\")))))
+    (format (concat \"\\\\begin{center}\\n\"
                    \"\\\\fbox{\\n\"
                    \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
                    \"%s\\n\\n\"
@@ -626,21 +626,21 @@ listings package, and if you want to have color, the color
 package.  Just add these to `org-latex-packages-alist', for
 example using customize, or with something like:
 
-  \(require 'ox-latex)
-  \(add-to-list 'org-latex-packages-alist '(\"\" \"listings\"))
-  \(add-to-list 'org-latex-packages-alist '(\"\" \"color\"))
+  (require \\='ox-latex)
+  (add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
+  (add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
 
 Alternatively,
 
-  \(setq org-latex-listings 'minted)
+  (setq org-latex-listings \\='minted)
 
 causes source code to be exported using the minted package as
 opposed to listings.  If you want to use minted, you need to add
 the minted package to `org-latex-packages-alist', for example
 using customize, or with
 
-  \(require 'ox-latex)
-  \(add-to-list 'org-latex-packages-alist '(\"\" \"minted\"))
+  (require \\='ox-latex)
+  (add-to-list \\='org-latex-packages-alist \\='(\"\" \"minted\"))
 
 In addition, it is necessary to install pygments
 \(http://pygments.org), and to configure the variable
@@ -736,8 +736,8 @@ These options are supplied within square brackets in
 be a list containing two strings: the name of the option, and the
 value.  For example,
 
-  \(setq org-latex-minted-options
-    '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
+  (setq org-latex-minted-options
+    '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
 
 will result in src blocks being exported with
 
@@ -757,8 +757,8 @@ options will be applied to blocks of all languages."
 It is used during export of src blocks by the listings and minted
 latex packages.  For example,
 
-  \(setq org-latex-custom-lang-environments
-     '\(\(python \"pythoncode\"\)\)\)
+  (setq org-latex-custom-lang-environments
+     '((python \"pythoncode\")))
 
 would have the effect that if org encounters begin_src python
 during latex export it will output
@@ -998,7 +998,9 @@ See `org-latex-text-markup-alist' for details."
      ;; and use "\\verb" command.
      ((eq 'verb fmt)
       (let ((separator (org-latex--find-verb-separator text)))
-       (concat "\\verb" separator text separator)))
+       (concat "\\verb" separator
+               (replace-regexp-in-string "\n" " " text)
+               separator)))
      ;; Handle the `protectedtexttt' special case: Protect some
      ;; special chars and use "\texttt{%s}" format string.
      ((eq 'protectedtexttt fmt)
@@ -1342,7 +1344,16 @@ holding contextual information."
               ((= (length sec) 4)
                (if numberedp (concat (car sec) "\n%s" (nth 1 sec))
                  (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
-          (text (org-export-data (org-element-property :title headline) info))
+          ;; Create a temporary export back-end that hard-codes
+          ;; "\underline" within "\section" and alike.
+          (section-back-end
+           (org-export-create-backend
+            :parent 'latex
+            :transcoders
+            '((underline . (lambda (o c i) (format "\\underline{%s}" c))))))
+          (text
+           (org-export-data-with-backend
+            (org-element-property :title headline) section-back-end info))
           (todo
            (and (plist-get info :with-todo-keywords)
                 (let ((todo (org-element-property :todo-keyword headline)))
@@ -1374,7 +1385,13 @@ holding contextual information."
                  (when (org-export-first-sibling-p headline info)
                    (format "\\begin{%s}\n" (if numberedp 'enumerate 'itemize)))
                  ;; Itemize headline
-                 "\\item " full-text "\n" headline-label pre-blanks contents)))
+                 "\\item"
+                 (and full-text (org-string-match-p "\\`[ \t]*\\[" full-text)
+                      "\\relax")
+                 " " full-text "\n"
+                 headline-label
+                 pre-blanks
+                 contents)))
            ;; If headline is not the last sibling simply return
            ;; LOW-LEVEL-BODY.  Otherwise, also close the list, before
            ;; any blank line.
@@ -1389,8 +1406,9 @@ holding contextual information."
        (let ((opt-title
               (funcall org-latex-format-headline-function
                        todo todo-type priority
-                       (org-export-data
-                        (org-export-get-alt-title headline info) info)
+                       (org-export-data-with-backend
+                        (org-export-get-alt-title headline info)
+                        section-back-end info)
                        (and (eq (plist-get info :with-tags) t) tags))))
          (if (and numberedp opt-title
                   (not (equal opt-title full-text))
@@ -1458,7 +1476,7 @@ contextual information."
       (let* ((org-lang (org-element-property :language inline-src-block))
             (mint-lang (or (cadr (assq (intern org-lang)
                                        org-latex-minted-langs))
-                           org-lang))
+                           (downcase org-lang)))
             (options (org-latex--make-option-string
                       org-latex-minted-options)))
        (concat (format "\\mint%s{%s}"
@@ -1508,17 +1526,16 @@ holding contextual information."
               (when priority (format "\\framebox{\\#%c} " priority))
               title
               (when tags (format "\\hfill{}\\textsc{:%s:}"
-                                 (mapconcat 'identity tags ":"))))))
-        (format (concat "\\begin{center}\n"
-                        "\\fbox{\n"
-                        "\\begin{minipage}[c]{.6\\textwidth}\n"
-                        "%s\n\n"
-                        "\\rule[.8em]{\\textwidth}{2pt}\n\n"
-                        "%s"
-                        "\\end{minipage}\n"
-                        "}\n"
-                        "\\end{center}")
-                full-title contents))))))
+                                 (mapconcat #'identity tags ":"))))))
+        (concat "\\begin{center}\n"
+                "\\fbox{\n"
+                "\\begin{minipage}[c]{.6\\textwidth}\n"
+                full-title "\n\n"
+                (and (org-string-nw-p contents)
+                     (concat "\\rule[.8em]{\\textwidth}{2pt}\n\n" contents))
+                "\\end{minipage}\n"
+                "}\n"
+                "\\end{center}"))))))
 
 
 ;;;; Italic
@@ -1564,7 +1581,25 @@ contextual information."
                (and tag (format "[{%s}] "
                                 (concat checkbox
                                         (org-export-data tag info)))))))
-    (concat counter "\\item" (or tag (concat " " checkbox))
+    (concat counter
+           "\\item"
+           (cond
+            (tag)
+            (checkbox (concat " " checkbox))
+            ;; Without a tag or a check-box, if CONTENTS starts with
+            ;; an opening square bracket, add "\relax" to "\item",
+            ;; unless the brackets comes from an initial export
+            ;; snippet (i.e. it is inserted willingly by the user).
+            ((and contents
+                  (org-string-match-p "\\`[ \t]*\\[" contents)
+                  (not (let ((e (car (org-element-contents item))))
+                         (and (eq (org-element-type e) 'paragraph)
+                              (let ((o (car (org-element-contents e))))
+                                (and (eq (org-element-type o) 'export-snippet)
+                                     (eq (org-export-snippet-backend o)
+                                         'latex)))))))
+             "\\relax ")
+            (t " "))
            (and contents (org-trim contents))
            ;; If there are footnotes references in tag, be sure to
            ;; add their definition at the end of the item.  This
@@ -1754,7 +1789,8 @@ DESC is the description part of the link, or the empty string.
 INFO is a plist holding contextual information.  See
 `org-export-data'."
   (let* ((type (org-element-property :type link))
-        (raw-path (org-element-property :path link))
+        (raw-path (replace-regexp-in-string
+                   "%" "\\%" (org-element-property :path link) nil t))
         ;; Ensure DESC really exists, or set it to nil.
         (desc (and (not (string= desc "")) desc))
         (imagep (org-export-inline-image-p
@@ -1762,9 +1798,8 @@ INFO is a plist holding contextual information.  See
         (path (cond
                ((member type '("http" "https" "ftp" "mailto"))
                 (concat type ":" raw-path))
-               ((string= type "file")
-                (if (not (file-name-absolute-p raw-path)) raw-path
-                  (concat "file://" (expand-file-name raw-path))))
+               ((and (string= type "file") (file-name-absolute-p raw-path))
+                (concat "file:" raw-path))
                (t raw-path)))
         protocol)
     (cond
@@ -1774,10 +1809,11 @@ INFO is a plist holding contextual information.  See
      ;; description.
      ((string= type "radio")
       (let ((destination (org-export-resolve-radio-link link info)))
-       (when destination
+       (if (not destination) desc
          (format "\\hyperref[%s]{%s}"
-                 (org-export-solidify-link-text path)
-                 (org-export-data (org-element-contents destination) info)))))
+                 (org-export-solidify-link-text
+                  (org-element-property :value destination))
+                 desc))))
      ;; Links pointing to a headline: Find destination and build
      ;; appropriate referencing command.
      ((member type '("custom-id" "fuzzy" "id"))
@@ -1806,7 +1842,8 @@ INFO is a plist holding contextual information.  See
                           'number-to-string
                           (org-export-get-headline-number destination info)
                           "-"))))
-            (if (and (plist-get info :section-numbers) (not desc))
+            (if (and (not desc)
+                     (org-export-numbered-headline-p destination info))
                 (format "\\ref{%s}" label)
               (format "\\hyperref[%s]{%s}" label
                       (or desc
@@ -1853,18 +1890,13 @@ contextual information."
         (latex-type (let ((env (plist-get attr :environment)))
                       (cond (env (format "%s" env))
                             ((eq type 'ordered) "enumerate")
-                            ((eq type 'unordered) "itemize")
-                            ((eq type 'descriptive) "description")))))
+                            ((eq type 'descriptive) "description")
+                            (t "itemize")))))
     (org-latex--wrap-label
      plain-list
      (format "\\begin{%s}%s\n%s\\end{%s}"
             latex-type
-            ;; Put optional arguments, if any inside square brackets
-            ;; when necessary.
-            (let ((options (format "%s" (or (plist-get attr :options) ""))))
-              (cond ((equal options "") "")
-                    ((string-match "\\`\\[.*\\]\\'" options) options)
-                    (t (format "[%s]" options))))
+            (or (plist-get attr :options) "")
             contents
             latex-type))))
 
@@ -2083,7 +2115,8 @@ contextual information."
                       ("firstnumber" ,(number-to-string (1+ num-start))))
                     org-latex-minted-options)))
                 ;; Language.
-                (or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
+                (or (cadr (assq (intern lang) org-latex-minted-langs))
+                    (downcase lang))
                 ;; Source code.
                 (let* ((code-info (org-export-unravel-code src-block))
                        (max-width
@@ -2132,8 +2165,8 @@ contextual information."
               ((and float (not (assoc "float" org-latex-listings-options)))
                `(("float" ,org-latex-default-figure-position))))
              `(("language" ,lst-lang))
-             (when label `(("label" ,label)))
-             (when caption-str `(("caption" ,caption-str)))
+             (if label `(("label" ,label)) '(("label" " ")))
+             (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
              (cond ((assoc "numbers" org-latex-listings-options) nil)
                    ((not num-start) '(("numbers" "none")))
                    ((zerop num-start) '(("numbers" "left")))
@@ -2813,7 +2846,7 @@ Return PDF file name or an error if it couldn't be produced."
                                (file-name-directory full-name)
                              default-directory))
         errors)
-    (unless snippet (message (format "Processing LaTeX file %s..." texfile)))
+    (unless snippet (message "Processing LaTeX file %s..." texfile))
     (save-window-excursion
       (cond
        ;; A function is provided: Apply it.
@@ -2843,8 +2876,8 @@ Return PDF file name or an error if it couldn't be produced."
        ;; Check for process failure.  Provide collected errors if
        ;; possible.
        (if (not (file-exists-p pdffile))
-           (error (concat (format "PDF file %s wasn't produced" pdffile)
-                          (when errors (concat ": " errors))))
+           (error "PDF file %s wasn't produced%s" pdffile
+                  (if errors (concat ": " errors) ""))
          ;; Else remove log files, when specified, and signal end of
          ;; process to user, along with any error encountered.
          (when (and (not snippet) org-latex-remove-logfiles)
@@ -2903,7 +2936,9 @@ Return output file name."
   ;; in working directory and then moved to publishing directory.
   (org-publish-attachment
    plist
-   (org-latex-compile (org-publish-org-to 'latex filename ".tex" plist))
+   (org-latex-compile
+    (org-publish-org-to
+     'latex filename ".tex" plist (file-name-directory filename)))
    pub-dir))