From c0b2a29c912d8e054c6062cc3bd63e0b4b5d907d Mon Sep 17 00:00:00 2001
From: Feng Shu <tumashu@gmail.com>
Date: Sat, 29 Jun 2013 23:04:03 +0800
Subject: [PATCH] Add `:caption' attribute to #+ATTR_LATEX property

* lisp/ox-latex.el (org-latex--caption/label-string): Add ability,
which can build a caption string from `:caption' attribute of #+ATTR_LATEX.
(org-latex--inline-image,org-latex--org-table): Tiny change.
* doc/org.texi (@LaTeX{} specific attributes): Document `:caption'
attribute of #+ATTR_LATEX.

This feature is very useful when you export org to latex with custom
caption command, for example:

\#+ATTR_LATEX: :caption \BiTableCaption{caption 1}{caption 2}
|---+---|
| x | y |
|---+---|
| 1 | 2 |
|---+---|
---
 doc/org.texi     |   32 +++++++++++++++++++++++++++++---
 lisp/ox-latex.el |   32 +++++++++++++++++++-------------
 2 个文件被修改，插入 48 行(+)，删除 16 行(-)

diff --git a/doc/org.texi b/doc/org.texi
index 9e47a17..7d378db 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -11599,6 +11599,12 @@ Environment used for the table.  It can be set to any @LaTeX{} table
 environment, like @code{tabularx}, @code{longtable}, @code{array},
 @code{tabu}, @code{bmatrix}@enddots{}  It defaults to
 @code{org-latex-default-table-environment} value.
+@item :caption
+By default, you should use @code{#+caption} keyword to add a table caption.
+If you want to add caption with complex or special latex commands, you can use
+@code{:caption} attribute. It will precedence over @code{#+caption} keyword.
+It should be set with raw latex command and nothing in it will be
+interpreted by Org.
 @item :float
 @itemx :placement
 Float environment for the table.  Possible values are @code{sidewaystable},
@@ -11651,6 +11657,18 @@ a table that will span over multiple pages, or a matrix product:
 | 3 | 4 |
 @end example
 
+
+When export the below example to latex, the table caption will be set
+with latex command @code{\bicaption@{Heading A@}@{Heading B@}} instead of
+@code{#+CAPTION} keywords.
+
+@example
+#+CAPTION: Heading C
+#+ATTR_LATEX: :caption \bicaption@{Heading A@}@{Heading B@}
+| ..... | ..... |
+| ..... | ..... |
+@end example
+
 @subsubheading Images in @LaTeX{} export
 @cindex images, inline in @LaTeX{}
 @cindex inlining images in @LaTeX{}
@@ -11663,9 +11681,17 @@ TikZ (@url{http://sourceforge.net/projects/pgf/}) images, it will become an
 @code{\input} macro wrapped within a @code{tikzpicture} environment.}.
 
 You can specify specify image width or height with, respectively,
-@code{:width} and @code{:height} attributes.  It is also possible to add any
-other option with the @code{:options} attribute, as shown in the following
-example:
+@code{:width} and @code{:height} attributes. If you want to set image
+caption with special latex command, you can use @code{:caption} attribute,
+for example:
+
+@example
+#+ATTR_LATEX: :caption \bicaption@{Heading A@}@{Heading B@}
+[[./img/sed-hr4049.pdf]]
+@end example
+
+It is also possible to add any other option with the @code{:options}
+attribute, as shown in the following example:
 
 @example
 #+ATTR_LATEX: :width 5cm :options angle=90
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 09928a4..ce04d57 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -881,17 +881,19 @@ For non-floats, see `org-latex--wrap-label'."
 		      (format "\\label{%s}"
 			      (org-export-solidify-link-text label))))
 	 (main (org-export-get-caption element))
-	 (short (org-export-get-caption element t)))
-    (cond
-     ((and (not main) (equal label-str "")) "")
-     ((not main) (concat label-str "\n"))
-     ;; Option caption format with short name.
-     (short (format "\\caption[%s]{%s%s}\n"
-		    (org-export-data short info)
-		    label-str
-		    (org-export-data main info)))
-     ;; Standard caption format.
-     (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
+	 (short (org-export-get-caption element t))
+	 (caption-from-attr-latex (org-export-read-attribute :attr_latex element :caption)))
+    (cond ((org-string-nw-p caption-from-attr-latex)
+	   (concat caption-from-attr-latex "\n"))
+	  ((and (not main) (equal label-str "")))
+	  ((not main) (concat label-str "\n"))
+	  ;; Option caption format with short name.
+	  (short (format "\\caption[%s]{%s%s}\n"
+			 (org-export-data short info)
+			 label-str
+			 (org-export-data main info)))
+	  ;; Standard caption format.
+	  (t (format "\\caption{%s%s}\n" label-str (org-export-data main info))))))
 
 (defun org-latex-guess-inputenc (header)
   "Set the coding system in inputenc to what the buffer is.
@@ -1655,7 +1657,9 @@ used as a communication channel."
 		  (cond ((and (not float) (plist-member attr :float)) nil)
 			((string= float "wrap") 'wrap)
 			((string= float "multicolumn") 'multicolumn)
-			((or float (org-element-property :caption parent))
+			((or float
+			     (org-element-property :caption parent)
+			     (org-string-nw-p (plist-get attr :caption)))
 			 'figure))))
 	 (placement
 	  (let ((place (plist-get attr :placement)))
@@ -2333,7 +2337,9 @@ This function assumes TABLE has `org' as its `:type' property and
 			 ((and (not float) (plist-member attr :float)) nil)
 			 ((string= float "sidewaystable") "sidewaystable")
 			 ((string= float "multicolumn") "table*")
-			 ((or float (org-element-property :caption table))
+			 ((or float
+			      (org-element-property :caption table)
+			      (org-string-nw-p (plist-get attr :caption)))
 			  "table")))))
 	 ;; Extract others display options.
 	 (fontsize (let ((font (plist-get attr :font)))
-- 
1.7.10.4

