-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.Rmd
215 lines (151 loc) · 8.9 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
title: "My Tikz Bars"
author: "Alfonso R. Reyes"
output:
html_document:
css: style.css
keep_md: true
toc: true
number_sections: false
---
```{r, include=FALSE}
# These are variables that need to be set for any TikZ website
website_url <- "https://github.com/f0nzie/tikz_bars"
source_dir <- "src"
output_dir <- "out"
tikz_ext <- ".tex"
image_ext <- ".png"
png_dir <- output_dir
data_files <- file.path(source_dir, "data")
cls_sty_files <- file.path(source_dir, "texmf")
url_source <- paste(website_url, "blob/master", source_dir, sep = "/")
dir_level <- "./"
# This will generate the template that will be used to add a TikZ page per graphic
# gsub makes many replacements per line. very greedy
library(magrittr)
readLines('template.Rmd') %>%
gsub("\\{png_dir\\}", png_dir, .) %>%
gsub("\\{image_ext\\}", image_ext, .) %>%
# sub only replaces one occurrence per line
gsub("\\{tikz_ext\\}", tikz_ext, .) %>%
gsub("\\{source_dir\\}", source_dir, .) %>%
gsub("\\{url_source\\}", url_source, .) %>%
gsub("\\{dir_level\\}", dir_level, .) %>%
writeLines("_template.Rmd") # this should be deleted at the end
# TODO: use a temporary file for this.
```
## NEWS
* Output files like .png and .pdf are now being stored in `./out`
* The `README.Rmd` document now allows to customize variables for the folders
* The Hugo website through the `_build_site.R` script also allow some customization
* The source folder with TikZ files are now only saved in `src`. Only `tex` files
* The template file `template.Rmd` includes now special identifiers so it can be updated with user custom settings that `child.Rmd` uses to retrieve all graphics and code for each of the examples
* The `Makefile` has been optimized to avoid code repetition
* All data files for the TikZ examples now live under `src/data`
## Introduction
This is a repository of the best TikZ examples I have found around the web and from papers, slides, tutorials, and books. Most of the examples are simple enough to encourage anyone to start learning TikZ. By having the result (graphic) on a web page, and be able to click on it to see the code, it rapidly helps to make the connection between **code** and **output**. I find this idea fantastic.
TikZ is ideal for any kind of publications either websites, blogs, papers, articles, slides, or books because they are portable and reproducible. TikZ files are text not binaries. They don't require mouse and clicks but writing code to connect objects and elements. These object can be very simple or as sophisticated as you want. TikZ graphics can be built under any LaTex environment for any operating system: Windows, Linux or Mac OS.
There are hundreds of libraries for TikZ in all scientific fields. That makes it easier to build advanced graphics for practically any domain or discipline because you are able to start from code already written.
## Motivation
While working with Latex and TikZ file I had this question:
**How can I use the power of R to organize TikZ related files?**
**R** is very friendly to Latex and TikZ through the packages `knitr` and `rmarkdown`.
TikZ involves a source file, which carries the `tex` extension; the PDF that is generated by your Latex compiler and editor (I use *TexStudio*); the graphics output file -that could be anything imaginable. I use `png` files.
## How this README is built
This `README` is smartly generated with TikZ graphics and code located in the `src` folder.
This means that as you are adding, building, designing new TikZ drawings, they become available to be automatically included in this README file. It uses some `knitr` tricks. But the whole thing is awesome.
The magic is provided by:
* the file `template.Rmd`
* the chunks of code that you can see in this `README`
## Deployment of website in GitHub Pages (GHP)
This is a very interesting article about publshing your TikZ site: [Simple deployment to GH Pages](https://www.gnu.org/software/make/manual/html_node/Conditional-Syntax.html). I chose to publish it in `/docs` instead of the `public` folder because it is much easier to deploy. GHP does not allow to publish a website on a folder named `public`, unfortunately.
## Thanks. Original repository and idea
I found this valuable resource while exploring TikZ and R: https://github.com/walmes/Tikz
It is pretty neat and relatively simple to use and personalize because it uses Hugo, a generator of static websites; the same engine that is used by `blogdown`, `bookdown`, and others.
Thanks to **Walmes Zeviani** for the great idea of combining Latex, TikZ, R and Hugo.
Source: https://github.com/walmes/Tikz
Web: http://leg.ufpr.br/~walmes/Tikz/
## Summary
This is my collection of favorite TikZ graphics. It is necessary to store and organize them as gallery because it makes it easier to peek at the code for new designs.
Some useful tutorials:
* [TikZ Adventskalender](https://tobiw.de/en/teotm/tikz-adventskalender)
* [TikZ for the Impatient](http://math.et.info.free.fr/TikZ/bdd/TikZ-Impatient.pdf)
* <http://www.mat.ufpb.br/lenimar/introtikz.pdf>
* [LaTeX for Economists](http://nfpoitiers.eu/wp-content/uploads/2018/08/3-presentations_and_graphics.pdf)
* [Configurable Graph Drawing Algorithmsfor the TikZ Graphics Description Language](https://www.tcs.uni-luebeck.de/downloads/papers/2011/2011-configurable-graph-drawing-algorithms-jannis-pohlmann.pdf)
* [TikZ for Economics By Chiu Yu Ko](https://www.google.com/books/edition/_/t3ZZDwAAQBAJ?hl=en&gbpv=0)
* <http://www.statistiker-wg.de/pgf/tutorials.htm>
* [A very minimal introduction to TikZ](https://cremeronline.com/LaTeX/minimaltikz.pdf)
* [TikZ Tutorial](https://indico.scc.kit.edu/event/48/contributions/3405/attachments/1693/2321/tikz_tutorial.pdf)
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
comment = NA,
results = "asis",
error = FALSE,
warning = FALSE,
message = FALSE)
a <- "```tex"
b <- "```"
```
```{r, include=FALSE}
# add file suffix for file selection
suffix <- ""
ext <- "png"
pattern <- paste0("^", suffix, ".*", "\\.", ext, "$")
if (file.exists("child.Rmd")) file.remove("child.Rmd")
tmp <- readLines("_template.Rmd")
# get all PGF files
pgf <- sub(x = list.files(path = source_dir, pattern = "*.pgf$"),
pattern = "\\.pgf$",
replacement = "")
tikz_files_all <- list.files(path = source_dir, pattern = "*\\.tex$")
tikz_lualatex <- grep(pattern = "\\.lualatex\\.tex$", x = tikz_files_all, value = TRUE)
tikz_pdflatex <- grep(pattern = "^.*(?<!\\.lualatex\\.tex)$", x = tikz_files_all,
value = TRUE, perl = TRUE)
data_files <- list.files(path = data_files, pattern = "*\\.dat$|*\\.csv$|*\\.txt$")
texmf_files <- list.files(path = cls_sty_files)
tex <- sub(x = tikz_files_all,
pattern = "\\.tex$",
replacement = "")
# get all PNG files
png <- sub(x = list.files(path = output_dir, pattern = pattern),
pattern = "\\.png$",
replacement = "")
if (length(png) == 0) stop("No TikZ objects available")
# Common tex and png files
# if a PDF has its PNG generated file
i <- intersect(tex, png)
```
```{r include=FALSE}
# create notebook child.Rmd
sapply(i,
FUN = function(i){
tmpi <- gsub(x = tmp,
pattern = "&PGF_FILE&",
replacement = i)
cat(tmpi,
file = "child.Rmd",
append = TRUE,
sep= "\n")
})
```
## Useful statistics
* There are `r length(i)` total Tikz figures saved as `.tex` files in this gallery.
The figures are sorted by filename.
* There are `r length(tikz_pdflatex)` files under `src/` to be compiled with `pdflatex`
* There are `r length(tikz_lualatex)` files under `src/` to be compiled with `lualatex`
* There are `r length(data_files)` data files under the folder `src/data` that are being used by the TikZ scripts
* There are `r length(texmf_files)` Latex classes, styles and library files under the `src/texmf` folder
## TikZ graphics and code
```{r, child = "child.Rmd"}
```
```{r}
# delete dummy template
invisible(if (file.exists("_template.Rmd")) file.remove("_template.Rmd") )
```
## Implementation of a simple tag system for files
I am implementing a very primitive tag system based of identifiers that are included in the filenames. I started to do this in TikZ because the graphics could belong to different categories at the same time. My plan is using what is available in the operating systems. Tools like `grep`, `find`, `regex`, etc., should make this possible. I have done a pilot of this and seems to be working.
To tag the file -as you will see in the repository- I use the plus sign. I tried different symbols and this symbol is not obfuscated as a search string.
* Characters that pass the search test at OS level: `@`, `!`, `=`, `+`.
* Character that do not pass: `#`.
> Other possibilities include using the comments section of the TikZ file to add the tags and categories.