3

:cache yes makes the settings on #+RESULTS be erased whenever execute the block.

For example, I want an image file be resized into width 12cm for LaTeX -> PDF exporting. However, #+ATTR_LATEX: :width 12cm :float c on top of #+RESULTS[fce2...]: will be erased (if the image is need to be updated after compared the SHA1) when evaluate the block...

#+tblname: standard
| Standard Conc(ppm) | Value |
|--------------------+----------------|
|                  0 |              0 |
|                 10 |          0.264 |
|                 20 |           0.52 |
|                 30 |          0.739 |

#+begin_src gnuplot :cache yes :var standard=standard :file plots/standard.png
reset
set terminal pngcairo size 800,800 enhanced font 'WenQuanYiZenHei,14'
set xlabel "Standard Conc.(ppm)"
set ylabel "Value" 

set style up points
set grid
set pointsize 5

stats standard using 1:2 name "STD"

# f(x)=a*(x)+b
line(x)=STD_slope*(x)+STD_intercept

set xrange [0:40]
set yrange [0:]
set xtics 0, 10, 40
set ytics 0, 0.1, 1

unset key
set label 1 sprintf("r = %4.4f\nR^2 = %4.4f\ny = %4.4f x + %.4f",STD_correlation, STD_correlation**2, STD_slope, STD_intercept) at graph 0.1, graph 0.9
plot standard u 1:2 notitle,\
     line(x) title 'Linear Fit' 
#+end_src

#+ATTR_LATEX: :width 12cm :float c
#+RESULTS[fce2bedf80238d089cc3e5d79570ccf6520eddaf]:
[[file:plots/standard.png]]
5

All you have to do is add #+NAME: my-name before your source block. Then, evaluate the source block again and a new #+RESULTS[...] my-name: section is produced with the same name that you gave to the source block. You can now safely add stuff like #+ATTR_LATEX lines before the results line and they will be preserved through subsequent re-evaluations of the source block. In fact, you can move the results line to anywhere you want in the same file (even before the source block) and it will still receive the evaluation results so long as it has the correct name.

It will look something like this:

#+name: standard-plot
#+begin_src gnuplot :cache yes :var standard=standard :file plots/standard.png
... CODE GOES HERE ...
#+end_src

#+ATTR_LATEX: :width 12cm :float c
#+RESULTS[6299f1b9325738f88305182a7295c5a45689db4a]: standard-plot
[[file:plots/standard.png]]

Edit I still cannot find this behaviour documented in Org's Info pages, but it is mentioned in Fabrice Niessen's Org Babel reference card:

Named source code blocks will refresh the corresponding named results blocks anywhere in the file. [...] It doesn’t matter whether the code block and the results block are “disconnected”, such as here, as the results is a named data which Babel can locate.

2

Try this

I adapted the answer from the :post header example code in the org-mode manual.

  1. Add a new code block that will add the #+ATTR_LATEX :width 12cm :float c during a post processing step.

    #+NAME: add_attr_latex
    #+BEGIN_SRC sh :var data="" :var width="textwidth" :var float="c" :results output
      echo "#+ATTR_LATEX :width $width :float $float"
      echo "$data"
    #+END_SRC
    

    This new code block takes three (3) parameters: width, float and data.

  2. Add new :post and :results headers to your original code block.

    e.g. :post add_attr_latex(width="12cm", float="c", data=*this*) :results drawer

    When a post argument is given, the results of the code block will temporarily be bound to the *this* variable.

    #+begin_src gnuplot :post add_attr_latex(width="12cm", float="c", data=*this*) :results drawer :cache yes :var standard=standard :file plots/standard.png
    reset
    set terminal pngcairo size 800,800 enhanced font 'WenQuanYiZenHei,14'
    set xlabel "Standard Conc.(ppm)"
    set ylabel "Value" 
    
    set style up points
    set grid
    set pointsize 5
    
    stats standard using 1:2 name "STD"
    
    # f(x)=a*(x)+b
    line(x)=STD_slope*(x)+STD_intercept
    
    set xrange [0:40]
    set yrange [0:]
    set xtics 0, 10, 40
    set ytics 0, 0.1, 1
    
    unset key
    set label 1 sprintf("r = %4.4f\nR^2 = %4.4f\ny = %4.4f x + %.4f",STD_correlation, STD_correlation**2, STD_slope, STD_intercept) at graph 0.1, graph 0.9
    plot standard u 1:2 notitle,\
         line(x) title 'Linear Fit' 
    #+end_src
    
  3. Invoke the updated code block with C-cC-c and new #+RESULTS: block should display similar to the one below.

    #+RESULTS[d91af022c181a9a4988311940517f53801282437]:
    :RESULTS:
    #+ATTR_LATEX :width 12cm :float c
    [[file:plots/standard.png]]
    :END:
    

Thanks for asking your question!

Hope that helped!

Note: This code was tested using the following versions of emacs and org-mode.

GNU Emacs 24.4.1 (x86_64-apple-darwin14.0.0, NS apple-appkit-1343.14)
Org-mode version 8.2.10 (8.2.10-29-g89a0ac-elpa)
  • 1
    @kuanyui - Please let me know if you found this answer useful. I apologize for the delay. On my machine the #+ATTR_LATEX always disappeared whether or not the :cache yes was added to the code block. – Melioratus Jan 13 '15 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.