emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [patch, ox-latex] caption and :float nil
@ 2015-06-30 23:33 Rasmus
  2015-07-01 14:21 ` Aaron Ecay
  2015-07-01 20:16 ` Nicolas Goaziou
  0 siblings, 2 replies; 8+ messages in thread
From: Rasmus @ 2015-06-30 23:33 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: aaronecay

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

Hi,

Attached is a patch that works reasonably well to allow :float nil
"floats" to have captions in ox-latex.

The hard part is src-blocks.

At the moment we sometimes allow captions to register as figures (at least
when when using verbatim).  Instead we could go with "listing".  Of course
listing is not a known environment to LaTeX.  We can add register it with
newfloat in default-packages, or we can mention in the manual the lines
that needs to be added to org-latex-packages if desired:

     \usepackage{newfloat}
     \DeclareFloatingEnvironment{listing}

We could also unconditionally use captionof in case of verbatim output for
src blocks, even if float is required, which would allow us to kill
float.sty from default package alist.

BTW: In the current code we uses \captionof{listing}{caption} with minted.
This seems incompatible.  But I have not really used minted so I don't
know if I did the test correctly.  minted has a [H] placement through it's
listings environment, it seems, so I don't think we need captionof there.

Aaron: I think you had something to do with this at some point, hence the Cc.

WDYT? 

Rasmus

Examples:

#+caption: cap
#+attr_latex: :float nil
|a|

⇒

\begin{center}
\captionof{table}{cap}
\begin{tabular}{l}
\toprule
a\\
\bottomrule
\end{tabular}
\end{center}

(Of course, this is from my normal Emacs session)


#+caption: cap
#+attr_latex: :float nil
[[file:org.png]]

⇒

\begin{center}
\includegraphics{org.png}
\captionof{figure}{cap}
\end{center}


#+caption: test
#+attr_latex: :float nil
#+BEGIN_SRC emacs-lisp
  (setq org-latex-listings 'minted)
#+END_SRC

⇒ [Note, latex is happy with this...]

% \usepackage{minted}
\begin{minted}[]{common-lisp}
(setq org-latex-listings 'minted)
\end{minted}
\captionof{listing}{test}


#+caption: test
#+attr_latex: :float nil
#+BEGIN_SRC emacs-lisp
  (setq org-latex-listings nil)
#+END_SRC

⇒ [Note, here I think we should use listing instead of figure]

\begin{verbatim}
(setq org-latex-listings nil)
\end{verbatim}
\captionof{figure}{test}


-- 
Don't panic!!!

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

From 590390f2e5cf34edcfbb13b18e5c9abc67b98d93 Mon Sep 17 00:00:00 2001
From: Rasmus <rasmus@gmx.us>
Date: Wed, 1 Jul 2015 01:12:42 +0200
Subject: [PATCH] ox-latex: Support captions for None-floats

* ox-latex.el (org-latex--caption/label-string): Format
  nonfloating captions using the capt-of package.
  (org-latex--inline-image, org-latex-src-block)
  (org-latex--org-table): Allow none-float and caption.
---
 etc/ORG-NEWS     |  1 +
 lisp/ox-latex.el | 68 +++++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index ea7a2de..9592f88 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -398,6 +398,7 @@ set using the hh:mm:ss format.
 ~org-clone-subtree-with-time-shift~ now accepts 0 as an argument for
 the number of clones, which removes the repeater from the original
 subtree and creates one shifted, repeating clone.
+*** None-floating tables, graphics and blocks can have captions
 ** Miscellaneous
 *** Strip all meta data from ITEM special property
 ITEM special property does not contain TODO, priority or tags anymore.
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 11fa00d..918949b 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1109,21 +1109,31 @@ caption nor label, return the empty string.
 For non-floats, see `org-latex--wrap-label'."
   (let* ((label (org-latex--label element info nil t))
 	 (main (org-export-get-caption element))
+	 (attr (org-export-read-attribute :attr_latex element))
+	 (nonfloat (and (member :float attr)
+			(not (plist-get attr :float))
+			main))
 	 (short (org-export-get-caption element t))
-	 (caption-from-attr-latex
-	  (org-export-read-attribute :attr_latex element :caption)))
+	 (caption-from-attr-latex (plist-get attr :caption)))
     (cond
      ((org-string-nw-p caption-from-attr-latex)
       (concat caption-from-attr-latex "\n"))
      ((and (not main) (equal label "")) "")
      ((not main) (concat label "\n"))
      ;; Option caption format with short name.
-     (short (format "\\caption[%s]{%s%s}\n"
-		    (org-export-data short info)
-		    label
-		    (org-export-data main info)))
-     ;; Standard caption format.
-     (t (format "\\caption{%s%s}\n" label (org-export-data main info))))))
+     (t
+      (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
+		"\\caption%s%s{%s%s}\n")
+	      (if nonfloat
+		  (let ((type (org-element-type element)))
+		    (case type
+		      (paragraph "figure")
+		      (src-block "listing")
+		      (t (symbol-name type))))
+		"")
+	      (if short (format "[%s]" (org-export-data short info)) "")
+	      label
+	      (org-export-data main info))))))
 
 (defun org-latex-guess-inputenc (header)
   "Set the coding system in inputenc to what the buffer is.
@@ -1975,14 +1985,15 @@ used as a communication channel."
 	 ;; Retrieve latex attributes from the element around.
 	 (attr (org-export-read-attribute :attr_latex parent))
 	 (float (let ((float (plist-get attr :float)))
-		  (cond ((and (not float) (plist-member attr :float)) nil)
-			((string= float "wrap") 'wrap)
+		  (cond ((string= float "wrap") 'wrap)
 			((string= float "sideways") 'sideways)
 			((string= float "multicolumn") 'multicolumn)
 			((or float
 			     (org-element-property :caption parent)
 			     (org-string-nw-p (plist-get attr :caption)))
-			 'figure))))
+			 (if (and (member :float attr) (not float)) 'nonfloat
+			   'figure))
+			((and (not float) (plist-member attr :float)) nil))))
 	 (placement
 	  (let ((place (plist-get attr :placement)))
 	    (cond
@@ -2087,6 +2098,13 @@ used as a communication channel."
 		      (if caption-above-p caption "")
 		      comment-include image-code
 		      (if caption-above-p "" caption)))
+      (nonfloat
+       (format "\\begin{center}%s
+%s
+%s\\end{center}"
+	       (if caption-above-p caption "")
+	       image-code
+	       (if caption-above-p "" caption)))
       (otherwise image-code))))
 
 (defun org-latex-link (link desc info)
@@ -2501,12 +2519,15 @@ contextual information."
        ((not listings)
 	(let* ((caption-str (org-latex--caption/label-string src-block info))
 	       (float-env
-		(cond ((and (not float) (plist-member attributes :float)) "%s")
-		      ((string= "multicolumn" float)
+		(cond ((string= "multicolumn" float)
 		       (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
 			       (plist-get info :latex-default-figure-position)
 			       (if caption-above-p caption-str "")
 			       (if caption-above-p "" caption-str)))
+		      ((and caption (not float)
+			    (plist-member attributes :float))
+		       (concat "%s\n"
+			       (replace-regexp-in-string "listing" "figure" caption-str)))
 		      ((or caption float)
 		       (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
 			       caption-str))
@@ -2530,13 +2551,9 @@ contextual information."
 	       (float-env
 		(cond
 		 ((and (not float) (plist-member attributes :float) caption)
-		  (let ((caption
-			 (replace-regexp-in-string
-			  "\\\\caption" "\\captionof{listing}" caption-str
-			  t t)))
-		    (concat (and caption-above-p caption)
-			    "%%s"
-			    (and (not caption-above-p) (concat "\n" caption)))))
+		  (concat (and caption-above-p caption-str)
+			  "%s"
+			  (and (not caption-above-p) (concat "\n" caption-str))))
 		 ((and (not float) (plist-member attributes :float)) "%s")
 		 ((string= "multicolumn" float)
 		  (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
@@ -2867,6 +2884,12 @@ This function assumes TABLE has `org' as its `:type' property and
 			  (if caption-above-p caption "")
 			  (when centerp "\\centering\n")
 			  fontsize))
+		 ((and (not float-env) caption)
+		  (concat
+		   (and centerp "\\begin{center}\n" )
+		   (if caption-above-p caption "")
+		   (cond ((and fontsize centerp) fontsize)
+			 (fontsize (concat "{" fontsize)))))
 		 (centerp (concat "\\begin{center}\n" fontsize))
 		 (fontsize (concat "{" fontsize)))
 		(cond ((equal "tabu" table-env)
@@ -2886,6 +2909,11 @@ This function assumes TABLE has `org' as its `:type' property and
 		 (float-env
 		  (concat (if caption-above-p "" caption)
 			  (format "\n\\end{%s}" float-env)))
+		 ((and (not float-env) caption)
+		  (concat
+		   (if caption-above-p "" caption)
+		   (and centerp "\n\\end{center}")
+		   (and fontsize (not centerp) "}")))
 		 (centerp "\n\\end{center}")
 		 (fontsize "}")))))))
 
-- 
2.4.4


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [patch, ox-latex] caption and :float nil
  2015-06-30 23:33 [patch, ox-latex] caption and :float nil Rasmus
@ 2015-07-01 14:21 ` Aaron Ecay
  2015-07-01 15:05   ` Rasmus
  2015-07-01 20:16 ` Nicolas Goaziou
  1 sibling, 1 reply; 8+ messages in thread
From: Aaron Ecay @ 2015-07-01 14:21 UTC (permalink / raw)
  To: Rasmus; +Cc: orgmode

Hi Rasmus,

AFAICS you sent the original message only to me.  I’m replying also to
the org mode list.  You may want to resend the patch attachment though,
since the quoting in this reply will probably mangle it.

I have almost no time to follow org development these days (sadly).
However these are my brief thoughts.

2015ko uztailak 1an, Rasmus-ek idatzi zuen:
> 
> Hi,
> 
> Attached is a patch that works reasonably well to allow :float nil
> "floats" to have captions in ox-latex.
> 
> The hard part is src-blocks.
> 
> At the moment we sometimes allow captions to register as figures (at least
> when when using verbatim).  Instead we could go with "listing".  Of course
> listing is not a known environment to LaTeX.  We can add register it with
> newfloat in default-packages, or we can mention in the manual the lines
> that needs to be added to org-latex-packages if desired:
> 
>      \usepackage{newfloat}
>      \DeclareFloatingEnvironment{listing}
>

I think this should depend on the value of org-latex-listings:
nil -> use \captionof{float}
t -> use \captionof{listing}
minted -> use \captionof{listing} (see below for why)

> We could also unconditionally use captionof in case of verbatim output for
> src blocks, even if float is required, which would allow us to kill
> float.sty from default package alist.
> 
> BTW: In the current code we uses \captionof{listing}{caption} with minted.
> This seems incompatible.  

Minted uses listing as its floating environment; see sec. 5 of the
minted manual.  So this seems correct in fact.

> But I have not really used minted so I don't know if I did the
> test correctly.  minted has a [H] placement through it's listings
> environment, it seems, so I don't think we need captionof there.

I’m not sure that minted provides this separately from the float
package.  So it’s probably better to use \captionof in the minted case
as well.  Certainly it will make things more consistent from org’s POV.

Thanks,
Aaron

> 
> Aaron: I think you had something to do with this at some point, hence the Cc.
> 
> WDYT? 
> 
> Rasmus
> 
> Examples:
> 
> #+caption: cap
> #+attr_latex: :float nil
> |a|
> 
> ⇒
> 
> \begin{center}
> \captionof{table}{cap}
> \begin{tabular}{l}
> \toprule
> a\\
> \bottomrule
> \end{tabular}
> \end{center}
> 
> (Of course, this is from my normal Emacs session)
> 
> 
> #+caption: cap
> #+attr_latex: :float nil
> [[file:org.png]]
> 
> ⇒
> 
> \begin{center}
> \includegraphics{org.png}
> \captionof{figure}{cap}
> \end{center}
> 
> 
> #+caption: test
> #+attr_latex: :float nil
> #+BEGIN_SRC emacs-lisp
>   (setq org-latex-listings 'minted)
> #+END_SRC
> 
> ⇒ [Note, latex is happy with this...]
> 
> % \usepackage{minted}
> \begin{minted}[]{common-lisp}
> (setq org-latex-listings 'minted)
> \end{minted}
> \captionof{listing}{test}
> 
> 
> #+caption: test
> #+attr_latex: :float nil
> #+BEGIN_SRC emacs-lisp
>   (setq org-latex-listings nil)
> #+END_SRC
> 
> ⇒ [Note, here I think we should use listing instead of figure]
> 
> \begin{verbatim}
> (setq org-latex-listings nil)
> \end{verbatim}
> \captionof{figure}{test}
> 
> 
> -- 
> Don't panic!!!
> From 590390f2e5cf34edcfbb13b18e5c9abc67b98d93 Mon Sep 17 00:00:00 2001
> From: Rasmus <rasmus@gmx.us>
> Date: Wed, 1 Jul 2015 01:12:42 +0200
> Subject: [PATCH] ox-latex: Support captions for None-floats
> 
> * ox-latex.el (org-latex--caption/label-string): Format
>   nonfloating captions using the capt-of package.
>   (org-latex--inline-image, org-latex-src-block)
>   (org-latex--org-table): Allow none-float and caption.
> ---
>  etc/ORG-NEWS     |  1 +
>  lisp/ox-latex.el | 68 +++++++++++++++++++++++++++++++++++++++-----------------
>  2 files changed, 49 insertions(+), 20 deletions(-)
> 
> diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
> index ea7a2de..9592f88 100644
> --- a/etc/ORG-NEWS
> +++ b/etc/ORG-NEWS
> @@ -398,6 +398,7 @@ set using the hh:mm:ss format.
>  ~org-clone-subtree-with-time-shift~ now accepts 0 as an argument for
>  the number of clones, which removes the repeater from the original
>  subtree and creates one shifted, repeating clone.
> +*** None-floating tables, graphics and blocks can have captions
>  ** Miscellaneous
>  *** Strip all meta data from ITEM special property
>  ITEM special property does not contain TODO, priority or tags anymore.
> diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
> index 11fa00d..918949b 100644
> --- a/lisp/ox-latex.el
> +++ b/lisp/ox-latex.el
> @@ -1109,21 +1109,31 @@ caption nor label, return the empty string.
>  For non-floats, see `org-latex--wrap-label'."
>    (let* ((label (org-latex--label element info nil t))
>  	 (main (org-export-get-caption element))
> +	 (attr (org-export-read-attribute :attr_latex element))
> +	 (nonfloat (and (member :float attr)
> +			(not (plist-get attr :float))
> +			main))
>  	 (short (org-export-get-caption element t))
> -	 (caption-from-attr-latex
> -	  (org-export-read-attribute :attr_latex element :caption)))
> +	 (caption-from-attr-latex (plist-get attr :caption)))
>      (cond
>       ((org-string-nw-p caption-from-attr-latex)
>        (concat caption-from-attr-latex "\n"))
>       ((and (not main) (equal label "")) "")
>       ((not main) (concat label "\n"))
>       ;; Option caption format with short name.
> -     (short (format "\\caption[%s]{%s%s}\n"
> -		    (org-export-data short info)
> -		    label
> -		    (org-export-data main info)))
> -     ;; Standard caption format.
> -     (t (format "\\caption{%s%s}\n" label (org-export-data main info))))))
> +     (t
> +      (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
> +		"\\caption%s%s{%s%s}\n")
> +	      (if nonfloat
> +		  (let ((type (org-element-type element)))
> +		    (case type
> +		      (paragraph "figure")
> +		      (src-block "listing")
> +		      (t (symbol-name type))))
> +		"")
> +	      (if short (format "[%s]" (org-export-data short info)) "")
> +	      label
> +	      (org-export-data main info))))))
>  
>  (defun org-latex-guess-inputenc (header)
>    "Set the coding system in inputenc to what the buffer is.
> @@ -1975,14 +1985,15 @@ used as a communication channel."
>  	 ;; Retrieve latex attributes from the element around.
>  	 (attr (org-export-read-attribute :attr_latex parent))
>  	 (float (let ((float (plist-get attr :float)))
> -		  (cond ((and (not float) (plist-member attr :float)) nil)
> -			((string= float "wrap") 'wrap)
> +		  (cond ((string= float "wrap") 'wrap)
>  			((string= float "sideways") 'sideways)
>  			((string= float "multicolumn") 'multicolumn)
>  			((or float
>  			     (org-element-property :caption parent)
>  			     (org-string-nw-p (plist-get attr :caption)))
> -			 'figure))))
> +			 (if (and (member :float attr) (not float)) 'nonfloat
> +			   'figure))
> +			((and (not float) (plist-member attr :float)) nil))))
>  	 (placement
>  	  (let ((place (plist-get attr :placement)))
>  	    (cond
> @@ -2087,6 +2098,13 @@ used as a communication channel."
>  		      (if caption-above-p caption "")
>  		      comment-include image-code
>  		      (if caption-above-p "" caption)))
> +      (nonfloat
> +       (format "\\begin{center}%s
> +%s
> +%s\\end{center}"
> +	       (if caption-above-p caption "")
> +	       image-code
> +	       (if caption-above-p "" caption)))
>        (otherwise image-code))))
>  
>  (defun org-latex-link (link desc info)
> @@ -2501,12 +2519,15 @@ contextual information."
>         ((not listings)
>  	(let* ((caption-str (org-latex--caption/label-string src-block info))
>  	       (float-env
> -		(cond ((and (not float) (plist-member attributes :float)) "%s")
> -		      ((string= "multicolumn" float)
> +		(cond ((string= "multicolumn" float)
>  		       (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
>  			       (plist-get info :latex-default-figure-position)
>  			       (if caption-above-p caption-str "")
>  			       (if caption-above-p "" caption-str)))
> +		      ((and caption (not float)
> +			    (plist-member attributes :float))
> +		       (concat "%s\n"
> +			       (replace-regexp-in-string "listing" "figure" caption-str)))
>  		      ((or caption float)
>  		       (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
>  			       caption-str))
> @@ -2530,13 +2551,9 @@ contextual information."
>  	       (float-env
>  		(cond
>  		 ((and (not float) (plist-member attributes :float) caption)
> -		  (let ((caption
> -			 (replace-regexp-in-string
> -			  "\\\\caption" "\\captionof{listing}" caption-str
> -			  t t)))
> -		    (concat (and caption-above-p caption)
> -			    "%%s"
> -			    (and (not caption-above-p) (concat "\n" caption)))))
> +		  (concat (and caption-above-p caption-str)
> +			  "%s"
> +			  (and (not caption-above-p) (concat "\n" caption-str))))
>  		 ((and (not float) (plist-member attributes :float)) "%s")
>  		 ((string= "multicolumn" float)
>  		  (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
> @@ -2867,6 +2884,12 @@ This function assumes TABLE has `org' as its `:type' property and
>  			  (if caption-above-p caption "")
>  			  (when centerp "\\centering\n")
>  			  fontsize))
> +		 ((and (not float-env) caption)
> +		  (concat
> +		   (and centerp "\\begin{center}\n" )
> +		   (if caption-above-p caption "")
> +		   (cond ((and fontsize centerp) fontsize)
> +			 (fontsize (concat "{" fontsize)))))
>  		 (centerp (concat "\\begin{center}\n" fontsize))
>  		 (fontsize (concat "{" fontsize)))
>  		(cond ((equal "tabu" table-env)
> @@ -2886,6 +2909,11 @@ This function assumes TABLE has `org' as its `:type' property and
>  		 (float-env
>  		  (concat (if caption-above-p "" caption)
>  			  (format "\n\\end{%s}" float-env)))
> +		 ((and (not float-env) caption)
> +		  (concat
> +		   (if caption-above-p "" caption)
> +		   (and centerp "\n\\end{center}")
> +		   (and fontsize (not centerp) "}")))
>  		 (centerp "\n\\end{center}")
>  		 (fontsize "}")))))))
>  
> -- 
> 2.4.4
> 
> 
-- 
Aaron Ecay

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [patch, ox-latex] caption and :float nil
  2015-07-01 14:21 ` Aaron Ecay
@ 2015-07-01 15:05   ` Rasmus
  2015-07-02  8:50     ` Aaron Ecay
  0 siblings, 1 reply; 8+ messages in thread
From: Rasmus @ 2015-07-01 15:05 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

Aaron Ecay <aaronecay@gmail.com> writes:

> AFAICS you sent the original message only to me.  I’m replying also to
> the org mode list.  You may want to resend the patch attachment though,
> since the quoting in this reply will probably mangle it.

  http://permalink.gmane.org/gmane.emacs.orgmode/98791

??

> 2015ko uztailak 1an, Rasmus-ek idatzi zuen:
>> 
>> Hi,
>> 
>> Attached is a patch that works reasonably well to allow :float nil
>> "floats" to have captions in ox-latex.
>> 
>> The hard part is src-blocks.
>> 
>> At the moment we sometimes allow captions to register as figures (at least
>> when when using verbatim).  Instead we could go with "listing".  Of course
>> listing is not a known environment to LaTeX.  We can add register it with
>> newfloat in default-packages, or we can mention in the manual the lines
>> that needs to be added to org-latex-packages if desired:
>> 
>>      \usepackage{newfloat}
>>      \DeclareFloatingEnvironment{listing}
>>
>
> I think this should depend on the value of org-latex-listings:
> nil -> use \captionof{float}

You mean figure.  But a source block in not a float.

> t -> use \captionof{listing}
> minted -> use \captionof{listing} (see below for why)


>> We could also unconditionally use captionof in case of verbatim output for
>> src blocks, even if float is required, which would allow us to kill
>> float.sty from default package alist.
>> 
>> BTW: In the current code we uses \captionof{listing}{caption} with minted.
>> This seems incompatible.  
>
> Minted uses listing as itsf floating environment; see sec. 5 of the
> minted manual.  So this seems correct in fact.

Consider this minimal example

\documentclass{article}
\usepackage{minted}
\usepackage{capt-of}
\begin{document}
\begin{minted}{lisp}
(+ 1 1 )
\end{minted}
\captionof{listing}{test}
\end{document}

This is the error I get when compiling:

ERROR: Undefined control sequence.

--- TeX said ---
\@tempf ... \@parboxrestore \normalsize \@fs@capt 
                                                  {\@nameuse {fnum@#1}}{\ign...
l.8 \captionof{listing}{test}

It works fine when I use another counter than listing.

>> But I have not really used minted so I don't know if I did the
>> test correctly.  minted has a [H] placement through it's listings
>> environment, it seems, so I don't think we need captionof there.
>
> I’m not sure that minted provides this separately from the float
> package.  So it’s probably better to use \captionof in the minted case
> as well.  Certainly it will make things more consistent from org’s POV.
>

I guess it doesn't.  In any, minted.sty has

  \RequirePackage{float}

So we can be sure [H] is available.  Thus, we should just rely on float.

Rasmus

-- 
Lasciate ogni speranza, voi che leggete questo.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [patch, ox-latex] caption and :float nil
  2015-06-30 23:33 [patch, ox-latex] caption and :float nil Rasmus
  2015-07-01 14:21 ` Aaron Ecay
@ 2015-07-01 20:16 ` Nicolas Goaziou
  1 sibling, 0 replies; 8+ messages in thread
From: Nicolas Goaziou @ 2015-07-01 20:16 UTC (permalink / raw)
  To: Rasmus; +Cc: aaronecay, emacs-orgmode

Hello,

Rasmus <rasmus@gmx.us> writes:

> Attached is a patch that works reasonably well to allow :float nil
> "floats" to have captions in ox-latex.

Thanks for the patch. A minor comment follows.

> +	 (attr (org-export-read-attribute :attr_latex element))
> +	 (nonfloat (and (member :float attr)

  (plist-member :float attr)


> -			 'figure))))
> +			 (if (and (member :float attr) (not float)) 'nonfloat
> +			   'figure))

Ditto.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [patch, ox-latex] caption and :float nil
  2015-07-01 15:05   ` Rasmus
@ 2015-07-02  8:50     ` Aaron Ecay
  2015-07-02 12:17       ` Rasmus
  0 siblings, 1 reply; 8+ messages in thread
From: Aaron Ecay @ 2015-07-02  8:50 UTC (permalink / raw)
  To: Rasmus, emacs-orgmode

Hi Rasmus,

2015ko uztailak 1an, Rasmus-ek idatzi zuen:
> 
> Hi,
> 
> Aaron Ecay <aaronecay@gmail.com> writes:
> 
>> AFAICS you sent the original message only to me.  I’m replying also to
>> the org mode list.  You may want to resend the patch attachment though,
>> since the quoting in this reply will probably mangle it.
> 
>   http://permalink.gmane.org/gmane.emacs.orgmode/98791
> 
> ??

Both Gmail and my MUA (notmuch) showed the message as addressed only to
me, but now that I examine the raw headers I see a Newsgroups: line.  I
guess you sent the list copy via NNTP instead of email?  In any case, I
didn’t notice it.  Sorry.

>> I think this should depend on the value of org-latex-listings:
>> nil -> use \captionof{float}
> 
> You mean figure.  But a source block in not a float.

Yes.

> 
>> t -> use \captionof{listing}
>> minted -> use \captionof{listing} (see below for why)
> 
> 
>>> We could also unconditionally use captionof in case of verbatim output for
>>> src blocks, even if float is required, which would allow us to kill
>>> float.sty from default package alist.
>>> 
>>> BTW: In the current code we uses \captionof{listing}{caption} with minted.
>>> This seems incompatible.  
>> 
>> Minted uses listing as itsf floating environment; see sec. 5 of the
>> minted manual.  So this seems correct in fact.
> 
> Consider this minimal example
> 
> \documentclass{article}
> \usepackage{minted}
> \usepackage{capt-of}
> \begin{document}
> \begin{minted}{lisp}
> (+ 1 1 )
> \end{minted}
> \captionof{listing}{test}
> \end{document}
> 
> This is the error I get when compiling:
> 
> ERROR: Undefined control sequence.
> 
> --- TeX said ---
> \@tempf ... \@parboxrestore \normalsize \@fs@capt 
>                                                   {\@nameuse {fnum@#1}}{\ign...
> l.8 \captionof{listing}{test}
> 
> It works fine when I use another counter than listing.

OK, I’ve done some more investigation.  By default, minted uses
\newfloat from the float package to create the listing float class.
This does not set up the caption integration.  OTOH, adding the newfloat
option to minted uses \DeclareFloatingEnvironment from the newfloat
package, which works.  IOW, replacing the relevant line in your MWE with
the following makes it work:

\usepackage[newfloat]{minted}

I’m not sure how that should be integrated in the picture – perhaps org
should recommend (require) this invocation of minted.

> 
>>> But I have not really used minted so I don't know if I did the
>>> test correctly.  minted has a [H] placement through it's listings
>>> environment, it seems, so I don't think we need captionof there.
>> 
>> I’m not sure that minted provides this separately from the float
>> package.  So it’s probably better to use \captionof in the minted case
>> as well.  Certainly it will make things more consistent from org’s POV.
>> 
> 
> I guess it doesn't.  In any, minted.sty has
> 
>   \RequirePackage{float}
> 
> So we can be sure [H] is available.  Thus, we should just rely on
> float.

But this is an implementation detail of minted, and could change in a
future release.

-- 
Aaron Ecay

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [patch, ox-latex] caption and :float nil
  2015-07-02  8:50     ` Aaron Ecay
@ 2015-07-02 12:17       ` Rasmus
  2015-07-03 12:43         ` Rasmus
  0 siblings, 1 reply; 8+ messages in thread
From: Rasmus @ 2015-07-02 12:17 UTC (permalink / raw)
  To: emacs-orgmode

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

Aaron Ecay <aaronecay@gmail.com> writes:

> OK, I’ve done some more investigation.  By default, minted uses
> \newfloat from the float package to create the listing float class.
> This does not set up the caption integration.  OTOH, adding the newfloat
> option to minted uses \DeclareFloatingEnvironment from the newfloat
> package, which works.  IOW, replacing the relevant line in your MWE with
> the following makes it work:
>
> \usepackage[newfloat]{minted}
>
> I’m not sure how that should be integrated in the picture – perhaps org
> should recommend (require) this invocation of minted.

That's weird.  The capt-of documentation recommend the float.sty way of
adding new environments.

>> I guess it doesn't.  In any, minted.sty has
>> 
>>   \RequirePackage{float}
>> 
>> So we can be sure [H] is available.  Thus, we should just rely on
>> float.
>
> But this is an implementation detail of minted, and could change in a
> future release.

That's a good point.

I updated the patches.

1. Rework captions when float is nil and caption is not.  Kill usage of
   [H] as it's the same as a nonfloat.  A src block is always a nonfloat
   unless it is not.
2. Update documentation. 
3. Kill float dependency.

Does anybody disagree to especially the third change?  Otherwise I'll push
it.

Rasmus

-- 
Together we will make the possible totalllly impossible!

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

From a0b7c47e37c3c5c62492236562a5f19a38efc06f Mon Sep 17 00:00:00 2001
From: Rasmus <rasmus@gmx.us>
Date: Wed, 1 Jul 2015 01:12:42 +0200
Subject: [PATCH 1/3] ox-latex: Support captions for None-floats

* ox-latex.el (org-latex--caption/label-string): Format
  nonfloating captions using the capt-of package.
  (org-latex--inline-image, org-latex-src-block)
  (org-latex--org-table): Allow none-float and caption.
---
 etc/ORG-NEWS     |  1 +
 lisp/ox-latex.el | 88 ++++++++++++++++++++++++++++++++++++--------------------
 2 files changed, 58 insertions(+), 31 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index ea7a2de..9592f88 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -398,6 +398,7 @@ set using the hh:mm:ss format.
 ~org-clone-subtree-with-time-shift~ now accepts 0 as an argument for
 the number of clones, which removes the repeater from the original
 subtree and creates one shifted, repeating clone.
+*** None-floating tables, graphics and blocks can have captions
 ** Miscellaneous
 *** Strip all meta data from ITEM special property
 ITEM special property does not contain TODO, priority or tags anymore.
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 11fa00d..31d804b 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1109,21 +1109,37 @@ caption nor label, return the empty string.
 For non-floats, see `org-latex--wrap-label'."
   (let* ((label (org-latex--label element info nil t))
 	 (main (org-export-get-caption element))
+	 (attr (org-export-read-attribute :attr_latex element))
+	 (type (org-element-type element))
+	 (nonfloat (or (and (plist-member attr :float)
+			    (not (plist-get attr :float))
+			    main)
+		       (and (eq type 'src-block)
+			    (not (plist-get attr :float))
+			    (memq (plist-get info :latex-listings)
+				  '(nil minted)))))
 	 (short (org-export-get-caption element t))
-	 (caption-from-attr-latex
-	  (org-export-read-attribute :attr_latex element :caption)))
+	 (caption-from-attr-latex (plist-get attr :caption)))
     (cond
      ((org-string-nw-p caption-from-attr-latex)
       (concat caption-from-attr-latex "\n"))
      ((and (not main) (equal label "")) "")
      ((not main) (concat label "\n"))
      ;; Option caption format with short name.
-     (short (format "\\caption[%s]{%s%s}\n"
-		    (org-export-data short info)
-		    label
-		    (org-export-data main info)))
-     ;; Standard caption format.
-     (t (format "\\caption{%s%s}\n" label (org-export-data main info))))))
+     (t
+      (format (if nonfloat "\\captionof{%s}%s{%s%s}\n"
+		"\\caption%s%s{%s%s}\n")
+	      (if nonfloat
+		  (case type
+		    (paragraph "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))))))
 
 (defun org-latex-guess-inputenc (header)
   "Set the coding system in inputenc to what the buffer is.
@@ -1975,14 +1991,15 @@ used as a communication channel."
 	 ;; Retrieve latex attributes from the element around.
 	 (attr (org-export-read-attribute :attr_latex parent))
 	 (float (let ((float (plist-get attr :float)))
-		  (cond ((and (not float) (plist-member attr :float)) nil)
-			((string= float "wrap") 'wrap)
+		  (cond ((string= float "wrap") 'wrap)
 			((string= float "sideways") 'sideways)
 			((string= float "multicolumn") 'multicolumn)
 			((or float
 			     (org-element-property :caption parent)
 			     (org-string-nw-p (plist-get attr :caption)))
-			 'figure))))
+			 (if (and (plist-member attr :float) (not float)) 'nonfloat
+			   'figure))
+			((and (not float) (plist-member attr :float)) nil))))
 	 (placement
 	  (let ((place (plist-get attr :placement)))
 	    (cond
@@ -2087,6 +2104,13 @@ used as a communication channel."
 		      (if caption-above-p caption "")
 		      comment-include image-code
 		      (if caption-above-p "" caption)))
+      (nonfloat
+       (format "\\begin{center}%s
+%s
+%s\\end{center}"
+	       (if caption-above-p caption "")
+	       image-code
+	       (if caption-above-p "" caption)))
       (otherwise image-code))))
 
 (defun org-latex-link (link desc info)
@@ -2501,15 +2525,15 @@ contextual information."
        ((not listings)
 	(let* ((caption-str (org-latex--caption/label-string src-block info))
 	       (float-env
-		(cond ((and (not float) (plist-member attributes :float)) "%s")
-		      ((string= "multicolumn" float)
+		(cond ((string= "multicolumn" float)
 		       (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
 			       (plist-get info :latex-default-figure-position)
 			       (if caption-above-p caption-str "")
 			       (if caption-above-p "" caption-str)))
-		      ((or caption float)
-		       (format "\\begin{figure}[H]\n%%s\n%s\\end{figure}"
-			       caption-str))
+		      (caption (concat
+				(if caption-above-p caption-str "")
+				"%s"
+				(if caption-above-p "" (concat "\n" caption-str))))
 		      (t "%s"))))
 	  (format
 	   float-env
@@ -2529,23 +2553,14 @@ contextual information."
 	(let* ((caption-str (org-latex--caption/label-string src-block info))
 	       (float-env
 		(cond
-		 ((and (not float) (plist-member attributes :float) caption)
-		  (let ((caption
-			 (replace-regexp-in-string
-			  "\\\\caption" "\\captionof{listing}" caption-str
-			  t t)))
-		    (concat (and caption-above-p caption)
-			    "%%s"
-			    (and (not caption-above-p) (concat "\n" caption)))))
-		 ((and (not float) (plist-member attributes :float)) "%s")
 		 ((string= "multicolumn" float)
 		  (format "\\begin{listing*}\n%s%%s\n%s\\end{listing*}"
 			  (if caption-above-p caption-str "")
 			  (if caption-above-p "" caption-str)))
-		 ((or caption float)
-		  (format "\\begin{listing}[H]\n%s%%s\n%s\\end{listing}"
-			  (if caption-above-p caption-str "")
-			  (if caption-above-p "" caption-str)))
+		 (caption
+		  (concat (if caption-above-p caption-str "")
+			  "%s"
+			  (if caption-above-p "" (concat "\n" caption-str))))
 		 (t "%s")))
 	       (options (plist-get info :latex-minted-options))
 	       (body
@@ -2867,6 +2882,12 @@ This function assumes TABLE has `org' as its `:type' property and
 			  (if caption-above-p caption "")
 			  (when centerp "\\centering\n")
 			  fontsize))
+		 ((and (not float-env) caption)
+		  (concat
+		   (and centerp "\\begin{center}\n" )
+		   (if caption-above-p caption "")
+		   (cond ((and fontsize centerp) fontsize)
+			 (fontsize (concat "{" fontsize)))))
 		 (centerp (concat "\\begin{center}\n" fontsize))
 		 (fontsize (concat "{" fontsize)))
 		(cond ((equal "tabu" table-env)
@@ -2884,8 +2905,13 @@ This function assumes TABLE has `org' as its `:type' property and
 				 table-env)))
 		(cond
 		 (float-env
-		  (concat (if caption-above-p "" caption)
-			  (format "\n\\end{%s}" float-env)))
+		  (concat (if caption-above-p "" (concat "\n" caption))
+			  (format "\\end{%s}" float-env)))
+		 ((and (not float-env) caption)
+		  (concat
+		   (if caption-above-p "" (concat "\n" caption))
+		   (and centerp "\\end{center}")
+		   (and fontsize (not centerp) "}")))
 		 (centerp "\n\\end{center}")
 		 (fontsize "}")))))))
 
-- 
2.4.4


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-Update-minted-documentation.patch --]
[-- Type: text/x-diff, Size: 2737 bytes --]

From 6d0e535cc9de8411cd7291acecca632a0eb46c11 Mon Sep 17 00:00:00 2001
From: Rasmus <rasmus@gmx.us>
Date: Thu, 2 Jul 2015 13:10:30 +0200
Subject: [PATCH 2/3] Update minted documentation.

* ox-latex.el (org-latex-listings):
* org.texi (Literal examples): Update minted documentation.
---
 doc/org.texi     | 22 +++++++++++++---------
 lisp/ox-latex.el |  2 +-
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index 8ec8a51..f979483 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -9996,15 +9996,19 @@ that can be marked up by font-lock in Emacs, you can ask for the example to
 look like the fontified Emacs buffer@footnote{This works automatically for
 the HTML back-end (it requires version 1.34 of the @file{htmlize.el} package,
 which is distributed with Org).  Fontified code chunks in @LaTeX{} can be
-achieved using either the listings or the
-@url{http://code.google.com/p/minted, minted,} package.  Refer to
-@code{org-latex-listings} documentation for details.}.  This is done
-with the @samp{src} block, where you also need to specify the name of the
-major mode that should be used to fontify the example@footnote{Code in
-@samp{src} blocks may also be evaluated either interactively or on export.
-See @pxref{Working with source code} for more information on evaluating code
-blocks.}, see @ref{Easy templates} for shortcuts to easily insert code
-blocks.
+achieved using either the
+@url{https://www.ctan.org/tex-archive/macros/latex/contrib/listings/?lang=en, listings,}
+or the
+@url{https://github.com/gpoore/minted, minted,} package.
+If you use minted or listing, you must load the packages manually, for
+example by adding the desired package to
+@code{org-latex-packages-alist}.  Refer to @code{org-latex-listings}
+for details.}.  This is done with the @samp{src} block, where you also need
+to specify the name of the major mode that should be used to fontify the
+example@footnote{Code in @samp{src} blocks may also be evaluated either
+interactively or on export.  See @pxref{Working with source code} for more
+information on evaluating code blocks.}, see @ref{Easy templates} for
+shortcuts to easily insert code blocks.
 @cindex #+BEGIN_SRC
 
 @example
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 31d804b..d9f7c0e 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -795,7 +795,7 @@ 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\"))
+  \(add-to-list 'org-latex-packages-alist '(\"newfloat\" \"minted\"))
 
 In addition, it is necessary to install pygments
 \(http://pygments.org), and to configure the variable
-- 
2.4.4


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0003-Remove-float-from-org-latex-default-packages-alist.patch --]
[-- Type: text/x-diff, Size: 1667 bytes --]

From 9f27ba3eb818303e2ef6e161343558438afa8e1a Mon Sep 17 00:00:00 2001
From: Rasmus <rasmus@gmx.us>
Date: Thu, 2 Jul 2015 13:49:30 +0200
Subject: [PATCH 3/3] Remove float from org-latex-default-packages-alist

---
 etc/ORG-NEWS | 2 ++
 lisp/org.el  | 3 +--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 9592f88..d43e9d3 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -427,6 +427,8 @@ when ~org-link-search-must-match-exact-headline~ is not nil.
 *** ~org-latex-hyperref-template~, ~org-latex-title-command~ formatting
 New formatting keys are supported.  See the respective docstrings.
 Note, ~org-latex-hyperref-template~ has a new default value.
+*** ~float.sty~ has been removed from ~org-latex-default-packages-alist~
+If you require this package add it to ~org-latex-packages-alist~.
 * Version 8.2
 
 ** Incompatible changes
diff --git a/lisp/org.el b/lisp/org.el
index 425999c..f0a0f0b 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -4075,7 +4075,6 @@ header, or they will be appended."
     (""     "graphicx"  t)
     (""     "grffile"   t)
     (""     "longtable" nil)
-    (""     "float"     nil)
     (""     "wrapfig"   nil)
     (""     "rotating"  nil)
     ("normalem" "ulem"  t)
@@ -4100,7 +4099,7 @@ Org mode to function properly:
 - graphicx: for including images
 - grffile: allow periods and spaces in graphics file names
 - longtable: For multipage tables
-- float, wrapfig: for figure placement
+- wrapfig: for figure placement
 - rotating: for sideways figures and tables
 - ulem: for underline and strike-through
 - amsmath: for subscript and superscript and math environments
-- 
2.4.4


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [patch, ox-latex] caption and :float nil
  2015-07-02 12:17       ` Rasmus
@ 2015-07-03 12:43         ` Rasmus
  2015-07-03 12:57           ` Nicolas Goaziou
  0 siblings, 1 reply; 8+ messages in thread
From: Rasmus @ 2015-07-03 12:43 UTC (permalink / raw)
  To: emacs-orgmode

> I updated the patches.

Pushed.

Thanks,
Rasmus

-- 
Hvor meget poesi tror De kommer ud af et glas isvand?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [patch, ox-latex] caption and :float nil
  2015-07-03 12:43         ` Rasmus
@ 2015-07-03 12:57           ` Nicolas Goaziou
  0 siblings, 0 replies; 8+ messages in thread
From: Nicolas Goaziou @ 2015-07-03 12:57 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

Rasmus <rasmus@gmx.us> writes:

> Pushed.

Thank you.

Regards,

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2015-07-03 12:55 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-30 23:33 [patch, ox-latex] caption and :float nil Rasmus
2015-07-01 14:21 ` Aaron Ecay
2015-07-01 15:05   ` Rasmus
2015-07-02  8:50     ` Aaron Ecay
2015-07-02 12:17       ` Rasmus
2015-07-03 12:43         ` Rasmus
2015-07-03 12:57           ` Nicolas Goaziou
2015-07-01 20:16 ` Nicolas Goaziou

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).