emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Eric Schulte" <schulte.eric@gmail.com>
To: Org Mode <emacs-orgmode@gnu.org>
Subject: [patch] latex export: org preprocesses code inside #+begin/end_latex block
Date: Tue, 06 Oct 2009 18:02:45 -0600	[thread overview]
Message-ID: <m2ljjo135x.fsf@gmail.com> (raw)

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

Hi,

I just wanted to share the following bug and potential patch.  When code
inside of a #+begin/end_latex block looks like a table, org-mode
processes it as a table on latex export.  For example given the
following Org-mode

--8<---------------cut here---------------start------------->8---
The following latex isn't exported correctly

#+begin_latex
  \begin{code}
  data BTree = Leaf a
             | Node Tree Tree
  \end{code}
#+end_latex
--8<---------------cut here---------------end--------------->8---

org exports to the following LaTeX

--8<---------------cut here---------------start------------->8---
The following latex isn't exported correctly

  \begin{code}
  data BTree = Leaf a

\begin{center}
\begin{tabular}{l}
 Node Tree Tree  \\
\end{tabular}
\end{center}


  \end{code}
--8<---------------cut here---------------end--------------->8---

The attached patch [1] appears to fix this bug, however as I don't know
the latex exporter that well there is the chance this could introduce
many other problems.  (Note that although the patch looks big, all it
does is wrap part of org-export-latex-tables in an unless block)

Thanks -- Eric

Footnotes: 
[1]  

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: protecting-tables.patch --]
[-- Type: text/x-patch, Size: 9982 bytes --]

diff --git a/lisp/org-latex.el b/lisp/org-latex.el
index c50f87e..8c50e4d 100644
--- a/lisp/org-latex.el
+++ b/lisp/org-latex.el
@@ -1336,114 +1336,115 @@ The conversion is made depending of STRING-BEFORE and STRING-AFTER."
   "Convert tables to LaTeX and INSERT it."
   (goto-char (point-min))
   (while (re-search-forward "^\\([ \t]*\\)|" nil t)
-    (org-table-align)
-    (let* ((beg (org-table-begin))
-	   (end (org-table-end))
-	   (raw-table (buffer-substring beg end))
-	   (org-table-last-alignment (copy-sequence org-table-last-alignment))
-	   (org-table-last-column-widths (copy-sequence
-					  org-table-last-column-widths))
-	   fnum fields line lines olines gr colgropen line-fmt align
-	   caption label attr floatp longtblp)
-      (if org-export-latex-tables-verbatim
-	  (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
-			      "\\end{verbatim}\n")))
-	    (apply 'delete-region (list beg end))
-	    (insert (org-export-latex-protect-string tbl)))
-	(progn
-	  (setq caption (org-find-text-property-in-string
-			 'org-caption raw-table)
-		attr (org-find-text-property-in-string
-		      'org-attributes raw-table)
-		label (org-find-text-property-in-string
-		       'org-label raw-table)
-		longtblp (and attr (stringp attr)
-			      (string-match "\\<longtable\\>" attr))
-		align (and attr (stringp attr)
-			   (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
-			   (match-string 1 attr))
-		floatp (or caption label))
-	  (setq lines (org-split-string raw-table "\n"))
-	  (apply 'delete-region (list beg end))
-	  (when org-export-table-remove-special-lines
-	    (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
-	  (when org-table-clean-did-remove-column
+    (unless (get-text-property (1- (point)) 'org-protected)
+      (org-table-align)
+      (let* ((beg (org-table-begin))
+             (end (org-table-end))
+             (raw-table (buffer-substring beg end))
+             (org-table-last-alignment (copy-sequence org-table-last-alignment))
+             (org-table-last-column-widths (copy-sequence
+                                            org-table-last-column-widths))
+             fnum fields line lines olines gr colgropen line-fmt align
+             caption label attr floatp longtblp)
+        (if org-export-latex-tables-verbatim
+            (let* ((tbl (concat "\\begin{verbatim}\n" raw-table
+                                "\\end{verbatim}\n")))
+              (apply 'delete-region (list beg end))
+              (insert (org-export-latex-protect-string tbl)))
+          (progn
+            (setq caption (org-find-text-property-in-string
+                           'org-caption raw-table)
+                  attr (org-find-text-property-in-string
+                        'org-attributes raw-table)
+                  label (org-find-text-property-in-string
+                         'org-label raw-table)
+                  longtblp (and attr (stringp attr)
+                                (string-match "\\<longtable\\>" attr))
+                  align (and attr (stringp attr)
+                             (string-match "\\<align=\\([^ \t\n\r,]+\\)" attr)
+                             (match-string 1 attr))
+                  floatp (or caption label))
+            (setq lines (org-split-string raw-table "\n"))
+            (apply 'delete-region (list beg end))
+            (when org-export-table-remove-special-lines
+              (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
+            (when org-table-clean-did-remove-column
 	      (pop org-table-last-alignment)
 	      (pop org-table-last-column-widths))
-	  ;; make a formatting string to reflect aligment
-	  (setq olines lines)
-	  (while (and (not line-fmt) (setq line (pop olines)))
-	    (unless (string-match "^[ \t]*|-" line)
-	      (setq fields (org-split-string line "[ \t]*|[ \t]*"))
-	      (setq fnum (make-vector (length fields) 0))
-	      (setq line-fmt
-		    (mapconcat
-		     (lambda (x)
-		       (setq gr (pop org-table-colgroup-info))
-		       (format "%s%%s%s"
-			       (cond ((eq gr :start)
-				      (prog1 (if colgropen "|" "|")
-					(setq colgropen t)))
-				     ((eq gr :startend)
-				      (prog1 (if colgropen "|" "|")
-					(setq colgropen nil)))
-				     (t ""))
-			       (if (memq gr '(:end :startend))
-				   (progn (setq colgropen nil) "|")
-				 "")))
-		     fnum ""))))
-	  ;; fix double || in line-fmt
-	  (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
-	  ;; maybe remove the first and last "|"
-	  (when (and (not org-export-latex-tables-column-borders)
-		     (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
-	    (setq line-fmt (match-string 2 line-fmt)))
-	  ;; format alignment
-	  (unless align
-	    (setq align (apply 'format
-			       (cons line-fmt
-				     (mapcar (lambda (x) (if x "r" "l"))
-					     org-table-last-alignment)))))
-	  ;; prepare the table to send to orgtbl-to-latex
-	  (setq lines
-		(mapcar
-		 (lambda(elem)
-		   (or (and (string-match "[ \t]*|-+" elem) 'hline)
-		       (org-split-string (org-trim elem) "|")))
-		 lines))
-	  (when insert
-	    (insert (org-export-latex-protect-string
-		     (concat
-		      (if longtblp
-			  (concat "\\begin{longtable}{" align "}\n")
-			(if floatp "\\begin{table}[htb]\n"))
-		      (if (or floatp longtblp)
-			  (format
-			   "\\caption{%s%s}"
-			   (if label (concat "\\\label{" label "}") "")
-			   (or caption "")))
-		      (if longtblp "\\\\\n" "\n")
-		      (if (and org-export-latex-tables-centered (not longtblp))
-			  "\\begin{center}\n")
-		      (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
-		      (orgtbl-to-latex
-		       lines
-		       `(:tstart nil :tend nil
-				 :hlend ,(if longtblp
-					     (format "\\\\
+            ;; make a formatting string to reflect aligment
+            (setq olines lines)
+            (while (and (not line-fmt) (setq line (pop olines)))
+              (unless (string-match "^[ \t]*|-" line)
+                (setq fields (org-split-string line "[ \t]*|[ \t]*"))
+                (setq fnum (make-vector (length fields) 0))
+                (setq line-fmt
+                      (mapconcat
+                       (lambda (x)
+                         (setq gr (pop org-table-colgroup-info))
+                         (format "%s%%s%s"
+                                 (cond ((eq gr :start)
+                                        (prog1 (if colgropen "|" "|")
+                                          (setq colgropen t)))
+                                       ((eq gr :startend)
+                                        (prog1 (if colgropen "|" "|")
+                                          (setq colgropen nil)))
+                                       (t ""))
+                                 (if (memq gr '(:end :startend))
+                                     (progn (setq colgropen nil) "|")
+                                   "")))
+                       fnum ""))))
+            ;; fix double || in line-fmt
+            (setq line-fmt (replace-regexp-in-string "||" "|" line-fmt))
+            ;; maybe remove the first and last "|"
+            (when (and (not org-export-latex-tables-column-borders)
+                       (string-match "^\\(|\\)?\\(.+\\)|$" line-fmt))
+              (setq line-fmt (match-string 2 line-fmt)))
+            ;; format alignment
+            (unless align
+              (setq align (apply 'format
+                                 (cons line-fmt
+                                       (mapcar (lambda (x) (if x "r" "l"))
+                                               org-table-last-alignment)))))
+            ;; prepare the table to send to orgtbl-to-latex
+            (setq lines
+                  (mapcar
+                   (lambda(elem)
+                     (or (and (string-match "[ \t]*|-+" elem) 'hline)
+                         (org-split-string (org-trim elem) "|")))
+                   lines))
+            (when insert
+              (insert (org-export-latex-protect-string
+                       (concat
+                        (if longtblp
+                            (concat "\\begin{longtable}{" align "}\n")
+                          (if floatp "\\begin{table}[htb]\n"))
+                        (if (or floatp longtblp)
+                            (format
+                             "\\caption{%s%s}"
+                             (if label (concat "\\\label{" label "}") "")
+                             (or caption "")))
+                        (if longtblp "\\\\\n" "\n")
+                        (if (and org-export-latex-tables-centered (not longtblp))
+                            "\\begin{center}\n")
+                        (if (not longtblp) (concat "\\begin{tabular}{" align "}\n"))
+                        (orgtbl-to-latex
+                         lines
+                         `(:tstart nil :tend nil
+                                   :hlend ,(if longtblp
+                                               (format "\\\\
 \\hline
 \\endhead
 \\hline\\multicolumn{%d}{r}{Continued on next page}\\
 \\endfoot
 \\endlastfoot" (length org-table-last-alignment))
-					   nil)))
-		      (if (not longtblp) (concat "\n\\end{tabular}"))
-		      (if longtblp "\n" (if org-export-latex-tables-centered
-					    "\n\\end{center}\n" "\n"))
-		      (if longtblp
-			  "\\end{longtable}"
-			(if floatp "\\end{table}"))))
-		    "\n\n")))))))
+                                             nil)))
+                        (if (not longtblp) (concat "\n\\end{tabular}"))
+                        (if longtblp "\n" (if org-export-latex-tables-centered
+                                              "\n\\end{center}\n" "\n"))
+                        (if longtblp
+                            "\\end{longtable}"
+                          (if floatp "\\end{table}"))))
+                      "\n\n"))))))))
 
 (defun org-export-latex-fontify ()
   "Convert fontification to LaTeX."

[-- Attachment #3: Type: text/plain, Size: 204 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Remember: use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

             reply	other threads:[~2009-10-07  0:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-07  0:02 Eric Schulte [this message]
2009-10-07  6:10 ` [patch] latex export: org preprocesses code inside #+begin/end_latex block Carsten Dominik

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=m2ljjo135x.fsf@gmail.com \
    --to=schulte.eric@gmail.com \
    --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).