How to print LaTeX in Julia? - julia

In Python, I have LaTeX code that I print as:
print('Lane-Emden Equation')
display(Latex(r'$'\
r'\dfrac{\text{d}}{\text{d}\xi}'\
r'\left( \xi^2 \dfrac{\text{d}\theta}{\text{d}\xi} \right)'\
r'='\
r'-\xi^2\theta^n'\
r'$'))
How would I print the same thing in Julia?

You could try this code (note \text does not work and you need to use \mathrm instead):
using LaTeXStrings
lat = L"""\dfrac{\mathrm {d}}{\mathrm {d}\xi} \left( \xi^2 \dfrac{\mathrm {d}\theta}{\mathrm {d}\xi} \right) = -\xi^2\theta^n"""
using PyPlot
PyPlot.cla()
PyPlot.text(0.5,0.5,lat)

Related

Is it possible to plot the tabuler representation of a dataframe from DataFrames.jl using Makie.jl?

I would like to plot the tabular representation of a dataframe from the DataFrames.jl package using Makie.jl or using any of the other plotting libraries in Julia. Another option would be to save the dataframe as an image.
You can just use annotate!, you just need to remember that inbuilt monospace font in Plots.jl does not nicely support all Unicode characters so you need to use a simpler table formatting (markdown formatting will actually look good and nice). On the other hand Makie seems to support system fonts so you can get nicer formatting.
using DataFrames, PrettyTables, Plots
pyplot()
df=DataFrame(a=1:5,b='a':'e',c=rand(5))
io = IOBuffer()
pretty_table(io, df, tf = tf_markdown, show_row_number = true)
str = String(io.data)
plot() # do whatever plotting you need
annotate!(0.5, 0.5, text(str; family="monospace", pointsize=8))
And here is the same example with Makie:
using CairoMakie, Makie
f = Figure()
ax = Axis(f[1, 1])
io = IOBuffer()
pretty_table(io, df)
str = String(io.data)
text(5,5, text=str, font="Consolas", textsize=14)

Creating animated plots in the command line with Julia

Julia has the delightful ability to generate plots constructed from Unicode symbols which are printed directly to the command line in a very straightforward way. For example, the following code generates a Unicode plot of a sine function directly to the command line:
using Plots
unicodeplots();
x = [0:0.1:2*pi;];
y = sin.(x);
plot(x,y)
I would like to try to find a way to create an animated plot of this form directly on the command line. Ideally, I would like to generate a single plot in Unicode that is ``updated" in such a way that it appears animated.
However, although printing hundreds of distinct frames to the command line is naturally less appealing, such a solution is acceptable if it ``looks" like an animation. Another less acceptable solution is to print such Unicode plots into a gif in a way that is consistent for all platforms; attempts to do any of this involving jury-rigging #animate and #gif have largely failed, since either function cannot even print Unicode plots to a file in the Windows form of Julia.
UPDATE: Here is an example of code that generates an "animation" in the command line that is not really acceptable, which simply plots each distinct frame followed by "spacing" in the command line provided by a special Unicode character (tip provided by niczky12):
using Plots
unicodeplots();
n = 100;
x = [0:0.1:4*pi;];
for i = 1:30
y = sin.(x .+ (i/2));
plot(x, y, show=true, xlims = (0,4*pi), ylims = (-1,1))
sleep(0.01)
println("\33[2J")
end
A slight improvement might be this:
let
print("\33[2J")
for i in 1:30
println("\33[H")
y = sin.(x .+ (i/2));
plot(x, y, show=true, xlims = (0,4*pi), ylims = (-1,1))
sleep(0.01)
end
end

Breaking a long equation up into two lines in R Markdown

I am using R Markdown using the texevier package
I have a long equation which I would like to seperate into two lines by seperating with a linebreak operator "\\". Here is the code chunk that I'm trying to run
\begin{equation}
l = -0.5 \sum_{t=1}^{T}[n log(2\pi) + log(\mid H_t \mid) + \varepsilon_t^{'} H_t \varepsilon_t] \\ = -0.5 \sum_{t=1}^{T}[n log(2\pi) + log(\mid D_t R_t D_t \mid) + \upsilon_t^{'}D_t^{-1}R_t^{-1}D_t^{-1}\upsilon_t]
\label{eq5}
\end{equation}
However, it seems that this does not make the contents after the seperator start on a seperate line. Does anyone have advice on how I should go about doing this?

Refer variable to LATEX function

Hi I am writing an R Markdown document in which there is a Latex function. I would like to refer the variable in the LATEX to the variable in the R code so that it automatically update the LATEX if the R code generates a new results for the variable.
Rcode:
var = rnorm(1,0)
Latex part:
$$ f(x) = beta_{0}*var $$
I have already looked these links but they doesn't work for me.
https://github.com/yihui/knitr/blob/master/inst/examples/knitr-minimal.Rmd;
Is there an R Markdown equivalent to \Sexpr{} in Sweave?; https://tex.stackexchange.com/questions/362119/passing-variable-from-r-to-latex
You can do
```{r, echo=FALSE, results='asis'}
var <- rnorm(1)
cat(sprintf("$$f(x) = \\beta_0*%s$$", var))
```
I believe this question has been answered in the post Is there an R Markdown equivalent to \Sexpr{} in Sweave? You can use inline R expressions `r `, e.g.
$$f(x) = \beta_{0} + `r var` * \beta_{1} + `r var2` * \beta_{2} + e$$
where var and var2, etc. are your variables in R.

adding equation to the R plot

I was wondering how I can paste the following equation as legend or text box to my plot in R (shown below in Latex format):
$$
x_i\stackrel{{\rm iid}}{\sim}{\rm Normal}\big(\mu-\frac \alpha 2,\sigma^2\big),\quad i = 1,\ldots, N_x \\
y_i\stackrel{{\rm iid}}{\sim}{\rm Normal}\big(\mu+\frac \alpha 2,\sigma^2\big),\quad i = 1,\ldots, N_y
$$
You can try the following
library(latex2exp)
plot(TeX("$x_i$ \t $\\overset{iid}{\\sim}$ \t $Normal$
$\\left(\\mu -\\frac{\\alpha}{2} ,\\sigma^2\\right)$,\t$i = 1,\\ldots, N_x$"))
to get the following

Resources