samcarter
In a readme file, I'd like to build upon a list of items which I've already defined in a latex file as a comma separated list `\def\mylist{duck,bear,owl}`.
Instead of trying to keep both lists updated, I was wondering if I can't just generate the readme file from my tex file.
The resulting readme file should look like this:
```
# Title
Some text
![](./pngs/duck.png)
![](./pngs/bear.png)
![](./pngs/owl.png)
```
The latex file approximately looks like this:
```
\documentclass{article}
\usepackage{pgffor}
\def\mylist{duck,bear,owl}
\begin{document}
\foreach \x in \mylist {%
\x\par
}
\end{document}
```
Top Answer
samcarter
One possible approach using tex's `\newwrite`, `\openout` and `\write`:
```
\documentclass{article}
\usepackage{pgffor}
\def\mylist{duck,bear,owl}
\newwrite\readme
\immediate\openout\readme=README.md
\begin{document}
\immediate\write\readme{\string# Title
^^J
^^JSome text
^^J
}
\foreach \x in \mylist {%
\x\par
\immediate\write\readme{![](./pngs/\x.png)}
}
\immediate\closeout\readme
\end{document}
```
Answer #2
Skillmon
Using `expl3`:
```
\def\mylist{duck,bear,owl}
\ExplSyntaxOn
% since \mylist is not an expl3-clist and relying on it working as N would be to
% rely on an implementation detail:
\cs_generate_variant:Nn \clist_map_function:nN { o }
\cs_new:Npn \samcarter_img_format:n #1
{
\iow_newline:
![](./pngs/#1.png)
}
\iow_new:N \g_samcarter_readme_iow
\iow_open:Nn \g_samcarter_readme_iow { \jobname.md }
\iow_now:Ne \g_samcarter_readme_iow
{
\token_to_str:N #~ Title\iow_newline:
\iow_newline:
Some~ text\iow_newline:
\clist_map_function:oN { \mylist } \samcarter_img_format:n
}
\ExplSyntaxOff
\stop
```
Results in
```markdown
# Title
Some text
![](./pngs/duck.png)
![](./pngs/bear.png)
![](./pngs/owl.png)
```