6

gnuplot ASCII graph is amazing. I'd like to include gnuplot dumb ASCII graph results in babel code block.

#+BEGIN_SRC gnuplot :results output :file gnuplot-dumb-plot.txt
set term dumb
set yrange [0:1.5]
plot [-2:2] exp(-x*x)
#+END_SRC

#+RESULTS:

Alas, first line of gnuplot-dumb-plot.txt includes ^L line feed control code.

Therefore, #+RESULTS: does not work.

Adding another #+INCLUDE: with example block specified does work.

#+INCLUDE: gnuplot-dumb-plot.txt example

or excluding first line with :lines specified,

#+INCLUDE: gnuplot-dumb-plot.txt example :lines "2-"

or by sed deleting first line

#+BEGIN_SRC sh
sed -e '1d' gnuplot-dumb-plot.txt
#+END_SRC

work, but are not elegant.

#+RESULTS:
#+begin_example
      +--------+-------+--------+--------+-------+--------+-------+--------+
  1.4 +-+      +       +        +        +       +        +       +      +-+
      |                                                  exp(-x*x) ******* |
      |                                                                    |
  1.2 +-+                                                                +-+
      |                                                                    |
    1 +-+                              ****                              +-+
      |                            ****    ****                            |
      |                          ***          ***                          |
  0.8 +-+                       *                *                       +-+
      |                       **                  **                       |
  0.6 +-+                   **                      **                   +-+
      |                    **                        **                    |
      |                  **                            **                  |
  0.4 +-+              **                                **              +-+
      |              ***                                  ***              |
  0.2 +-+          ***                                      ***          +-+
      |         ***                                            ***         |
      +   ******       +        +        +       +        +       ******   +
    0 ****-----+-------+--------+--------+-------+--------+-------+-----****
     -2      -1.5     -1      -0.5       0      0.5       1      1.5       2

#+end_example

Are there any options or hacks for including gnuplot dumb ASCII graphs?

Thanks to @wvxvw, it turned out to be:

  • Note: Emacs 24.5.1, org-mode 8.3.2, gnuplot Version 5.0 patchlevel 1, Mac OS X 10.10.5.
  • :session none is required in gnuplot code block for printing ASCII graph in #+RESULTS: .
    • I wish if gnuplot has an option such as noheadfeed for deleting a top ^L / or, perhaps org-babel might manage in the future.
  • HTML export

    • :results output verbatim in gnuplot code block enables printing ASCII graph at #+RESULTS: within org-mode.
    • org-html-export-as-html works. This seems to be a simple solution if you don't need LaTeX.
    • But ^L appears in the beginning of #+RESULTS: which later makes error at LaTeX.
  • LaTeX export

    • Due to ^L, "Forbidden control sequence" in LaTeX, org-latex-export-to-pdf fails.
    • For LaTeX export, do not C-c C-c at gnuplot code block. #+RESULTS: at gnuplot should be left blank.
    • :exports none is also required in gnuplot code block for avoiding LaTeX error.
  • Org-mode, HTML, LaTeX

    • @wvxvw's solution removing ^L (\014) succeeds in org-mode #+RESULTS, HTML/LaTeX exports.
    • Use #+NAME: , :var , #+CALL: for output a clean ASCII graph #+RESULTS: (without ^L)
  • 1
    Instead of outputting to a file you could add another source block with, say, Emacs Lisp code where you'd assign the output of the GNUPlot to a variable, i.e. :var=graph() (assuming you previously named the GNUPlot block with #+name: graph) and then remove the extra line in the output. – wvxvw Nov 22 '15 at 8:29
  • Thank you! I appreciate your advice! Unfortunately, perhaps due to "^L" control code, #+RESULTS: yields nothing on the org-mode, therefore variable labelled with #+NAME: graph seems to be nil... – RUserPassingBy Nov 22 '15 at 11:56
4

Below is the example code for what I meant. Also note that you need :session none since otherwise the output isn't generated. This looks like a bug to me in org-babel-execute:gnuplot, so, if you feel like it, you could write to Org mailing list about it. ;)

#+NAME: pp_ascii_gnuplot
#+BEGIN_SRC emacs-lisp :var plot=""
  (mapconcat 'identity (cl-subseq (split-string plot "\n") 2 -2) "\n")
#+END_SRC

#+NAME: graph
#+BEGIN_SRC gnuplot :results output :session none
set term dumb
set yrange [0:1.5]
plot [-2:2] exp(-x*x)
#+END_SRC

#+CALL: pp_ascii_gnuplot(plot=graph())

#+RESULTS:
#+begin_example
      +--------+-------+--------+--------+-------+--------+-------+--------+
  1.4 ++       +       +        +        +       +        exp(-x*x) ******++
      |                                                                    |
      |                                                                    |
  1.2 ++                                                                  ++
      |                                                                    |
    1 ++                               ****                               ++
      |                            ****    ****                            |
      |                          ***          ***                          |
  0.8 ++                        *                *                        ++
      |                       **                  **                       |
  0.6 ++                    **                      **                    ++
      |                    **                        **                    |
      |                  **                            **                  |
  0.4 ++               **                                **               ++
      |              ***                                  ***              |
  0.2 ++           ***                                      ***           ++
      |         ***                                            ***         |
      +   ******       +        +        +       +        +       ******   +
    0 ****-----+-------+--------+--------+-------+--------+-------+-----****
     -2      -1.5     -1      -0.5       0      0.5       1      1.5       2
#+end_example
  • I really thank you for your kind and detailed advice. A Combination of gnuplot :session none, split-string in Emacs Lisp, #+CALL: in org-mode knocked me down ! But I can't replicate the results yet )-: , on Emacs 24.5.1, org-mode 8.3.2, Mac OS X 10.10.5. evaluating #+CALL: still yields blank. I'll struggle to catch up your advice. – RUserPassingBy Nov 23 '15 at 2:36
  • 1
    @RUserPassingBy I have a newer version of Emacs and am doing this on Linux. One way I'd try to test it is by just outputting a string with ^L in it from one source block and seeing if it gets into another source block unaffected. If you need to insert ^L in a string, something like "foo\014bar" should work. – wvxvw Nov 23 '15 at 15:06
  • 1
    Oh, I thank @wvxvw ! It works ! In my environment, I need :results output verbatim in gnuplot block, :results output value in Emacs Lisp and #+CALL: blocks. Now, I'm ready to dive into deep ASCII world ! Thanks a lot ! – RUserPassingBy Nov 24 '15 at 6:08
0

Thanks to @wvxvw's many advice, I attempted a short try. I'll never figure it out without @wvxvw's advice on :session none and code \014.

#+NAME: graph
#+BEGIN_SRC gnuplot :results output verbatim :session none :exports none
set term dumb
set yrange [0:1.5]
plot [-2:2] exp(-x*x)
#+END_SRC

#+BEGIN_SRC emacs-lisp :results output value :exports both :var tmp=graph :exports both
(cadr (split-string tmp "\014"))
#+END_SRC

#+RESULTS:

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.