Giter VIP home page Giter VIP logo

Comments (10)

boltomli avatar boltomli commented on July 26, 2024

是的吧,网页暂时不改宽度,之前是90%

参看 d0037ad

from msg.

boltomli avatar boltomli commented on July 26, 2024

至于图中文字乱码的问题,我用过这样一个方式解决,估计用source字体也是可以的

https://github.com/boltomli/tidy-text-mining-zh/blob/e20ef34b469c5f293e3f78e67686736b6bdb41d3/01-tidy-text.Rmd#L202-L216

from msg.

XiangyunHuang avatar XiangyunHuang commented on July 26, 2024

如果图片里有中文,就在代码块选项中添加 fig.showtext=TRUE

contour-pop-1

from msg.

XiangyunHuang avatar XiangyunHuang commented on July 26, 2024

我刚才在网页版中默认将图片以矢量格式方式显示,如果图里点很多就手动指定 png 格式

奇怪的是,我全局指定 svg 格式后,https://github.com/XiangyunHuang/MSG-Book/blob/4b66b1b22b01e5ec3f7f515fb12425093c152e7e/_output.yml#L4

在遇到图里有数学公式时,我手动指定了 dev='tikz',它应该调用 tikzDevice 处理,但是我发现在网页版中没有起作用,图里的公式并没有 LaTeX 渲染的结果

https://github.com/XiangyunHuang/MSG-Book/blob/4b66b1b22b01e5ec3f7f515fb12425093c152e7e/elements.Rmd#L544-L546

这个问题我呈给 @yihui 审阅

from msg.

yihui avatar yihui commented on July 26, 2024

#3 中所说,dev = 'tikz' 在 R Markdown 中使用是一个超级复杂的问题,世界上可能只有我一个人明白这里面的来龙去脉。就算你用了 PDF 转 PNG 也不行,因为首先 PDF 的渲染就不对。knitr 生成图片的机制是这样的:在执行一段代码之前,首先打开一个图形设备,一边执行代码一边用这个图形设备录制图形,执行完一个代码段之后再把录制的图形用用户指定的图形设备重绘出来并生成到文件中。这里问题就出在那个录制图形的设备上,它默认是 knitr:::pdf_null,所以你的数学公式其实都是以普通文本形式录下来的,到后面再传给 tikzDevice 的时候它并不知道这些文本有何特殊之处。要解决这个问题,就得把录制的图形设备改成真正的 tikzDevice::tikz() 设备。目前 knitr 没有提供直接修改录制设备的办法,只能通过 options(device = tikzDevice::tikz) 这种办法暴力修改;你最好是在 dev='tikz' 那段代码段之前作这个修改,在那之后再重置这个选项。当然也可以考虑用钩子函数,诸如

knitr::knit_hooks$set(dev = function(before, options) {
  if (options$dev != 'tikz') return()
  options(if (before) tikzDevice::tikz else knitr:::pdf_null)
  NULL
})

我没测试,不知道是否真的能用。就算纯英文字符的图能用,当图片包含中文时,还得调节 tikzDevice 的一些选项,如 options(documentDeclaration) 得是支持中文的文档类,等等。

dev = 'tikz' 最初是为 Rnw 文档设计的。要平滑过渡到 Rmd 的话,这水真是深得不要不要的。

from msg.

XiangyunHuang avatar XiangyunHuang commented on July 26, 2024

尝试了这个钩子,但是即使是英文的环境都无法把数学公式渲染出来,这个问题看来要请原作 @yihui 亲自出手

https://github.com/XiangyunHuang/MSG-Book/blob/ffedc09d32c772dcd533369635932c92cc7dcb6c/_common.R#L38-L43

from msg.

XiangyunHuang avatar XiangyunHuang commented on July 26, 2024

我试了个很简单的带中文的例子,遗憾的是不能够渲染中文,问题可能出在 tikz 上

library(tikzDevice)

options(
  tikzDefaultEngine = "xetex",
  tikzDocumentDeclaration = "\\documentclass[12pt,UTF8]{ctexart}\n",
  tikzXelatexPackages = c(
    "\\usepackage[colorlinks, breaklinks]{hyperref}",
    "\\usepackage{color}",
    "\\usepackage{tikz}",
    "\\usepackage[active,tightpage,xetex]{preview}",
    "\\PreviewEnvironment{pgfpicture}",
    "\\usepackage{amsmath,amsfonts,mathrsfs}"
  )
)

tikz('tikz-example.tex', standAlone = TRUE, engine = "xetex",
width = 3.25, height = 3.25)
plot(1, 1, main = 'Hello \\TeX !', xlab = "中文")
dev.off()

tinytex::xelatex('tikz-example.tex')

tikz-example.tex 文件内容

% Created by tikzDevice version 0.12.3 on 2019-08-22 10:52:42
% !TEX encoding = UTF-8 Unicode
\documentclass[12pt,UTF8]{ctexart}
\usepackage[colorlinks, breaklinks]{hyperref}
\usepackage{color}
\usepackage{tikz}
\usepackage[active,tightpage,xetex]{preview}
\PreviewEnvironment{pgfpicture}
\usepackage{amsmath,amsfonts,mathrsfs}\begin{document}

\begin{tikzpicture}[x=1pt,y=1pt]
\definecolor{fillColor}{RGB}{255,255,255}
\path[use as bounding box,fill=fillColor,fill opacity=0.00] (0,0) rectangle (234.88,234.88);
\begin{scope}
\path[clip] ( 59.04, 73.44) rectangle (204.64,175.84);
\definecolor{drawColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (131.84,124.64) circle (  2.70);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (234.88,234.88);
\definecolor{drawColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 64.43, 73.44) -- (199.25, 73.44);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 64.43, 73.44) -- ( 64.43, 66.24);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 98.14, 73.44) -- ( 98.14, 66.24);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (131.84, 73.44) -- (131.84, 66.24);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (165.54, 73.44) -- (165.54, 66.24);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (199.25, 73.44) -- (199.25, 66.24);

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 64.43, 47.52) {0.6};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 98.14, 47.52) {0.8};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (131.84, 47.52) {1.0};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (165.54, 47.52) {1.2};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (199.25, 47.52) {1.4};

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04, 77.23) -- ( 59.04,172.05);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04, 77.23) -- ( 51.84, 77.23);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04,100.94) -- ( 51.84,100.94);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04,124.64) -- ( 51.84,124.64);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04,148.34) -- ( 51.84,148.34);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04,172.05) -- ( 51.84,172.05);

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 41.76, 77.23) {0.6};

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 41.76,124.64) {1.0};

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 41.76,172.05) {1.4};

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04, 73.44) --
	(204.64, 73.44) --
	(204.64,175.84) --
	( 59.04,175.84) --
	( 59.04, 73.44);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (234.88,234.88);
\definecolor{drawColor}{RGB}{0,0,0}

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.20] at (131.84,200.43) {\bfseries Hello \TeX !};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (131.84, 18.72) {中文};

\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 12.96,124.64) {1};
\end{scope}
\end{tikzpicture}

\end{document}

效果图

tikz-example.pdf

from msg.

yihui avatar yihui commented on July 26, 2024

@XiangyunHuang 好了,你已经走完 99% 的路了。中文出不来八成是字体的问题。我在我的电脑(苹果)上跑了一下你的例子,中文可以出来:

image

我估计你可能是在 Linux 下测的。也许可以显式指定 Fandol 字体。ctex 在 Windows 下会自动选择宋体,苹果系统下也是选宋体,Linux 下如果 Fandol 存在的话会自动选 Fandol。

from msg.

XiangyunHuang avatar XiangyunHuang commented on July 26, 2024

@yihui Issues 列表里还有3个 bug 没解决多半也是字体问题,我搞不定了,我在 Windows 下测的,不知道哪里出了问题

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 
[2] LC_CTYPE=Chinese (Simplified)_China.936   
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C                              
[5] LC_TIME=Chinese (Simplified)_China.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tikzDevice_0.12.3

loaded via a namespace (and not attached):
[1] compiler_3.6.1 tools_3.6.1    tinytex_0.15   filehash_2.4-2 grid_3.6.1    
[6] xfun_0.9      

from msg.

XiangyunHuang avatar XiangyunHuang commented on July 26, 2024

Windows 环境太难搞了,我在 Linux 下运行结果正常

from msg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.