# Literate programming style macros in Literate.jl or Weave.jl

**URL:** <https://discourse.julialang.org/t/literate-programming-style-macros-in-literate-jl-or-weave-jl/60967>\
**Category:** General Usage\
**Tags:** literate-programming, weave\
**Created:** [May 11, 2021, 4:31pm UTC](https://discourse.julialang.org/t/literate-programming-style-macros-in-literate-jl-or-weave-jl/60967 "2021-05-11T16:31:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [May 11, 2021, 4:31pm UTC](https://discourse.julialang.org/t/literate-programming-style-macros-in-literate-jl-or-weave-jl/60967/1 "2021-05-11T16:31:35Z")

</div>

Dear All,

Is there any way of implementing “literate programming style macro” in `Literate.jl` or `Weave.jl`? My goal is to write a tutorial-style literate program in a logical order for the human reader. For example:

```julia
# Here is a complicated thing that we want to do. Given two numbers `a` and `b`, 
# we want to add them and store them in `c`.
    
<some_complicated_thing>=
c = a + b    

# But to do that, we first need to verify where a particular condition 
# on `a, b` holds.
        
<my_condition>=
a == 1 && b == 2

    
# Here is the function that does everything in one shot.
        
function complicated_function(a,b)

     if <my_condition>
          <some_complicated_thing>
     end

     return c

end   

```

The function above should be the same as:

```julia
function complicated_function(a,b)

     if a == 1 && b == 2
         c = a + b  
     end
     
     return c

end      

```

Is there a way of executing something similar in `Literate.jl` or `Weave.jl`? Any tips will be much appreciated!

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [May 11, 2021, 7:00pm UTC](https://discourse.julialang.org/t/literate-programming-style-macros-in-literate-jl-or-weave-jl/60967/2 "2021-05-11T19:00:07Z")

</div>

I would not want to do this with macros.  
this kind of literate program I assume comes from one of the original notions of literate programming by Knuth.  
Knuth is also the creator of the most popular macro-centric programming language around: TeX.  
TeX doesn’t have functions, only macros.  
(though its macros are a bit safer looking than that…)  
Here your code is bound up with variable names that have to match those that are used later.

I would write it with functions

```julia
# Here is a complicated thing that we want to do. Given two numbers `a` and `b`, 
# we want to add them
    
some_complicated_thing(a, b) = a + b    

# But to do that, we first need to verify where a particular condition 
# on `a, b` holds.
        
my_condition(a, b) = a == 1 && b == 2

    
# Here is the function that does everything in one shot.
        
function complicated_function(a,b)

     if my_condition(a, b)
          c = some_complicated_thing(a, b)
     end

     return c

end

```

---

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [May 11, 2021, 7:24pm UTC](https://discourse.julialang.org/t/literate-programming-style-macros-in-literate-jl-or-weave-jl/60967/3 "2021-05-11T19:24:35Z")

</div>

Thanks for your response, @oxinabox. The function-based method is a nice way of achieving similar behavior. Yes, `<>=` example indeed comes from original notions of literate programming by Knuth, I was wondering if one can also do the same thing in `Literate.jl` or `Weave.jl` as well, which are inspired by Knuth’s literate programming.

Thanks again!
