R 图中的 LaTeX 命令
LaTeX command in a R plot
我在 Rstudio 的图表中写下一些 LaTeX 表达式时遇到问题,我已经使用
完成了
axis(..., label=TeX("x_{min}")
当我尝试将命令 \mathcal{M}
放入该标签时出现问题。有什么方法可以使用 axis
或 mtext
在 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)
等轴线,我觉得这段代码应该运行。它产生如下图所示的内容,其中蓝线显然是我没有粘贴到这里的代码的一部分
根据要求,我 post 我找到了解决方案。首先,您可以在此处找到完整的文档: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()
我在 Rstudio 的图表中写下一些 LaTeX 表达式时遇到问题,我已经使用
完成了axis(..., label=TeX("x_{min}")
当我尝试将命令 \mathcal{M}
放入该标签时出现问题。有什么方法可以使用 axis
或 mtext
在 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)
等轴线,我觉得这段代码应该运行。它产生如下图所示的内容,其中蓝线显然是我没有粘贴到这里的代码的一部分
根据要求,我 post 我找到了解决方案。首先,您可以在此处找到完整的文档: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()