latex.to.textspan = function(str) {
#str = "x_{5ab} y_{1} z_3"
restore.point("latex.to.textspan")
#return(str)
#return(paste0("R\\(",str,"\\)"))
#str = "x_{5ab}\\alpha * \\beta"
#str = "E^{*}"
str = replace.latex.with.unicode(str)
txt = str
txt = svg.change.subscripts(txt,super = FALSE)
txt = svg.change.subscripts(txt,super = TRUE)
if (!identical(txt,str)) {
txt = paste0("<tspan>",txt,"</tspan>")
}
# remove curley braces
txt = gsub("{{","jJj",txt, fixed=TRUE)
txt = gsub("}}","hHh",txt, fixed=TRUE)
txt = gsub("{","",txt, fixed=TRUE)
txt = gsub("}"," ",txt, fixed=TRUE)
txt = gsub(" "," ",txt, fixed=TRUE)
txt = gsub("jJj","{{",txt, fixed=TRUE)
txt = gsub("hHh","}}",txt, fixed=TRUE)
txt
}
svg.change.subscripts = function(str, add.tspan = FALSE, super=FALSE) {
restore.point("svg.change.subscripts")
if (!super) {
char = "_"
class = "label_subscript"
sign = 1
} else {
char = "\\^"
class = "label_superscript"
sign = -0.75
}
li = find.subscripts(str,char=char)$s
if (length(li)==1) {
txt = li
} else {
#if (length(li) %% 2 ==1) li = c(li,"")
sub = seq(2, length(li),by=2)
dy = sapply(seq_along(sub),function(i) {
restore.point("hdfhkjdh")
nc = nchar(li[sub[i]])
if (substring(li[sub[i]],1,1)=="{") nc = nc-2
paste0(c(5,rep(0,nc-1),-5)*sign , collapse=",")
})
li[sub] = paste0('<tspan dy="',dy,'" class="', class,'">', li[sub],'</tspan>')
txt = paste0(li,collapse="")
if (add.tspan) {
txt = paste0("<tspan>",txt,"</tspan>")
}
}
txt
}
find.subscripts = function(str, char = "_") {
restore.point("find.subscripts")
# find subscripts
pos1 = str.find(str,paste0(char,'[0-9a-zA-Z|.=]+'),fixed=FALSE)
pos2 = str.find(str,paste0(char,'\\{[0-9a-zA-Z_|.=,*+-°]+\\}'),fixed=FALSE)
pos = rbind(pos1,pos2)
if (NROW(pos)==0) {
return(list(s=str,is.sub=FALSE))
}
spl = str.split.at.pos(str,pos,keep.pos = TRUE)
first = pos[1,1]==1
if (first) {
is.sub = rep(c(TRUE,FALSE),length.out=length(spl))
} else {
is.sub = rep(c(FALSE,TRUE),length.out=length(spl))
}
spl[is.sub] = substring(spl[is.sub],2)
list(s=spl, is.sub=is.sub)
}
replace.latex.with.unicode = function(str) {
latex = c( "\\alpha","\\beta","\\gamma","\\delta","\\epsilon","\\zeta","\\eta","\\theta","\\iota","\\kappa","\\lambda","\\mu","\\nu","\\xi","\\pi","\\rho","\\varsigma","\\sigma","\\tau","\\upsilon","\\phi","\\chi","\\psi","\\omega","\\Gamma","\\Delta","\\Theta","\\Lambda","\\Xi","\\Pi","\\Sigma","\\Upsilon","\\Phi","\\Psi","\\Omega","\\neg","\\pm","\\cdot","\\to","\\Rightarrow","\\Leftrightarrow","\\forall","\\partial","\\exists","\\emptyset","\\nabla","\\in","\\notin","\\prod","\\sum","\\surd","\\infty","\\wedge","\\vee","\\cap","\\cup","\\int","\\approx","\\neq","\\equiv","\\leq","\\geq","\\subset","\\supset","\\^circ","\\times","\\lfloor","\\rfloor","\\lceil","\\rceil" )
uc = c( "\U3B1","\U3B2","\U3B3","\U3B4","\U3B5","\U3B6","\U3B7","\U3B8","\U3B9","\U3BA","\U3BB","\U3BC","\U3BD","\U3BE","\U3C0","\U3C1","\U3C2","\U3C3","\U3C4","\U3C5","\U3C6","\U3C7","\U3C8","\U3C9","\U393","\U394","\U398","\U39B","\U39E","\U3A0","\U3A3","\U3A5","\U3A6","\U3A8","\U3A9","\U00AC","\U00B1","\U00B7","\U2192","\U21D2","\U21D4","\U2200","\U2202","\U2203","\U2205","\U2207","\U2208","\U2209","\U220F","\U2211","\U221A","\U221E","\U2227","\U2228","\U2229","\U222A","\U222B","\U2248","\U2260","\U2261","\U2264","\U2265","\U2282","\U2283","\U00B0","\U00D7","\U230A","\U230B","\U2308","\U2309" )
pos = str.find(str,'\\\\[0-9a-zA-Z]+',fixed=FALSE)
spl = str.split.at.pos(str,pos,keep.pos = TRUE)
ind = match(spl, latex)
rows = !is.na(ind)
spl[rows] = uc[ind[rows]]
res = paste0(spl,collapse="")
Encoding(res) = "UTF-8"
res
}
make.greece.code = function() {
str='
α,alpha,α,x3B1
β,beta,β,x3B2
γ,gamma,γ,x3B3
δ,delta,δ,x3B4
ε,epsilon,ε,x3B5
ζ,zeta,ζ,x3B6
η,eta,η,x3B7
θ,theta,θ,x3B8
ι,iota,ι,x3B9
κ,kappa,κ,x3BA
λ,lambda,λ,x3BB
μ,mu,μ,x3BC
ν,nu,ν,x3BD
ξ,xi,ξ,x3BE
π,pi,π,x3C0
ρ,rho,ρ,x3C1
ς,varsigma,ς,x3C2
σ,sigma,σ,x3C3
τ,tau,τ,x3C4
υ,upsilon,υ,x3C5
φ,phi,φ,x3C6
χ,chi,χ,x3C7
ψ,psi,ψ,x3C8
ω,omega,ω,x3C9
Γ,Gamma,Γ,x393
Δ,Delta,Δ,x394
Θ,Theta,Θ,x398
Λ,Lambda,Λ,x39B
Ξ,Xi,Ξ,x39E
Π,Pi,Π,x3A0
Σ,Sigma,Σ,x3A3
Υ,Upsilon,Υ,x3A5
Φ,Phi,Φ,x3A6
Ψ,Psi,Ψ,x3A8
Ω,Omega,Ω,x3A9
¬,neg,¬,x00AC
±,pm,±,x00B1
·,cdot,·,x00B7
→,to,→,x2192
⇒,Rightarrow,⇒,x21D2
⇔,Leftrightarrow,⇔,x21D4
∀,forall,∀,x2200
∂,partial,∂,x2202
∃,exists,∃,x2203
∅,emptyset,∅,x2205
∇,nabla,∇,x2207
∈,in,∈,x2208
∉,notin,∉,x2209
∏,prod,∏,x220F
∑,sum,∑,x2211
√,surd,√,x221A
∞,infty,∞,x221E
∧,wedge,∧,x2227
∨,vee,∨,x2228
∩,cap,∩,x2229
∪,cup,∪,x222A
∫,int,∫,x222B
≈,approx,≈,x2248
≠,neq,≠,x2260
≡,equiv,≡,x2261
≤,leq,≤,x2264
≥,geq,≥,x2265
⊂,subset,⊂,x2282
⊃,supset,⊃,x2283
°,^circ,°,x00B0
×,times,×,x00D7
⌊,lfloor,⌊,x230A
⌋,rfloor,⌋,x230B
⌈,lceil,⌈,x2308
⌉,rceil,⌉,x2309'
d = read.csv(textConnection(str),header = FALSE,stringsAsFactors = FALSE)
cat("latex = c(",paste0('"\\\\',d[,2],'"',collapse=","),")")
uc = d[,4]
cat("uc = c(",paste0('"\\U',substring(uc,2),'"',collapse=","),")")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.