Last active
September 3, 2021 16:35
-
-
Save andrewheiss/55f06b079cb7064f2de5b2c174546fae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "TikZ fun!" | |
output: | |
pdf_document: default | |
--- | |
Here's an empty DAG | |
```{tikz, echo=FALSE, cache=TRUE, fig.cap="Empty DAG", fig.align="center"} | |
\usetikzlibrary{positioning} | |
\begin{tikzpicture}[every node/.append style={draw, minimum size=0.5cm}] | |
\node [circle] (X) at (0,0) {}; | |
\node [circle] (Y) at (2,0) {}; | |
\node [rectangle] (Z) at (1,1) {}; | |
\path [-latex] (X) edge (Y); | |
\draw [-latex] (Z) edge (Y); | |
\draw [-latex] (Z) edge (X); | |
\end{tikzpicture} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "TikZ fun!" | |
output: | |
html_document: | |
self_contained: no | |
--- | |
Here's an empty DAG: | |
```{tikz empty-dag, echo=FALSE, fig.cap="Empty DAG", fig.align="center", fig.ext="svg"} | |
\usetikzlibrary{positioning} | |
\begin{tikzpicture}[every node/.append style={draw, minimum size=0.5cm}] | |
\node [circle] (X) at (0,0) {}; | |
\node [circle] (Y) at (2,0) {}; | |
\node [rectangle] (Z) at (1,1) {}; | |
\path [-latex] (X) edge (Y); | |
\draw [-latex] (Z) edge (Y); | |
\draw [-latex] (Z) edge (X); | |
\end{tikzpicture} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "TikZ fun!" | |
output: | |
html_document: | |
self_contained: no | |
--- | |
```{r point-to-ghostscript, include=FALSE} | |
Sys.setenv(LIBGS = "/usr/local/share/ghostscript/9.53.3/lib/libgs.dylib.9.53") | |
``` | |
Here's an empty DAG: | |
```{tikz empty-dag, echo=FALSE, fig.cap="Empty DAG", fig.align="center", fig.ext="svg"} | |
\usetikzlibrary{positioning} | |
\begin{tikzpicture}[every node/.append style={draw, minimum size=0.5cm}] | |
\node [circle] (X) at (0,0) {}; | |
\node [circle] (Y) at (2,0) {}; | |
\node [rectangle] (Z) at (1,1) {}; | |
\path [-latex] (X) edge (Y); | |
\draw [-latex] (Z) edge (Y); | |
\draw [-latex] (Z) edge (X); | |
\end{tikzpicture} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "TikZ fun!" | |
output: | |
html_document: | |
self_contained: no | |
pdf_document: default | |
--- | |
```{r point-to-ghostscript, include=FALSE} | |
Sys.setenv(LIBGS = "/usr/local/share/ghostscript/9.53.3/lib/libgs.dylib.9.53") | |
``` | |
```{r setup, include=FALSE} | |
# Conditional tikz output types; use PDF if this is LaTeX, otherwise use SVG | |
if (knitr::is_latex_output()) { | |
knitr::opts_template$set( | |
tikz_settings = list(fig.ext = "pdf", fig.align = "center") | |
) | |
} else { | |
knitr::opts_template$set( | |
tikz_settings = list(fig.ext = "svg", fig.align = "center") | |
) | |
} | |
``` | |
Here's an empty DAG: | |
```{tikz empty-dag, echo=FALSE, fig.cap="Empty DAG", fig.align="center", fig.ext="svg"} | |
\usetikzlibrary{positioning} | |
\begin{tikzpicture}[every node/.append style={draw, minimum size=0.5cm}] | |
\node [circle] (X) at (0,0) {}; | |
\node [circle] (Y) at (2,0) {}; | |
\node [rectangle] (Z) at (1,1) {}; | |
\path [-latex] (X) edge (Y); | |
\draw [-latex] (Z) edge (Y); | |
\draw [-latex] (Z) edge (X); | |
\end{tikzpicture} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "TikZ fun!" | |
output: | |
html_document: | |
self_contained: no | |
--- | |
```{r point-to-ghostscript, include=FALSE} | |
Sys.setenv(LIBGS = "/usr/local/share/ghostscript/9.53.3/lib/libgs.dylib.9.53") | |
``` | |
```{r setup, include=FALSE} | |
# Conditional tikz output types; use PDF if this is LaTeX, otherwise use SVG | |
if (knitr::is_latex_output()) { | |
knitr::opts_template$set( | |
tikz_settings = list(fig.ext = "pdf", fig.align = "center") | |
) | |
} else { | |
knitr::opts_template$set( | |
tikz_settings = list(fig.ext = "svg", fig.align = "center") | |
) | |
} | |
``` | |
Here's a DAG with text: | |
```{tikz dag-text, echo=FALSE, fig.cap="DAG with text", opts.label="tikz_settings"} | |
\usetikzlibrary{positioning} | |
\begin{tikzpicture}[every node/.append style={draw, minimum size=0.5cm}] | |
\node [draw=none] (X) at (0,0) {$X_{it}$}; | |
\node [draw=none] (Y) at (2,0) {$Y_{it}$}; | |
\node [rectangle] (Z) at (1,1) {$Z$}; | |
\path [-latex] (X) edge (Y); | |
\draw [-latex] (Z) edge (Y); | |
\draw [-latex] (Z) edge (X); | |
\end{tikzpicture} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "TikZ fun!" | |
output: | |
html_document: | |
self_contained: no | |
pdf_document: default | |
--- | |
```{r point-to-ghostscript, include=FALSE} | |
Sys.setenv(LIBGS = "/usr/local/share/ghostscript/9.53.3/lib/libgs.dylib.9.53") | |
``` | |
```{r setup, include=FALSE} | |
# Conditional tikz output types; use PDF if this is LaTeX, otherwise use SVG with embedded fonts | |
if (knitr::is_latex_output()) { | |
knitr::opts_template$set( | |
tikz_settings = list(fig.ext = "pdf", fig.align = "center") | |
) | |
} else { | |
knitr::opts_template$set( | |
tikz_settings = list(fig.ext = "svg", fig.align = "center", | |
engine.opts = list(dvisvgm.opts = "--font-format=woff")) | |
) | |
} | |
``` | |
Here's a DAG with text: | |
```{tikz dag-text, echo=FALSE, fig.cap="DAG with text", opts.label="tikz_settings"} | |
\usetikzlibrary{positioning} | |
\begin{tikzpicture}[every node/.append style={draw, minimum size=0.5cm}] | |
\node [draw=none] (X) at (0,0) {$X_{it}$}; | |
\node [draw=none] (Y) at (2,0) {$Y_{it}$}; | |
\node [rectangle] (Z) at (1,1) {$Z$}; | |
\path [-latex] (X) edge (Y); | |
\draw [-latex] (Z) edge (Y); | |
\draw [-latex] (Z) edge (X); | |
\end{tikzpicture} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "TikZ fun!" | |
output: | |
html_document: | |
self_contained: no | |
pdf_document: default | |
--- | |
```{r svg-settings, include=FALSE} | |
Sys.setenv(LIBGS = "/usr/local/share/ghostscript/9.53.3/lib/libgs.dylib.9.53") | |
embed_svg_fonts <- function(before, options, envir) { | |
# !before means after the chunk is run | |
if (!before) { | |
# Get a list of all the plot files this chunk makes | |
paths <- knitr:::get_plot_files() | |
# Loop through all the plot files. When dealing with SVG, knitr actually | |
# creates two different files: an initial DVI and the converted SVG. We'll | |
# use the original DVI and pass it through dvisvgm again, this time with | |
# --font-format=woff enabled. This will overwrite the SVG that knitr makes | |
knitr:::in_base_dir( | |
lapply(paths, function(x) { | |
message("Embedding fonts in ", x) | |
path_svg <- xfun::with_ext(x, "svg") | |
path_dvi <- xfun::with_ext(x, "dvi") | |
if (system2('dvisvgm', c('--font-format=woff', '-o', shQuote(path_svg), shQuote(path_dvi))) != 0) | |
stop('Failed to convert ', path_dvi, ' to ', path_svg) | |
}) | |
) | |
} | |
} | |
# Register the function as a possible hook | |
# Use this in a chunk by setting embed_svg_fonts=TRUE in the chunk options | |
knitr::knit_hooks$set(embed_svg_fonts = embed_svg_fonts) | |
``` | |
```{r setup, include=FALSE} | |
# Conditional tikz output types; use PDF if this is LaTeX, otherwise use SVG | |
if (knitr::is_latex_output()) { | |
knitr::opts_template$set( | |
tikz_settings = list(fig.ext = "pdf", fig.align = "center") | |
) | |
} else { | |
knitr::opts_template$set( | |
tikz_settings = list(fig.ext = "svg", fig.align = "center", embed_svg_fonts = TRUE) | |
) | |
} | |
``` | |
Here's a DAG with text: | |
```{tikz dag-text, echo=FALSE, fig.cap="DAG with text", opts.label="tikz_settings"} | |
\usetikzlibrary{positioning} | |
\begin{tikzpicture}[every node/.append style={draw, minimum size=0.5cm}] | |
\node [draw=none] (X) at (0,0) {$X_{it}$}; | |
\node [draw=none] (Y) at (2,0) {$Y_{it}$}; | |
\node [rectangle] (Z) at (1,1) {$Z$}; | |
\path [-latex] (X) edge (Y); | |
\draw [-latex] (Z) edge (Y); | |
\draw [-latex] (Z) edge (X); | |
\end{tikzpicture} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "TikZ fun!" | |
output: | |
html_document: default | |
--- | |
```{r point-to-ghostscript, include=FALSE} | |
Sys.setenv(LIBGS = "/usr/local/share/ghostscript/9.53.3/lib/libgs.dylib.9.53") | |
``` | |
```{r setup, include=FALSE} | |
font_opts <- list(extra.preamble = c("\\usepackage{gfsartemisia-euler}"), | |
dvisvgm.opts = "--font-format=woff") | |
``` | |
```{tikz rectangle-text, fig.ext="svg", echo=FALSE, fig.align="center", engine.opts=font_opts} | |
\begin{tikzpicture} | |
\draw (0,0) rectangle (6, 2) node[midway, align=center] {I am a rectangle with math: \\ $\hat{y} = \beta_0 + \beta_1 x_1 + \text{Some text} + \varepsilon$}; | |
\end{tikzpicture} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "TikZ fun!" | |
output: | |
html_document: default | |
--- | |
```{r point-to-ghostscript, include=FALSE} | |
Sys.setenv(LIBGS = "/usr/local/share/ghostscript/9.53.3/lib/libgs.dylib.9.53") | |
``` | |
```{r setup, include=FALSE} | |
font_opts <- list(extra.preamble = c("\\usepackage{libertine}", "\\usepackage{libertinust1math}"), | |
dvisvgm.opts = "--font-format=woff") | |
``` | |
```{tikz rectangle-text, fig.ext="svg", echo=FALSE, fig.align="center", engine.opts=font_opts} | |
\begin{tikzpicture} | |
\draw (0,0) rectangle (6, 2) node[midway, align=center] {I am a rectangle with math: \\ $\hat{y} = \beta_0 + \beta_1 x_1 + \text{Some text} + \varepsilon$}; | |
\end{tikzpicture} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "TikZ fun!" | |
output: | |
pdf_document: | |
latex_engine: xelatex | |
--- | |
```{r setup, include=FALSE} | |
options(tinytex.engine = "xelatex") | |
font_opts <- list(extra.preamble = c("\\usepackage{fontspec}", | |
"\\setmainfont{Comic Sans MS}", | |
"\\usepackage{unicode-math}", | |
"\\usepackage{mathastext}", | |
"\\setmathfont{Comic Sans MS}")) | |
``` | |
```{tikz rectangle-text, echo=FALSE, fig.align="center", engine.opts=font_opts} | |
\begin{tikzpicture} | |
\draw (0,0) rectangle (6, 2) node[midway, align=center] {I am a rectangle with math: \\ $\hat{y} = \beta_0 + \beta_1 x_1 + \text{Some text} + \varepsilon$}; | |
\end{tikzpicture} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment