Re: [proof of concept, tip] 'Templates with arguments' using org src blocks

2023-10-13 Thread Max Nikulin

On 06/10/2023 15:07, Juan Manuel Macías wrote:

And, finally:

┌
│ #+header: :var table = table1
│ #+header: :var notes = "\\item [1] First note\n\\item [2] Second note"
│ {{{template(tptable,table notes)}}}
└


Usage is quite close to "#+call:", but it does not support specifying 
arguments using "#+header:"

https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-org.html

I would still consider noweb (table1 is omitted):

--- 8< ---
#+property: header-args:org :exports results :results replace :noweb yes

#+NAME: tptable
#+begin_src org :exports none
  ,#+begin_table
  ,#+ATTR_LaTeX: :options [b]
  ,#+begin_threeparttable
  $table
  ,#+begin_tablenotes
  $notes
  ,#+end_tablenotes
  ,#+end_threeparttable
  ,#+end_table
#+end_src

#+header: :var table = table1
#+header: :var notes = "\\item [1] First note\n\\item [2] Second note"
#+begin_src org
  <>
#+end_src
--- >8 ---

or "%(param)s" substitutions in python (another variant f"""{param}""" 
is inconvenient for LaTeX):


--- 8< ---
#+property: header-args:python :python python3 :results org :noweb yes 
:exports results

#+property: header-args:org :exports results :results replace
#+NAME: tptable
#+begin_src python :exports none
  return """\
  ,#+begin_table
  ,#+ATTR_LaTeX: :options [b]
  ,#+begin_threeparttable
  %(table)s
  ,#+begin_tablenotes
  %(notes)s
  ,#+end_tablenotes
  ,#+end_threeparttable
  ,#+end_table
  """ % locals()
#+end_src

#+header: :var table = table1
#+header: :var notes = "\\item [1] First note\n\\item [2] Second note"
#+begin_src python
  <>
#+end_src
--- >8 ---



[proof of concept, tip] 'Templates with arguments' using org src blocks

2023-10-06 Thread Juan Manuel Macías
Hi, this is a proof of concept of how a sort of 'templates with
arguments' can be used within Org, just with Org’s own resources at the
user level. It occurred to me that org src blocks might be handy for
this. For this test I just made a disguised format string using this
function:

┌
│   (defun my-format-template (template vars)
│ (format
│"#+begin_src emacs-lisp :results raw :exports results :var template = %s
│   (format
│(format \"%%s\" template)
│   %s)
│ #+end_src" template vars))
└

and a macro:

┌
│ #+MACRO: template (eval (my-format-template $1 $2))
└

A simple example. We start from this template:

┌
│ #+NAME: template1
│ #+begin_src org :exports none
│   ,*%s*
│   %s
│ #+end_src
│ 
│ #+header: :var hello = "Hello World"
│ #+header: :var str = "Lorem ipsum dolor"
│ {{{template(template1,hello str)}}}
└

An example with a src block inside the template:

┌
│ #+NAME: template2
│ #+begin_src org :exports none
│   ,#+begin_src emacs-lisp :exports results :results raw
│   (let ((name "%s"))
│   (format "/Hello, %%s/" name))
│   ,#+end_src
│ #+end_src
│ 
│ #+header: :var name = "World"
│ {{{template(template2,name)}}}
└

And this example is somewhat more elaborate. We want a table in LaTeX
with the threeparttable package. Template:

┌
│ #+NAME: tptable
│ #+begin_src org :exports none
│   ,#+begin_table
│   ,#+ATTR_LaTeX: :options [b]
│   ,#+begin_threeparttable
│   %s
│   ,#+begin_tablenotes
│   %s
│   ,#+end_tablenotes
│   ,#+end_threeparttable
│   ,#+end_table
│ #+end_src
└

the table:

┌
│ #+NAME: table1
│ #+begin_src org :exports none
│   ,#+ATTR_LaTeX: :center nil :booktabs t :float nil
│   ,#+caption: Lorem ipsum dolor
│   ,#+name: table1
│   |---+--+---|
│   | lorem | ipsum@@latex:\tnote{1}@@ | dolor |
│   | lorem | ipsum@@latex:\tnote{2}@@ | dolor |
│   |---+--+---|
│ #+end_src
└


And, finally:

┌
│ #+header: :var table = table1
│ #+header: :var notes = "\\item [1] First note\n\\item [2] Second note"
│ {{{template(tptable,table notes)}}}
└

Best regards,

Juan Manuel

-- 
Juan Manuel Macías 

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com