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

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

# 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
1 Like