0

I'm using bookdown to type up my notes from some of my math courses. I want to insert tikzpictures into my book, and even though they render perfectly when using render_book("index.Rmd", "pdf_book"), they do not appear at all, on any browser (I've tried Chrome, Firefox, and even Internet Explorer) when I use render_book("index.Rmd", "gitbook"). Likewise when using preview_chapter instead of render_book.

Here is the code that can be used to render my Tikz image:

\def\firstcircle{(0:-0.5cm) circle (1.5cm)}
\def\secondcircle{(0:0.4cm) circle (0.5cm)}

\colorlet{circle edge}{blue!50}
\colorlet{circle area}{blue!20}

\tikzset{filled/.style={fill=circle area, draw=circle edge, thick},
    outline/.style={draw=circle edge, thick}}

\begin{figure}
\centering
\begin{tikzpicture}
    \begin{scope}
        \clip \firstcircle;
        \secondcircle;
    \end{scope}
    \draw[outline] \firstcircle node {$B$};
    \draw[outline] \secondcircle node {$A$};
\end{tikzpicture}
\caption{$A$ as a subset of $B$}
\end{figure}

When I use pdf_book it's beautiful. If I use gitbook it just does not appear. I've tried to do something similar as what's described in this question here i.e. using the same chunk but replacing that code with my code (though I did center mine) like so:

```{r, echo=FALSE, engine='tikz', out.width='90%', fig.ext='pdf', fig.align='center', fig.cap='Some caption.'}
\def\firstcircle{(0:-0.5cm) circle (1.5cm)}
\def\secondcircle{(0:0.4cm) circle (0.5cm)}

\colorlet{circle edge}{blue!50}
\colorlet{circle area}{blue!20}

\tikzset{filled/.style={fill=circle area, draw=circle edge, thick},
    outline/.style={draw=circle edge, thick}}

\begin{tikzpicture}
    \begin{scope}
        \clip \firstcircle;
        \secondcircle;
    \end{scope}
    \draw[outline] \firstcircle node {$B$};
    \draw[outline] \secondcircle node {$A$};
\end{tikzpicture}
```

when I do this, again it renders beautifully in pdfbook and actually I get further in gitbook (the figure caption appears and a "broken image link" symbol appears, I've tried across browsers, as mentioned) but still no image.

Any ideas on how I can get this to work?

Julian
  • 451
  • 5
  • 14

1 Answers1

1

With fig.ext='pdf' you are creating a PDF file, that your browser cannot include. Instead, you can use something like fig.ext=if(knitr:::is_latex_output()) 'pdf' else 'png' to use PDF output together with LaTeX and PNG output for all other cases. Alternatively, you could remove fig.ext completely and use the defaults. Given one of these changes, you example works for me with both HTML/Gitbook and PDF/LaTeX output.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75