emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Rasmus <rasmus@gmx.us>
To: emacs-orgmode@gnu.org
Subject: [patch, ox-latex] captions and latex-environments
Date: Thu, 16 Mar 2017 13:02:20 +0100	[thread overview]
Message-ID: <87k27pv38z.fsf@gmx.us> (raw)

[-- Attachment #1: Type: text/plain, Size: 726 bytes --]

Hi,

I often generate tables directly from R.  At the moment, I have to do
something like the following because org-latex-latex-environment doesn’t
support captions.

    #+name: tbl:1
    #+caption: cap
    #+begin_table
    #+begin_center
    #+include: "tbl.tex"
    #+end_center
    #+end_table

This is a bit too verbose IMO.
With the attached patch this can be cut down to,

    #+name: tbl:1
    #+caption: cap
    #+include: "tbl.tex"

It should respect ‘org-latex-custom-lang-environments’ and
‘org-latex-caption-above’...

Would this be an appropriate change or is it too hackish?  I have not
looked at other backends, but I suspect this is mostly a latex thing.

Thanks,
Rasmus

-- 
When in doubt, do it!

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-latex-Support-caption-for-latex-environment.patch --]
[-- Type: text/x-diff, Size: 4421 bytes --]

From 4304552a0c8a72c6aeb2805a8cf703eddb5da123 Mon Sep 17 00:00:00 2001
From: Rasmus <rasmus@gmx.us>
Date: Thu, 16 Mar 2017 12:45:10 +0100
Subject: [PATCH] ox-latex: Support caption for latex-environment

* lisp/ox-latex.el (org-latex-environment--type): New function
  determining type of a latex-environment.
  (org-latex-latex-environment): Add support for caption.
  (org-latex--caption/label-string): Use correct type for non-floating
  latex-environments.
---
 lisp/ox-latex.el | 78 ++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 62 insertions(+), 16 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 2727359cb..f226dc7ae 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1312,14 +1312,19 @@ For non-floats, see `org-latex--wrap-label'."
      (t
       (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
 		"\\caption%s%s{%s%s}\n")
-	      (if nonfloat
-		  (cl-case type
-		    (paragraph "figure")
-		    (src-block (if (plist-get info :latex-listings)
-				   "listing"
-				 "figure"))
-		    (t (symbol-name type)))
-		"")
+	      (let ((type* (if (eq type 'latex-environment)
+			       (org-latex-environment--type element)
+			     type)))
+		(if nonfloat
+		    (cl-case type*
+		      (paragraph "figure")
+		      (image "figure")
+		      (special-block "figure")
+		      (src-block (if (plist-get info :latex-listings)
+				     "listing"
+				   "figure"))
+		      (t (symbol-name type*)))
+		  ""))
 	      (if short (format "[%s]" (org-export-data short info)) "")
 	      label
 	      (org-export-data main info))))))
@@ -2250,24 +2255,65 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 
 ;;;; Latex Environment
 
+(defun org-latex-environment--type (latex-environment)
+  "Return the TYPE of LATEX-ENVIRONMENT.
+
+The TYPE is determined from the actual latex environment, and
+could be a member of `org-latex-caption-above' or `math'."
+  (let* ((value (org-remove-indentation
+		 (org-element-property :value latex-environment)))
+	 (latex-begin-re (cadr (assoc "begin" org-latex-regexps)))
+	 (env (progn (string-match latex-begin-re value)
+		     (match-string 2 value))))
+    (cond
+     ((string-match org-latex-math-environments-re value) 'math)
+     ((string-match-p "tab\\(le\\|ular\\)" env) 'table)
+     ((string-match-p "figure" env) 'image)
+     ;; Default coding environments
+     ((or (string-match-p "\\(\\(lst\\)?listing\\|verbatim\\|minted\\)" env)
+	  (string-match-p
+	   (regexp-opt
+	    (mapcar (lambda (str)
+		      (let ((s (cadr str)))
+			(if (string-match latex-begin-re s)
+			    (match-string 2 s)
+			  s)))
+		    org-latex-custom-lang-environments))
+	   env))
+      'src-block)
+     (t 'special-block))))
+
 (defun org-latex-latex-environment (latex-environment _contents info)
   "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
 CONTENTS is nil.  INFO is a plist holding contextual information."
   (when (plist-get info :with-latex)
-    (let ((value (org-remove-indentation
-		  (org-element-property :value latex-environment))))
-      (if (not (org-element-property :name latex-environment)) value
+    (let* ((value (org-remove-indentation
+		   (org-element-property :value latex-environment)))
+	   (type (org-latex-environment--type latex-environment))
+	   (caption (if (eq type 'math)
+			(org-latex--label latex-environment info nil t)
+		      (org-latex--caption/label-string latex-environment info)))
+	   (caption-above-p
+	    (memq type (append (plist-get info :latex-caption-above) '(math)))))
+      (if (not (or (org-element-property :name latex-environment)
+		   (org-element-property :caption latex-environment)))
+	  value
 	;; Environment is labeled: label must be within the environment
 	;; (otherwise, a reference pointing to that element will count
-	;; the section instead).
+	;; the section instead). Also insert caption if `latex-environment'
+	;; is not a math environment.
 	(with-temp-buffer
 	  (insert value)
-	  (goto-char (point-min))
-	  (forward-line)
-	  (insert (org-latex--label latex-environment info nil t))
+	  (if caption-above-p
+	      (progn
+		(goto-char (point-min))
+		(forward-line)
+		(insert caption))
+	    (goto-char (point-max))
+	    (forward-line -1)
+	    (insert caption))
 	  (buffer-string))))))
 
-
 ;;;; Latex Fragment
 
 (defun org-latex-latex-fragment (latex-fragment _contents _info)
-- 
2.12.0


             reply	other threads:[~2017-03-16 12:02 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-16 12:02 Rasmus [this message]
2017-03-16 12:09 ` [patch, ox-latex] captions and latex-environments Rasmus
2017-03-17  7:22   ` Nicolas Goaziou
2017-03-17  9:23     ` Rasmus
2017-03-18  9:44       ` Nicolas Goaziou
2017-03-20 14:34         ` Rasmus
2017-03-23 16:17           ` Nicolas Goaziou
2017-03-24 16:25             ` Rasmus
2017-03-27 12:02               ` Nicolas Goaziou
2017-03-27 12:30                 ` Rasmus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k27pv38z.fsf@gmx.us \
    --to=rasmus@gmx.us \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).