Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 standalone 文类简化 tikz 模版 #1985

Merged
merged 2 commits into from
Apr 13, 2021
Merged

使用 standalone 文类简化 tikz 模版 #1985

merged 2 commits into from
Apr 13, 2021

Conversation

XiangyunHuang
Copy link
Contributor

@XiangyunHuang XiangyunHuang commented Apr 11, 2021

standalone 文类替换可以支持 PDFLaTeX 和 XeLaTeX 两种编译方式,当前仅支持 PDFLaTeX, 这对图形中有中文的情况不利,此外,standalone 默认无需指定 preview 且不推荐 preview,preview 设置起来比较繁琐。

[standalone](https://ctan.org/pkg/standalone) 文类替换可以支持 PDFLaTeX 和 XeLaTeX 两种编译方式,当前仅支持 PDFLaTeX, 这对图形中有中文的情况不利,此外,standalone 默认无需指定 preview 且不推荐 preview
@CLAassistant
Copy link

CLAassistant commented Apr 11, 2021

CLA assistant check
All committers have signed the CLA.

@XiangyunHuang XiangyunHuang changed the title 使用 standalone 文类简化模版 使用 standalone 文类简化 tikz 模版 Apr 11, 2021
Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,谢谢!

@yihui yihui merged commit 1f3bd75 into yihui:master Apr 13, 2021
@XiangyunHuang XiangyunHuang deleted the patch-1 branch April 13, 2021 06:52
\documentclass{article}
\include{preview}
\usepackage[pdftex,active,tightpage]{preview}
\documentclass[tikz]{standalone}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\documentclass[tikz]{standalone} 和 xcolor 包冲突,调用 xcolor 包预定义的色彩是通过启用文类选项的方式,比如 \documentclass[tikz, svgnames]{standalone},因为 xcolor 包会预先加载,而这一点竟然没有在 standalone 的帮助文档中提及。一种解决方式是默认模版启用 dvipsnames/svgnames/x11names 选项,还有一种解决方式是类似 Pandoc的模版那样,以变量方式传递选项

https://github.com/jgm/pandoc/blob/56a0d874c76fccfd1cbd1ede2c464446e889d302/data/templates/default.latex#L16-L38

后者更好但是更复杂些,且超出我的能力范围了 😭 。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我没太明白,你是说现在这个模板编译会直接出错吗?还是说需要 knitr 提供一个类似 https://github.com/yihui/knitr/pull/1886/files 里的 engine.opts$extra.preamble 选项来自定义 standalone 文档类选项?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我没太明白,你是说现在这个模板编译会直接出错吗?

不会直接报错,只有在 engine.opts$extra.preamble 里加载 xcolor 包的时候会报错。

还是说需要 knitr 提供一个类似 https://github.com/yihui/knitr/pull/1886/files 里的 engine.opts$extra.preamble 选项来自定义 standalone 文档类选项?

是的,这样就太好了

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那就加个 engine.opts$classoption 选项好了,让用户可以自定义。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,如果用户啥也不加,就默认是 tikz

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

补充一个例子,取自 https://zhuanlan.zhihu.com/p/96762346

---
title: "Using TikZ"
output: 
  pdf_document: 
    latex_engine: xelatex
    extra_dependencies:
      ctex:
       - fontset=fandol
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

options(
  citation.bibtex.max = 999,
  tinytex.engine = 'xelatex',
  tikzDefaultEngine = "xetex"
)
```

## TikZ in R Markdown

```{r dk-curve, engine="tikz", out.width="75%", fig.align='center', echo=FALSE, fig.cap="TikZ 绘制邓宁-克鲁格曲线", engine.opts=list(classoption="dvipsnames,tikz", extra.preamble="\\usepackage[fontset=fandol]{ctex}")}
\begin{tikzpicture}[scale=2, transform shape]
\draw[<->, thick] (8,0) node[below]{大师} -- (0,0) node[left]{低} -- (0,7) node[left]{高};
\draw (4,0) node[below]{\textbf{智慧}} -- (0,0) node[below right]{巨婴} -- (0,4.5) node[above left, rotate=90]{\textbf{自信程度}};
\draw (1, 5.5) node[above]{愚昧山峰};
\draw (2, 0.2) node[above]{绝望山谷};
\draw (4.5, 3) node[above, rotate=60]{开悟之旅};
\draw (7, 6) node[above]{持续平稳高原};
\draw (4, -0.5) node[below]{\textbf{(知识\texttt{+}经验)}};
\draw[very thick, ForestGreen] (0,0) to [out=80,in=100] (1,5) 
    to [out=-80,in=100] (1.5, 1) to [out=-80,in=230] (6,5)
    to [out=50,in=190] (8, 6);
\end{tikzpicture}
```

Untitled

yihui added a commit that referenced this pull request May 3, 2021
clrpackages pushed a commit to clearlinux-pkgs/R-knitr that referenced this pull request Sep 14, 2021
Andrew Heiss (1):
      fix #2038: add ability to pass arguments to dvisvgm through engine.opts (#2039)

Christophe Dervieux (3):
      fix #1993: ignore some values of `fig.keep` (e.g., fig.keep = 'last') when there is only one figure in a chunk (#1996)
      Throw an error if inline result cannot be coerced to character (#2007)
      `include_*`

David C Hall (1):
      Use `conditionMessage()` to render conditions (#2016)

Jamie Lentin (1):
      utils-vignettes: Don't disable vtangle if .Rout.save exists (#2018)

Kenneth Blake Vernon (1):
      fix #1935: make collapse and strip.white = TRUE independent (#2011)

Will Landau (1):
      Add a new targets engine (#2031)

Yihui Xie (19):
      start the next version
      stop() early on CI servers if Pandoc is not found before building R Markdown vignettes: yihui/knitr#1864 (comment)
      use the chunk option engine.opts$classoption to customize the class options of `standalone` for the tikz engine: yihui/knitr#1985 (comment)
      close #1992 (originally reported at #492): allow customizing the tilde symbol ~ with the package option latex.tilde
      add the attribute data=external="1" to <iframe> generated by include_url() so that Pandoc won't base64 encode the URL: https://stackoverflow.com/q/67477667/559676
      close #1997: remove the duplicate \format{} on the help page ?opts_chunk
      use a dedicated env var to decide whether to check DESCRIPTION: yihui/knitr#1864 (comment)
      fix rstudio/rmarkdown#2165: emit a message instead of a warning when building rmarkdown vignettes without pandoc
      remove the code that was for backward compatibility with a very old version of knitr
      remove is_abs_path from knitr, which is no longer used in bookdown >= v0.22 (rstudio/bookdown@0098721)
      signal an error when buiding rmarkdown vignettes but Pandoc is not available, unless the vignette is build during R CMD check (in which case a message is emitted instead)
      specify the package name in \link[]
      `write_bib()` only uses the first URL if multiple are found in a package (#2028)
      close #1864: move markdown to Suggests (#2020)
      no longer test the declaration of dependency on markdown in DESCRIPTION or throw an error (#1864)
      update URLs
      do not throw an error in R CMD check when checking a package on CRAN but rmarkdown/markdown/Pandoc is not available (#1864)
      respect the env var R_CRANDALF: yihui/crandalf@d5949e5
      CRAN release v1.34

christophe dervieux (1):
      re-document. Trailing whitespace are removed in code now.

knokknok (1):
      cache.globals = FALSE means detecting all variables in a code chunk (#1898)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants