emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Cannot expand macros through included org file
       [not found] <7d032f82-9d16-edff-b548-3c01f6ea68cb@housseini.me>
@ 2023-02-15 20:34 ` reza
  2023-02-15 20:44   ` Ihor Radchenko
  0 siblings, 1 reply; 8+ messages in thread
From: reza @ 2023-02-15 20:34 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 846 bytes --]

Hi List

I have the following setup:
In "template.org" is the structure of a report defined which I need to 
generate for several years, e.g:

   #+TITLE: Report

   * My headline

   #+BEGIN_SRC bash :file log_{{{year}}}.csv
     ./get_log_file --start={{{year}}}-01-01 --end={{{year}}}-12-31
   #+END_SRC

   #+BEGIN_SRC python
     log = pd.read_csv("log_{{{year}}}.csv")
   #END_SRC

Now I want to generate a report for different years, my approach was now 
to have a file "report_2019.org" like:

   #+TITLE: Report 2019
   #+MACRO: year 2019

   #+INCLUDE: "template.org" :lines "2-"


But that does not seem to work. It looks like the macro expansion does 
not happen for included content. Is this a bug or correct behavior, what 
would be the approach to not repeat the content for several reports?

Thanks for your inputs.

Cheers,
Reza

[-- Attachment #2: OpenPGP_0xC375C6AF05125C52.asc --]
[-- Type: application/pgp-keys, Size: 15557 bytes --]

[-- Attachment #3: OpenPGP_signature --]
[-- Type: application/pgp-signature, Size: 499 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cannot expand macros through included org file
  2023-02-15 20:34 ` Cannot expand macros through included org file reza
@ 2023-02-15 20:44   ` Ihor Radchenko
       [not found]     ` <ecd7d8ae-81de-5bb8-f66f-6bfc2152480b@housseini.me>
  0 siblings, 1 reply; 8+ messages in thread
From: Ihor Radchenko @ 2023-02-15 20:44 UTC (permalink / raw)
  To: reza; +Cc: emacs-orgmode@gnu.org

reza <reza@housseini.me> writes:

>    #+BEGIN_SRC bash :file log_{{{year}}}.csv
>      ./get_log_file --start={{{year}}}-01-01 --end={{{year}}}-12-31
>    #+END_SRC
>
>    #+BEGIN_SRC python
>      log = pd.read_csv("log_{{{year}}}.csv")
>    #END_SRC
> ...
> But that does not seem to work. It looks like the macro expansion does 
> not happen for included content. Is this a bug or correct behavior, what 
> would be the approach to not repeat the content for several reports?

Macro expansion only works inside Org proper markup.
Macros are only recognized withing non-verbatim contexts.
Src blocks are considered verbatim and macros are not expanded there.
Otherwise, Org would risk interfering with proper programming language
syntax.

What you can do instead is creating a named Org paragraph with macro,
like

#+name: year
{{{year}}}

and then refer to it via variable assignments.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cannot expand macros through included org file
       [not found]     ` <ecd7d8ae-81de-5bb8-f66f-6bfc2152480b@housseini.me>
@ 2023-02-15 20:53       ` reza
  2023-02-15 21:10         ` Ihor Radchenko
  2023-02-15 21:51         ` Nick Dokos
  0 siblings, 2 replies; 8+ messages in thread
From: reza @ 2023-02-15 20:53 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 618 bytes --]

Hi Ihor

Thanks for your quick reply

> What you can do instead is creating a named Org paragraph with macro,
> like
> 
> #+name: year
> {{{year}}}

I tried this approach but it does not seem to work either:

"template.org":

   #+TITLE: Report
   #+NAME: year
   {{{year}}}

   * My headline

   #+BEGIN_SRC bash :var year=year
     ./get_log_file --start=$year-01-01 --end=$year-12-31
   #+END_SRC

   #+BEGIN_SRC python :var year=year
     log = pd.read_csv(f"log_{year}.csv")
   #END_SRC

"report_2019.org":

    #+TITLE: Report 2019
    #+MACRO: year 2019

    #+INCLUDE: "template.org" :lines "2-"

Cheers,
Reza

[-- Attachment #2: OpenPGP_0xC375C6AF05125C52.asc --]
[-- Type: application/pgp-keys, Size: 15557 bytes --]

[-- Attachment #3: OpenPGP_signature --]
[-- Type: application/pgp-signature, Size: 499 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cannot expand macros through included org file
  2023-02-15 20:53       ` reza
@ 2023-02-15 21:10         ` Ihor Radchenko
       [not found]           ` <2b6f1944-66e1-c491-40f5-420018411312@housseini.me>
  2023-02-15 21:51         ` Nick Dokos
  1 sibling, 1 reply; 8+ messages in thread
From: Ihor Radchenko @ 2023-02-15 21:10 UTC (permalink / raw)
  To: reza; +Cc: emacs-orgmode@gnu.org

reza <reza@housseini.me> writes:

>> What you can do instead is creating a named Org paragraph with macro,
>> like
>> 
>> #+name: year
>> {{{year}}}
>
> I tried this approach but it does not seem to work either:

It works, but Org does not include variable assignments to exported code
by default. See org-babel-exp-code-template. The assignment would be
done if you execute the blocks though.

Note that year will have a value of "2019\n" with trailing newline.
Yet another approach is including common :var year=2009 header arg.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cannot expand macros through included org file
       [not found]           ` <2b6f1944-66e1-c491-40f5-420018411312@housseini.me>
@ 2023-02-15 21:26             ` reza
  2023-02-17 13:02               ` Ihor Radchenko
  0 siblings, 1 reply; 8+ messages in thread
From: reza @ 2023-02-15 21:26 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 490 bytes --]


> It works, but Org does not include variable assignments to exported code
> by default. See org-babel-exp-code-template. The assignment would be
> done if you execute the blocks though.

So I have to set this variable to true?

> Note that year will have a value of "2019\n" with trailing newline.

hm this is unfortunate...

> Yet another approach is including common :var year=2009 header arg.

what do you mean by common? I want to have a report for the years 
2019-2022 for example.


[-- Attachment #2: OpenPGP_0xC375C6AF05125C52.asc --]
[-- Type: application/pgp-keys, Size: 15557 bytes --]

[-- Attachment #3: OpenPGP_signature --]
[-- Type: application/pgp-signature, Size: 499 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cannot expand macros through included org file
  2023-02-15 20:53       ` reza
  2023-02-15 21:10         ` Ihor Radchenko
@ 2023-02-15 21:51         ` Nick Dokos
  1 sibling, 0 replies; 8+ messages in thread
From: Nick Dokos @ 2023-02-15 21:51 UTC (permalink / raw)
  To: emacs-orgmode

reza <reza@housseini.me> writes:

> Hi Ihor
>
> Thanks for your quick reply
>
>> What you can do instead is creating a named Org paragraph with macro,
>> like
>>
>> #+name: year
>> {{{year}}}
>
> I tried this approach but it does not seem to work either:
>
> "template.org":
>
>    #+TITLE: Report
>    #+NAME: year
>
>    {{{year}}}
>

There should be no empty line between `#+NAME: year' and `{{{year}}}'.

There is a `+' missing at the end of the python code block.

-- 
Nick

"There are only two hard problems in computer science: cache
invalidation, naming things, and off-by-one errors." -Martin Fowler



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cannot expand macros through included org file
  2023-02-15 21:26             ` reza
@ 2023-02-17 13:02               ` Ihor Radchenko
       [not found]                 ` <fc618f97-e272-5773-7143-4a1381f242e6@housseini.me>
  0 siblings, 1 reply; 8+ messages in thread
From: Ihor Radchenko @ 2023-02-17 13:02 UTC (permalink / raw)
  To: reza; +Cc: emacs-orgmode@gnu.org

reza <reza@housseini.me> writes:

>> It works, but Org does not include variable assignments to exported code
>> by default. See org-babel-exp-code-template. The assignment would be
>> done if you execute the blocks though.
>
> So I have to set this variable to true?

Check out the docstring.
<F1> v org-babel-exp-code-template <RET>

>> Yet another approach is including common :var year=2009 header arg.
>
> what do you mean by common? I want to have a report for the years 
> 2019-2022 for example.

report_2019.org:

#+TITLE: Report 2019
#+property: header-args :var year=2019

#+INCLUDE: "template.org" :lines "2-"

template.org:

#+TITLE: Report

* My headline

#+begin_src emacs-lisp :exports both :results value :eval yes
year
#+end_src

#+BEGIN_SRC bash
./get_log_file --start=$year-01-01 --end=$year-12-31
#+END_SRC

#+BEGIN_SRC python
log = pd.read_csv(f"log_{year}.csv")
#+END_SRC


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Cannot expand macros through included org file
       [not found]                 ` <fc618f97-e272-5773-7143-4a1381f242e6@housseini.me>
@ 2023-02-17 13:05                   ` reza
  0 siblings, 0 replies; 8+ messages in thread
From: reza @ 2023-02-17 13:05 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 841 bytes --]

> report_2019.org:
> 
> #+TITLE: Report 2019
> #+property: header-args :var year=2019
> 
> #+INCLUDE: "template.org" :lines "2-"
> 
> template.org:
> 
> #+TITLE: Report
> 
> * My headline
> 
> #+begin_src emacs-lisp :exports both :results value :eval yes
> year
> #+end_src
> 
> #+BEGIN_SRC bash
> ./get_log_file --start=$year-01-01 --end=$year-12-31
> #+END_SRC
> 
> #+BEGIN_SRC python
> log = pd.read_csv(f"log_{year}.csv")
> #+END_SRC

Thank you, I got it to work by only using

report_2019.org:

#+TITLE: Report 2019
#+property: header-args :var year=2019

#+INCLUDE: "template.org" :lines "2-"

template.org:

#+TITLE: Report

* My headline

#+BEGIN_SRC bash
./get_log_file --start=$year-01-01 --end=$year-12-31
#+END_SRC

#+BEGIN_SRC python
log = pd.read_csv(f"log_{year}.csv")
#+END_SRC

[-- Attachment #2: OpenPGP_0xC375C6AF05125C52.asc --]
[-- Type: application/pgp-keys, Size: 15557 bytes --]

[-- Attachment #3: OpenPGP_signature --]
[-- Type: application/pgp-signature, Size: 499 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-02-17 13:06 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <7d032f82-9d16-edff-b548-3c01f6ea68cb@housseini.me>
2023-02-15 20:34 ` Cannot expand macros through included org file reza
2023-02-15 20:44   ` Ihor Radchenko
     [not found]     ` <ecd7d8ae-81de-5bb8-f66f-6bfc2152480b@housseini.me>
2023-02-15 20:53       ` reza
2023-02-15 21:10         ` Ihor Radchenko
     [not found]           ` <2b6f1944-66e1-c491-40f5-420018411312@housseini.me>
2023-02-15 21:26             ` reza
2023-02-17 13:02               ` Ihor Radchenko
     [not found]                 ` <fc618f97-e272-5773-7143-4a1381f242e6@housseini.me>
2023-02-17 13:05                   ` reza
2023-02-15 21:51         ` Nick Dokos

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).