【问题标题】:LaTeX command in a R plotR图中的LaTeX命令
【发布时间】:2016-10-28 10:24:32
【问题描述】:

我在 Rstudio 的图表中写下一些 LaTeX 表达式时遇到问题,我已经使用

axis(..., label=TeX("x_{min}")

当我尝试将命令\mathcal{M} 放入该标签时,问题就出现了。有什么方法可以使用axismtext 在 R 图中使用该命令生成 LaTeX 生成的 M?我宁愿使用mtext,但使用它,它甚至不会像我之前写的那样产生我使用轴产生的x_{min}

提前感谢您的帮助

编辑:根据要求,我在这里放了一个我的代码示例

library(latex2exp)
b=7/5
fx <- function (x,b) x^(4*(b-1)/(b+1)) *(1/(b-1)+1/x)
fmin <- optimize(fx, interval=c(0,2), b=7/5)

x <- seq(10^(-4),20, by = 0.01)

y <- fx(x,b)


 plot(x, y, log="xy", ann="FALSE", pch=20, type="l",lwd=2, xlim=c(10^(-3),10), ylim=c(1.5,40), 
 xaxt="n", yaxt="n")

mtext("x", side=1, line=2, cex=1, col="black")
mtext("f(x)", side=2, line=2, cex=1, col="black")

#assex
  axis(1,at=fmin[1], label=TeX("x_{min}"))
  axis(1,at=0.001, label=0.001)

和其他轴线一样,我认为这段代码应该运行。它会产生类似于下图的内容,其中蓝色线显然是我没有粘贴在这里的代码的一部分

【问题讨论】:

  • #1 axis 没有 label 参数,#2 TeX 来自哪里?请用一个可重复的最小示例(可复制粘贴可运行)补充 R 问题,该示例说明您的尝试和问题。
  • 我编辑了写我的代码的帖子,TeX 来自包 latex2exp,是的,轴有标签参数,你能帮我吗?
  • 谢谢。你的意思是\mathcal 而不是\mathchal"\\mathcal" %in% latex2exp_supported()FALSE,表示包不支持。
  • 可能能够使用 Unicode 02133 ℳ milde.users.sourceforge.net/LUCR/Math/unimathsymbols.xhtml ;在 StackOverflow 中搜索如何在绘图中包含 Unicode 字符
  • 我使用包 tickzDevice 解决了。现在我发现了这个包裹的力量,我不会回来了!

标签: r plot latex rstudio axis-labels


【解决方案1】:

根据要求,我发布了我找到的解决方案。首先你可以在这里找到完整的文档:https://cran.r-project.org/web/packages/tikzDevice/vignettes/tikzDevice.pdf

使用这个包,您的整个图表将获得 LaTeX 数学格式样式。所以你会得到更好的数字,甚至是现在数学风格的字母(如果你显然使用 $$ 的话)。您还可以通过一些没有它就无法添加的命令来添加一些字母。例如,我需要输入这个 LaTeX 命令:\mathcal{M}

这是我找到的解决方案,使用包 tikzDevice。 这个包允许你从你的 R 图中创建一个我们称之为“plot.tex”的文件。你不会立即看到情节(或者至少我还没有学会)。一旦包创建了这个 plot.tex,你必须将它包含在你的另一个 .tex 文件中。您希望它出现的文件。方法是这样的:

\begin{figure}
   \input{plot.tex}
\end{figure}    

很明显

\usepackage{tikz}

在序言中。

Rstudio中的代码或多或少是这样的,参考我问题中的代码:

#Defintion of f
b=7/5
fx <- function (x,b) x^(4*(b-1)/(b+1)) *(1/(b-1)+1/x)

#find minimum of f (oviously optional)
fmin <- optimize(fx, interval=c(0,2), b=7/5)

#Density of points in the x-asxis
x <- seq(10^(-4),20, by = 0.01)

#Assign value to y
y <- fx(x,b)


#call the tikzDevice package, don't forget the dev.off() at the end or you won't be able to produce a proper file  
library(tikzDevice)

#name the file you want to create and decide its dimensions
tikz('faggy.tex',
width=5.5,height=4)

#do your normal plot as usual: you can add any LaTeX symbol as you can see below


#plot_f(x)_______________
  plot(x, y, log="xy", ann="FALSE", pch=20, type="l",lwd=2, xlim=c(10^(-3),10), 
       ylim=c(1.5,40), xaxt="n", yaxt="n")

#code useful to plot the lines that touch of the minimum of the function
linea1 <- seq(1,fmin[[2]], by = 0.01)
xminimo <- numeric(length(linea1))
xminimo <- rep(fmin[1],length(linea1))

linea2 <- seq(10^(-4),fmin[[1]], by = 0.01)
fminimo <- numeric(length(linea2)) 
fminimo <- rep(fmin[2],length(linea2))

#plot_lines___
  points(xminimo,linea1, pch=20, type="l", lwd=2, lty=2, col="blue")

  points(linea2,fminimo, pch=20, type="l", lwd=2, lty=2, col="blue")


 #writing on the axis: you can use LaTeX here!!! any symbol or command, like \mathcal{}
mtext("$x$", side=1, line=2, cex=1, col="black")
mtext("$f(x)$", side=2, line=2, cex=1, col="black", las=2)

#assex, example of the use \mathcal, I didn't need it there oviously :) 
  axis(1,at=fmin[1], label=paste("$\\mathcal{M}_{min}$"))
  axis(1,at=0.001, label=0.001)
  axis(1,at=0.01, label=0.01)
  axis(1,at=1, label=1)
  axis(1,at=10, label=10)

#assey
  axis(2,at=fmin[2], label=paste("$f_{min}$"),las = 2)
  axis(2,at=2, label=2,las = 2)
  axis(2,at=5, label=5,las = 2)
  axis(2,at=10, label=10,las = 2)
  axis(2,at=20, label=20,las = 2)
  axis(2,at=40, label=40,las = 2)




dev.off()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 2021-11-11
    • 2010-11-26
    相关资源
    最近更新 更多