Re: [O] Exporting source code blocks as LaTeX figures

2013-05-20 Thread James Harkins
On May 20, 2013 4:54 PM, "Nicolas Goaziou"  wrote:
>
> Hello,
>
> James Harkins  writes:
>
> > The other reference to multicolumn is for table export, and this isn't
> > a table either. So I think, as currently designed, :multicolumn simply
> > doesn't apply.
>
> Correct. The first attached patch implements :float multicolumn
> and :float figure handling for source blocks. Would you mind to test it?

Sure, I can try both... but not until later today or maybe tomorrow.

Thanks!
hjh


Re: [O] Exporting source code blocks as LaTeX figures

2013-05-20 Thread Nicolas Goaziou
Hello,

James Harkins  writes:

> The other reference to multicolumn is for table export, and this isn't
> a table either. So I think, as currently designed, :multicolumn simply
> doesn't apply.

Correct. The first attached patch implements :float multicolumn
and :float figure handling for source blocks. Would you mind to test it?

> I'm aware that BEGIN_figure delimits a "special block" which
> translates into LaTeX as an arbitrary environment, and not all
> environments can be floated safely... but perhaps one approach for the
> future would be to consider BEGIN_figure to be an extra-special
> "special block" where we know that the properties of LaTeX floating
> bodies are valid.

The second patch introduces :starred attribute for special blocks in
latex back-end, e.g.,

  #+attr_latex: :starred t :options [b]
  #+begin_figure
  something
  #+end_figure

I think it could be useful. Do you?


Regards,

-- 
Nicolas Goaziou
>From cbc4a3a0b98cab9455bd7d3bd8b0ea8bfe8ea34e Mon Sep 17 00:00:00 2001
From: Nicolas Goaziou 
Date: Mon, 20 May 2013 10:43:18 +0200
Subject: [PATCH 1/2] ox-latex: Implement :float attribute for source blocks

* lisp/ox-latex.el (org-latex-src-block): Handle :float attribute. Its
  value can be set to "figure", "multicolumn" or "nil".

This needs to be documented in Org manual.
---
 lisp/ox-latex.el | 142 +++
 1 file changed, 80 insertions(+), 62 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 41cf1d0..669c84b 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2073,21 +2073,27 @@ contextual information."
 			(continued (org-export-get-loc src-block info))
 			(new 0)))
 	   (retain-labels (org-element-property :retain-labels src-block))
-	   (long-listing
-	(let ((attr (org-export-read-attribute :attr_latex src-block)))
-	  (if (plist-member attr :long-listing)
-		  (plist-get attr :long-listing)
-		org-latex-long-listings
+	   (attributes (org-export-read-attribute :attr_latex src-block))
+	   (long-listing (if (plist-member attributes :long-listing)
+			 (plist-get attributes :long-listing)
+			   org-latex-long-listings))
+	   (float (plist-get attributes :float)))
   (cond
;; Case 1.  No source fontification.
((not org-latex-listings)
 	(let* ((caption-str (org-latex--caption/label-string src-block info))
-	   (float-env (and (not long-listing)
-			   (or label caption)
-			   (format "\\begin{figure}[H]\n%s%%s\n\\end{figure}"
-   caption-str
+	   (float-env
+		(cond (long-listing "%s")
+		  ((string= "multicolumn" float)
+		   (format "\\begin{figure*}[%s]\n%s%%s\n\\end{figure*}"
+			   org-latex-default-figure-position
+			   caption-str))
+		  ((or label caption (string= "figure" float))
+		   (format "\\begin{figure}[H]\n%s%%s\n\\end{figure}"
+			   caption-str))
+		  (t "%s"
 	  (format
-	   (or float-env "%s")
+	   float-env
 	   (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
 			   (org-export-format-code-default src-block info))
;; Case 2.  Custom environment.
@@ -2097,46 +2103,52 @@ contextual information."
 			   custom-env))
;; Case 3.  Use minted package.
((eq org-latex-listings 'minted)
-	(let ((float-env
-	   (and (not long-listing)
-		(or label caption)
-		(format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
-			(org-latex--caption/label-string src-block info
-	  (body
-	   (format
-		"\\begin{minted}[%s]{%s}\n%s\\end{minted}"
-		;; Options.
-		(org-latex--make-option-string
-		 (if (or (not num-start)
-			 (assoc "linenos" org-latex-minted-options))
-		 org-latex-minted-options
-		   (append `(("linenos")
-			 ("firstnumber" ,(number-to-string (1+ num-start
-			   org-latex-minted-options)))
-		;; Language.
-		(or (cadr (assq (intern lang) org-latex-minted-langs)) lang)
-		;; Source code.
-		(let* ((code-info (org-export-unravel-code src-block))
-		   (max-width
-			(apply 'max
-			   (mapcar 'length
-   (org-split-string (car code-info)
-			 "\n")
-		  (org-export-format-code
-		   (car code-info)
-		   (lambda (loc num ref)
-		 (concat
-		  loc
-		  (when ref
-			;; Ensure references are flushed to the right,
-			;; separated with 6 spaces from the widest line
-			;; of code.
-			(concat (make-string (+ (- max-width (length loc)) 6)
-	 ?\s)
-(format "(%s)" ref)
-		   nil (and retain-labels (cdr code-info)))
+	(let* ((caption-str (org-latex--caption/label-string src-block info))
+	   (float-env
+		(cond (long-listing "%s")
+		  ((string= "multicolumn" float)
+		   (format "\\begin{listing*}\n%%s\n%s\\end{listing*}"
+			   caption-str))
+		  ((or label caption (string= "figure" float))
+		   (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
+			   caption-str))
+		  (t "%s")))
+	   (body
+		(format
+		 "\\begin{minted}[%s]{%

Re: [O] Exporting source code blocks as LaTeX figures

2013-05-20 Thread James Harkins
On Mon, May 20, 2013 at 2:30 PM, Thomas S. Dye  wrote:
>> Then the next problem... I'm going to have some figures that need to
>> span two columns. According to [1], I should use \begin{figure*} ...
>> \end{figure*}. Is there a way to do that using a special block? (First
>> guess, "#+BEGIN_figure*" causes the special block not to be
>> recognized.) Or do I need to write the literal LaTeX code for that?
>
> No, you don't need to write literal LaTeX code. See Section 12.7.4 of
> the manual, which describes setting the :float attribute to
> 'multicolumn'.

That section of the manual seems to describe the behavior for images. I did try:

#+ATTR_LaTeX: :multicolumn :options [htb]
#+BEGIN_figure
...
#+END_figure

... and :multicolumn had no effect on the output LaTeX code (either
with or without the preceding ":" -- I tried both):

\begin{figure}[htb]
\lstset{language={},label=code1,caption={Some code},numbers=none}
\begin{lstlisting}
// SuperCollider code here
.
\end{lstlisting}
\end{figure}

Nor does this make a difference:

#+ATTR_LaTeX: :float multicolumn :options [htb]
#+BEGIN_figure

And if I omit BEGIN_figure altogether, and try to apply :float
directly to the source code block, then the floating environment
doesn't appear in the LaTeX code at all.

The other reference to multicolumn is for table export, and this isn't
a table either. So I think, as currently designed, :multicolumn simply
doesn't apply.

I'm aware that BEGIN_figure delimits a "special block" which
translates into LaTeX as an arbitrary environment, and not all
environments can be floated safely... but perhaps one approach for the
future would be to consider BEGIN_figure to be an extra-special
"special block" where we know that the properties of LaTeX floating
bodies are valid.

Or relax the restriction so that BEGIN_figure* no longer tries to
treat _ as a subscript marker.

hjh



Re: [O] Exporting source code blocks as LaTeX figures

2013-05-19 Thread Thomas S. Dye
Aloha James,

James Harkins  writes:

> On Sat, May 18, 2013 at 3:39 PM, Nicolas Goaziou  wrote:
>> instead? I.e., why don't you apply caption to src block? You can tweak
>> the position of the caption with "captionpos" option in listings
>> environment.
>
> Wow, I hadn't thought of that. Actually that works better in a lot of ways.
>
> Then the next problem... I'm going to have some figures that need to
> span two columns. According to [1], I should use \begin{figure*} ...
> \end{figure*}. Is there a way to do that using a special block? (First
> guess, "#+BEGIN_figure*" causes the special block not to be
> recognized.) Or do I need to write the literal LaTeX code for that?
>

No, you don't need to write literal LaTeX code. See Section 12.7.4 of
the manual, which describes setting the :float attribute to
'multicolumn'.

hth,
Tom

-- 
Thomas S. Dye
http://www.tsdye.com



Re: [O] Exporting source code blocks as LaTeX figures

2013-05-19 Thread James Harkins
On Sat, May 18, 2013 at 3:39 PM, Nicolas Goaziou  wrote:
> instead? I.e., why don't you apply caption to src block? You can tweak
> the position of the caption with "captionpos" option in listings
> environment.

Wow, I hadn't thought of that. Actually that works better in a lot of ways.

Then the next problem... I'm going to have some figures that need to
span two columns. According to [1], I should use \begin{figure*} ...
\end{figure*}. Is there a way to do that using a special block? (First
guess, "#+BEGIN_figure*" causes the special block not to be
recognized.) Or do I need to write the literal LaTeX code for that?

hjh

[1] 
http://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions#Wide_figures_in_two_column_documents



Re: [O] Exporting source code blocks as LaTeX figures

2013-05-18 Thread Nicolas Goaziou
Hello,

James Harkins  writes:

> Couple of questions about exporting LaTeX figures that are neither
> tables nor images -- specifically, source code blocks using the
> listings package.
>
> 1. Is there any way to use #+CAPTION with a #+BEGIN_figure block, so
> that the caption will be rendered underneath the text in the figure?
>
> It seems (from reading the org manual and some experimentation on my
> own) that the answer is no. That's rather inconvenient; the journal
> specifies that captions should appear below their respective figures,
> but as it is:
>
> #+CAPTION: Some code
> #+NAME: code1
> #+BEGIN_figure
> #+BEGIN_SRC {}
> // blah blah

Why don't you use:

  #+begin_figure
  #+caption: Some code
  #+name: code1
  #+begin_src {}
  // blah blah

instead? I.e., why don't you apply caption to src block? You can tweak
the position of the caption with "captionpos" option in listings
environment.

Caption are not supported in special blocks because these could contain
more than one object, and it wouldn't be clear which of them would have
the caption.

> 2. Is the customize variable "Org Latex Default Figure Position" only
> used for tables and images? It would be nice if it applied to
> BEGIN_figure as well (unless :options override that). "#+ATTR_LaTeX:
> :options [htb]" is a mite inconvenient to replicate for every code
> example. Certainly possible, but it would save a little effort if the
> default were used here too.

BEGIN_figure is a "special block", i.e. a generic environment. "[htb]"
does not always make sense in every environment.

Also, figure environment is created automatically in some cases (e.g.,
when `org-latex-listings' is nil and source block is captioned), so you
generally don't need to write it explicitly.

Anyway, you can easily add "[htb]" options to all figure environments in
the output with a filter.


Regards,

-- 
Nicolas Goaziou



[O] Exporting source code blocks as LaTeX figures

2013-05-17 Thread James Harkins
Couple of questions about exporting LaTeX figures that are neither
tables nor images -- specifically, source code blocks using the
listings package.

1. Is there any way to use #+CAPTION with a #+BEGIN_figure block, so
that the caption will be rendered underneath the text in the figure?

It seems (from reading the org manual and some experimentation on my
own) that the answer is no. That's rather inconvenient; the journal
specifies that captions should appear below their respective figures,
but as it is:

#+CAPTION: Some code
#+NAME: code1
#+BEGIN_figure
#+BEGIN_SRC {}
// blah blah

-->

\begin{figure}
\caption{\label{code1}Some code}
\lstset{language={},numbers=none}
\begin{lstlisting}
// blah blah

-->

Figure 1. Some code.

// blah blah
.

I can work around it by not using #+CAPTION, and writing
\caption{...}\label{...}. That shouldn't be a problem for this
project, unless for some reason I should have to re-export as, say,
ODT or HTML.

2. Is the customize variable "Org Latex Default Figure Position" only
used for tables and images? It would be nice if it applied to
BEGIN_figure as well (unless :options override that). "#+ATTR_LaTeX:
:options [htb]" is a mite inconvenient to replicate for every code
example. Certainly possible, but it would save a little effort if the
default were used here too.

hjh