emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] example or source blocks with captions


From: Thomas S. Dye
Subject: Re: [O] example or source blocks with captions
Date: Wed, 27 Apr 2011 11:21:02 -1000

Aloha Neilen Marais,

A while back I took a stab at an overly ambitious project that I've subsequently dropped. In that project I did manage to establish captions that work with org-special-blocks. What follows is a cut and paste job from the bones of the project that might help you solve the problem of captioning constructs other than figures and tables. It was aimed primarily at LaTeX export, but I seem to recall that it worked for html as well.

All the best,
Tom

This link establishes a caption that works with both LaTeX and html
and can be used to mark blocks that Org-mode doesn't recognize by
default.  Note that you currently have to enter these links by hand
and not with the usual =org-insert-link= function, which doesn't allow
spaces in the =PATH= argument.

#+BEGIN_listing
# <<caption-link-type>>
#+source: define-caption-link
#+begin_src emacs-lisp :exports code
  (org-add-link-type
   "caption" nil
   (lambda (path desc format)
     (cond
      ((eq format 'html)
       (format "<span class=\"caption\">%s</span>" desc))
      ((eq format 'latex)
       (format "\\caption[%s]{%s}" path desc)))))
#+end_src
[[caption:A new caption link type][A new caption link type.]]
# <<fig:caption-link>>
#+END_listing

Block-level markup is accomplished with the help of the
[[latex:package][org-special-blocks]] package. It is used in this file to wrap the [[latex:progstruct][listing]] environment defined by the [[latex:package][minted]] package around a source code
block to get a floating listing in the LaTeX document.  LaTeX will
keep track of floating listings and will also prepare a list of
listings that can be placed between the table of contents and the
first section of the article. [[latex:classfile][Org-article]] makes the [[latex:progstruct][listing]] environment available with the [[latex:package][listings]] package, as well, so this
facility can be used regardless of the package chosen to highlight
syntax and typeset source code listings.

Use a construct like this to wrap the source block in a [[latex:progstruct][listing]]
environment.  Typically, you will want to include a figure caption and
a label for cross referencing.  This can be done with a =#+LATEX:= line.

 : #+BEGIN_listing
 :  <source block>
 : #+LATEX: \caption{The caption.}\ref{fig:src_blk}
 : #+END_listing

To use this facility, you'll need to load [[latex:package][org-special- blocks]] (Listing
\ref{fig:org-special-blocks}).  This code can go in =.emacs=, or you
can load it for the session by executing the following source code
block with =C-c C-c=.

#+BEGIN_listing
#+source: special-blocks
#+begin_src emacs-lisp :exports code :results silent
  (require 'org-special-blocks)
#+end_src
[[caption:Require org-special-blocks][Require org-special-blocks.]]
# <<fig:org-special-blocks>>
#+END_listing

The [[latex:package][org-special-blocks]] package leaves it up to the user to see that
the HTML output is styled correctly.  A line of code like Listing
\ref{fig:css}, or something similar, when added to the [[latex:proglang][Org-mode]]
buffer, styles listings by putting a black box around them.

#+BEGIN_listing
#+source: css
#+begin_src org :exports code
#+STYLE: <style>.listing {margin: 1em; padding: 1em; border: 1px solid black}</style>
#+end_src
[[caption:A simple CSS style for listings][A simple CSS style for listings.]]
# <<fig:css>>
#+END_listing

On Apr 27, 2011, at 5:57 AM, Neilen Marais wrote:

Hi,

Is it possible to caption a #+begin_src or #+begin_example block? Doing

#+CAPTION: dipole_analytical_balanis.mac
#+begin_example
....
#+end_example

doesn't seem to do the trick. I would find this useful to include
suggested filenames when quoting source on a web page.

Thanks
Neilen





reply via email to

[Prev in Thread] Current Thread [Next in Thread]