首页 > 解决方案 > Tikzpictures 不在 gitbook 中呈现,即使它们出现在 pdf_book 中

问题描述

我过去bookdown常常从我的一些数学课程中输入我的笔记。我想将 tikzpictures 插入到我的书中,即使它们在使用时呈现完美render_book("index.Rmd", "pdf_book"),但在我使用render_book("index.Rmd", "gitbook"). 同样在使用preview_chapter而不是render_book.

这是可用于渲染我的 Tikz 图像的代码:

\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}

我用pdf_book的时候很漂亮。如果我使用gitbook它就不会出现。我试图做一些与这里问题中描述的类似的事情,即使用相同的块但用我的代码替换该代码(尽管我做了中心我的),如下所示:

```{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}
```

当我这样做时,它再次呈现精美,pdfbook实际上我更进一步gitbook(出现图形标题并出现“损坏的图像链接”符号,如前所述,我已经尝试跨浏览器)但仍然没有图像。

关于如何让它发挥作用的任何想法?

标签: rlatexknitrbookdowntikz

解决方案


fig.ext='pdf'您正在创建一个 PDF 文件,您的浏览器无法包含该文件。相反,您可以将fig.ext=if(knitr:::is_latex_output()) 'pdf' else 'png'PDF 输出与 LaTeX 和 PNG 输出一起用于所有其他情况。或者,您可以完全删除fig.ext并使用默认值。鉴于其中一项更改,您的示例适用于 HTML/Gitbook 和 PDF/LaTeX 输出。


推荐阅读