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:

```
# 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:

```
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!

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

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!