* [proof of concept, tip] 'Templates with arguments' using org src blocks
@ 2023-10-06 8:07 Juan Manuel Macías
2023-10-13 16:00 ` Max Nikulin
0 siblings, 1 reply; 2+ messages in thread
From: Juan Manuel Macías @ 2023-10-06 8:07 UTC (permalink / raw)
To: orgmode
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [proof of concept, tip] 'Templates with arguments' using org src blocks
2023-10-06 8:07 [proof of concept, tip] 'Templates with arguments' using org src blocks Juan Manuel Macías
@ 2023-10-13 16:00 ` Max Nikulin
0 siblings, 0 replies; 2+ messages in thread
From: Max Nikulin @ 2023-10-13 16:00 UTC (permalink / raw)
To: Juan Manuel Macías, orgmode
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
<<tptable>>
#+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
<<tptable>>
#+end_src
--- >8 ---
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-10-13 16:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-06 8:07 [proof of concept, tip] 'Templates with arguments' using org src blocks Juan Manuel Macías
2023-10-13 16:00 ` Max Nikulin
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).