在书本图形环境中包含TikZ代码 [英] Include TikZ code in bookdown figure environment

查看:83
本文介绍了在书本图形环境中包含TikZ代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在bookdown文档中添加TikZ图形,以便包含一些精美的图形.

I'd like to add a TikZ figure to a bookdown document in order to include some fancy graphics.

我的主要输出格式是LaTeX,这意味着我基本上可以在Rmarkdown文件中逐字包含TikZ图形,并且可以很好地渲染.但是,有两个问题困扰着我:

My primary output format is LaTeX which means that I could essentially just include the TikZ graphics verbatim in the Rmarkdown file and it would render fine. However, two problems are haunting me:

  • 我希望TikZ图形成为图形环境的一部分(用于编号,标题等).
  • 我希望能够将相同的代码呈现给PDF(LaTeX)和Gitbook(HTML).

现在,我有以下块,当我将其渲染为pdf时,可以很好地生成相关的图形作为图形.

Right now I have the following chunk which nicely produces the relevant graph as a figure when I render to pdf.

```{r, echo=FALSE, engine='tikz', out.width='90%', fig.ext='pdf', fig.cap='Some caption.'}
\begin{tikzpicture}[scale=.7]
\draw [fill=gray!30,very thick] (0,-1) rectangle (5,1);
\draw [very thick] (5, 0) -- (13,0);
\node [below] at (2,-1) {\large Hello};
\node [below, align=center] at (0,-1) {\large Two\\ lines};
\end{tikzpicture}
```

但是,代码有两个问题:

However, there are two problems with the code:

  1. 渲染到gitbook时(使用knitrbookdown)我没有得到任何输出.但是,我确实得到了图形标题,如果我渲染到html_document,它也可以工作,并且我可以看到图形.
  2. 对于PDF,使用计算机现代字体呈现文本.我真的很想更改此设置,并且LaTeX文档中的主要字体已经设置为其他字体.但是,由于代码是由TikZ引擎本地渲染然后插入的,因此它不是完整LaTeX文档的一部分.我可以在呈现代码之前 添加TikZ引擎包含的一些LaTeX选项,软件包等吗?
  1. I do not get any output when rendering to gitbook (using knitr and bookdown). I do get the figure caption, however, and if I render to html_document then it works too and I can see the graph.
  2. For PDF the text is rendered using the computer modern font. I'd really like to change this, and the main font in the LaTeX document has already been set to something else. However, because the code is rendered locally by the TikZ engine and then inserted, it is not part of the full LaTeX document. Can I add some LaTeX options, packages etc. that are included by the TikZ engine before the code is rendered?

如果还有其他方法可以将TikZ代码作为图形环境的一部分包含在内,那么我很高兴知道.

If there are other ways to include the TikZ code as part of a figure environment then I'd be happy to know.

更新:我猜第二点可以通过设置engine.opts = list(template = "latex/tikz2pdf.tex")来解决,其中tikz2pdf.tex文件中包含LaTeX的必要设置.该文件是使用LaTeX读取的,但由于要使用fontspec LaTex软件包,因此我想使用xelatex来解析该文件.仍然可以更改吗?

Update: I guess the second point could be fixed by setting engine.opts = list(template = "latex/tikz2pdf.tex") where the necessary setup for LaTeX is included in the tikz2pdf.tex file. That file is read using LaTeX but I'd like to use xelatex to parse the file since I'm using the fontspec LaTex package. Can that be changed anyway?

推荐答案

我想我找到了两个问题的答案.正如Yihui指出的,确实花了相当长的时间.我在这里包括答案,以防万一有人(或我自己)需要这个.

I think I found an answer to both of my questions. It did take - as Yihui pointed out - quite some time. I'm including the answer here in case someone else turns out to need this (or myself at a later point).

Re 1)将TikZ代码同时渲染为pdf和gitbook

事实证明,这比我预期的要容易.将参数fig.ext=if (knitr:::is_latex_output()) 'pdf' else 'png'设置为块参数的一部分可以帮助实现这一点.如果我不是编织为PDF,则imagemagick或其他软件会自动将其转换为PNG.

This turned out to be easier than I anticipated. Setting the argument fig.ext=if (knitr:::is_latex_output()) 'pdf' else 'png' as part of the chunk arguments helps this along. If I'm not knitting to PDF then imagemagick or some other software automatically converts it to PNG.

Re 2)修改字体

正如我更新的问题中列出的那样,可以通过调整knitr的文件tikz2pdf.tex进行设置.它的副本包含在下面,因此您不必自己进行搜索.设置块参数engine.opts = list(template = "latex/tikz2pdf.tex")可使您在呈现TikZ代码之前将任何所需的字体,LaTeX包等放入序言中.

As listed in my updated question this can be set by tweaking the file tikz2pdf.tex that is part of knitr. A copy of it is included below so you don't have to search for it yourself. Setting the chunk argument engine.opts = list(template = "latex/tikz2pdf.tex") enables you to put any desired fonts, LaTeX packages etc in preamble before the TikZ code is rendered.

浏览knitr代码,您会看到texi2dvi用于解析插入了TikZ代码的tikz2pdf.tex文件. texi2dvi调用pdflatex会弄乱事情,以防万一您需要使用XeLaTeX或LuaLaTeX来使用fontspec包含TrueType字体.

Looking through the knitr code, you can see that texi2dvi is used to parse the tikz2pdf.tex file with the TikZ code inserted. texi2dvi calls pdflatex which messes things up in case you need to use XeLaTeX or LuaLaTeX to include TrueType fonts using fontspec.

我敢肯定,可以在texi2dvi代码中以某种方式解决此问题,但更简单的解决方案(至少对我而言)是更改环境.如果我将两个环境变量设置为 before ,然后开始R并渲染该书,则xelatex将自动用于编译所有代码.在我的bash终端中,这是使用

I'm sure it would be possible to fix that somehow in the texi2dvi code but a much simpler solution (at least for me) was to change the environment. If I set the two environmental variable before starting R and rendering the book then xelatex is automatically used for compiling all the code. In my bash terminal this is done using

export LATEX="xelatex"
export PDFLATEX="xelatex"

Voila!

该块变为

```{r, echo=FALSE, engine='tikz', out.width='90%', fig.ext=if (knitr:::is_latex_output()) 'pdf' else 'png', fig.cap='Some caption.', engine.opts = list(template = "latex/tikz2pdf.tex")
}
\begin{tikzpicture}[scale=.7]
\draw [fill=gray!30,very thick] (0,-1) rectangle (5,1);
\draw [very thick] (5, 0) -- (13,0);
\node [below] at (2,-1) {\large Hello};
\node [below, align=center] at (0,-1) {\large Two\\ lines};
\end{tikzpicture}
```

tikz2pdf.tex

\documentclass{article}
\include{preview}
\usepackage[pdftex,active,tightpage]{preview}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
%% INSERT YOUR OWN CODE HERE 
\begin{document}
\begin{preview}
%% TIKZ_CODE %%
\end{preview}
\end{document}

我仍然对knitr和相关软件包的整体灵活性感到惊讶.干得好逸熙!

I'm still surprised at the whole flexibility of knitr and related packages. Nice work Yihui!

这篇关于在书本图形环境中包含TikZ代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆