# Weave how to print markdown in for loop?

**URL:** <https://discourse.julialang.org/t/weave-how-to-print-markdown-in-for-loop/24643>\
**Category:** General Usage\
**Tags:** markdown, weave\
**Created:** [May 27, 2019, 8:40am UTC](https://discourse.julialang.org/t/weave-how-to-print-markdown-in-for-loop/24643 "2019-05-27T08:40:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [May 27, 2019, 8:40am UTC](https://discourse.julialang.org/t/weave-how-to-print-markdown-in-for-loop/24643/1 "2019-05-27T08:40:17Z")

</div>

I currently trying to use [GitHub - JunoLab/Weave.jl: Scientific reports/literate programming for Julia](https://github.com/mpastell/Weave.jl) but not really getting my head around its usage.  
In general I need more examples of the usage of julia markdown.  
My current problem: I want to do the same plot for x folders and it should have a header (outside of the plots environment)  
How do I achieve outputting markdown down in my julia code chunk? I assumed it works like this:

```julia
for dir_name in dirs
    println("## "*dir_name)
    display(plot_all(dir_name))
end

```

But `results="markup"` doesn’t seem to have an effect.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 27, 2019, 9:18am UTC](https://discourse.julialang.org/t/weave-how-to-print-markdown-in-for-loop/24643/2 "2019-05-27T09:18:56Z")

</div>

````nohighlight
# Markdown test
```julia; results="raw"
using Plots, Markdown
dirs = ["a", "b"]
for dir_name in dirs
    show(stdout, "text/html", Markdown.parse("## $dir_name title"))
    show(stdout, "text/html", plot(rand(30), title=dir_name))
end
```

````

works fine if you’re weaving to HTML. `show(..., "text/latex", ...)` should work for pdf output, but I didn’t actually try that.

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [May 27, 2019, 9:32am UTC](https://discourse.julialang.org/t/weave-how-to-print-markdown-in-for-loop/24643/3 "2019-05-27T09:32:38Z")

</div>

Thanks. Additionally is there a way to use get the `doctype` from weave and use it in the the `jmd` file? So that it can automatically use `text/html` or `test/latext` ?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 27, 2019, 10:01am UTC](https://discourse.julialang.org/t/weave-how-to-print-markdown-in-for-loop/24643/4 "2019-05-27T10:01:45Z")

</div>

````nohighlight
# Markdown test
```julia; results="raw"
using Plots, Markdown
dirs = ["a", "b"]

mimes = Dict(
    "tex" => "text/latex",
    "html" => "text/html"
)
mime = mimes[Base.Multimedia.displays[end].formatdict[:extension]]
for dir_name in dirs
    show(stdout, mime, Markdown.parse("## $dir_name title"))
    show(stdout, mime, plot(rand(30), title=dir_name))
end
```

````

Unfortunately `show(stdout, "text/latex", ...)` usually doesn’t work. Not sure how to do this properly.

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [May 27, 2019, 10:16am UTC](https://discourse.julialang.org/t/weave-how-to-print-markdown-in-for-loop/24643/5 "2019-05-27T10:16:31Z")

</div>

Nice. Works for me but I don’t know how to have a reasonable size of the plots.  
I’m saving them to a png file now as plotting takes quite a bit time (~500,000 dots in a scatter) and include them with `![](foobar.png)` which looks nice in html and only a fraction is shown in the pdf. The size of the plots is 1920x1080. No idea where I can uses the options mentioned here:  
[http://weavejl.mpastell.com/stable/chunk\_options/#Options-for-figures-1](http://weavejl.mpastell.com/stable/chunk_options/#Options-for-figures-1)

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 27, 2019, 10:19am UTC](https://discourse.julialang.org/t/weave-how-to-print-markdown-in-for-loop/24643/6 "2019-05-27T10:19:14Z")

</div>

Should be able to just append them after a `;`, so e.g.

````nohighlight
```julia; results="raw"; out_width="12cm"

````

or whatever.

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [May 27, 2019, 10:30am UTC](https://discourse.julialang.org/t/weave-how-to-print-markdown-in-for-loop/24643/8 "2019-05-27T10:30:07Z")

</div>

So what I tried:

````julia
```julia; echo=false
mimes = Dict(
    "tex" => "text/latex",
    "html" => "text/html"
);
mime = mimes[Base.Multimedia.displays[end].formatdict[:extension]]
function m2o(ex)
    show(stdout, mime, Markdown.parse(ex))
end;

````

and

````julia
```julia; echo=false; results="raw"; out_width="12cm"
for dir_name in dirs
    m2o("## "*dir_name)
    png(plot(rand(30), size=(1920,1080)), "visuals/"*dir_name*"_combined_dgs")
    m2o("![$dir_name](visuals/"*dir_name*"_combined_dgs.png)")
end

````

 ![screenshot](https://global.discourse-cdn.com/julialang/original/3X/6/e/6e4084b8ad0e5c1f84e55537fed0a0f0c945a919.png)

Don’t know how to have code chunks in code 😃 on this website
