all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: orgmode <emacs-orgmode@gnu.org>
Subject: [proof of concept, tip] 'Templates with arguments' using org src blocks
Date: Fri, 06 Oct 2023 08:07:54 +0000	[thread overview]
Message-ID: <87bkdc5g2t.fsf@posteo.net> (raw)

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




             reply	other threads:[~2023-10-06  8:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-06  8:07 Juan Manuel Macías [this message]
2023-10-13 16:00 ` [proof of concept, tip] 'Templates with arguments' using org src blocks Max Nikulin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bkdc5g2t.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.