I am new in Julia and using Symbolics. I wanna make Latex output. In Symbolics' docs there is an easy example:
using Latexify
latexify(A) # A - some symbolic expression
And in their example they have a beautiful Latex output. But in my case I have Latex code as output.
julia> expr = 2x + y^2
2x + y^2
julia> latexify(expr)
L"\begin{equation*}
2 x + y^{2}
\end{equation*}
"
How can I fix it?
latexify produces an LaTeXString objects that does not provide any visualization of its own. However there are many renderers that can display contents havingtext/latex MIME type. Have a look at https://github.com/stevengj/LaTeXStrings.jl
Perhaps your best bet is IJulia (Jupyter notebook)
Note that Symbolics object get correctly displayed regardless whether you use latexify.
Another possible renderers could be plotting packages that support LaTeX annotations on plots via functions such as annotate!.
Try render(latexify(expr)). It should work in Atom:
https://korsbo.github.io/Latexify.jl/stable/#External-rendering
Related
Plotting in Julia has been a pain for me thus far. I do
using Plots.jl
Initially the plotting was fine, even with latex expressions. However, something must have changed (I don't know what), and now when I wish to plot something with latex expression in the axis or title, for example:
plot(
p1,p2,
layout=layout,
link=:y,#aligns y-axis gridlines
size = (800, 400),
xtickfontsize=15,ytickfontsize=15,
xlab="s " *L" [h^{-1}Mpc]",
ylab=L"|x_s^2\xi_l^{bisector ,(2)}(s)/\xi_l^{pp}(s)|")
I get the error message:
latex: major issue: So far, no MiKTeX administrator has checked for updates.
latex: failed to create a dvi file
Furthermore, my plots are not longer being shown in Jupyter notebook (although I save them, and when I look at what was saved, the plots are there, although with messed up Latex).
Is there any easy fix for this?
I have tried using PyPlots (and PyCall to call it from python) but seem to have an issue importing that. For now I would prefer to stick with pure Julia plotting libraries, and be able to use them with latex.
I have been using WxMaxima for my symbolic calculations for a while now. The good thing about WxMaxima is that you can get formatted outputs right in the program and then export them to LaTeX format with a click of the mouse.
Now I want to try the Jupyter/Ipython plus sympy for multiple reasons. I know how to use display(Math(r' some LaTeX math here ')) but what I want is to have the result/output of a cell in a nice mathematical form; something like the TraditionalForm[] command in Mathematica.
I would appreciate if you could help me know if/how I can get that right in a Jupyter notebook?
I think I found the proper solution and it is a sympy feature rather than Jupyter/IPython one. As explained here:
If all you want is the best pretty printing, use the init_printing() function. This will automatically enable the best printer available in your environment.
and
In the [Jupyter/]IPython notebook, it will use MathJax to render LATEX.
Then one can right click on the output and select Show Math As > Tex commands:
to get the LaTeX output.
P.S. A more proper formatting can be achieved via galgebra library. I will look into that and add it here later.
Hello stackoverflow community,
I have trouble with the interaction between text in R plots and the Latex Environment. In R, I programmed a simple distributional plot and added the percentage below a given threshold via the command mtext(expression(paste("x% < 0 < 1-x%")), ...) . Then the graph has been saved as eps.-figure in order to Transfer it to Latex. The latter, however, displays the < and > symbols as inverted exclamation marks (usually, the < are obtained by the command \textless outside the mathmode in Latex).
I further added some font packages in Latex and it becomes possible to type the < signs into the text without drawing on the math mode. In the Graphs from R, however, the inverted exclamation marks remain.
Does anybody knows how to solve this issue? Although I think that it is a general problem between the R text and Latex, please let me know if some code helps to understand the Problem.
Thank you very much for your help and suggestions.
I don't think it is a general issue in the communication between R and LaTeX, although I agree it can be tricky. I wasn't able to reproduce the behavior you described using what I normally do to create graphics for TeX-documents.
Maybe this setup can also solve the problem you describe. Below is an example to print the said expression using Cairo and PDF graphics.
R-script:
library(Cairo)
Cairo(file="pic", type="pdf", dpi=100)
x <- y <- 1:5
plot(x,y)
mtext(expression(paste("x% < 0 < 1-x%")))
dev.off()
TeX-file:
\documentclass[12pt,paper=a4]{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}[tbp]
\includegraphics{pic.pdf}
\end{figure}
\end{document}
The resulting PDF that is compiled using pdflatex appears to show the expression correctly. A quick screenshot:
I would like to use knitr to produce pdf documents from which example code can be cleanly cut and pasted, but don't seem to be able to.
An example of the problems I run into:
The knitr manual pdf includes this code block (p.3):
## option tidy=TRUE
for (k in 1:10) {
j <- cos(sin(k) * kˆ2) + 3
print(j - 5)
}
When copied from the pdf and then pasted into R (or SO, or etc.), it yields:
## option tidy=TRUE
for (k in 1:10) f j <- cos(sin(k) * kˆ2) + 3
print(j - 5)
g
See how the first two code lines get combined onto one, and, worse, { and } get converted to f and g?
My questions:
First, I guess, is this something other folks experience? Does it happen just on Windows, or elsewhere as well?
If it's not just me, is there some easy workaround? Would using a different font when compiling the *.tex file produce a *.pdf document that is easier to copy-and-paste from?
(FWIW, if I instead use minted to highlight my R code, I don't have any of the same problems, so I know it's possible to get this right.)
Based on clues in this question and its accepted answer, I found that using the LaTeX fontenc package to set the font encoding to T1 fixes the problems reported above. (See also here for some explanation of why using T1 is the more or less officially recommended best practice for LaTeX documents of all sorts. Improved copy-and-pastability is one of several good reasons noted at that link.)
Here's what the start of the preamble in the fixed document looks like:
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
Is it possible to source a file without printing all the charts etc (already tried with echo=F)?
In my case I call the png("filename%03d.png") device early in the script. It is not to big a hassle to comment this out - but all the charts do take a lot of time to render. (the specific file I am working with now uses base-graphics - but mostly I'll be using ggplot2 - which makes the issue somewhat more important (ggplot2 is excellent, but in the current implementation not the fastest))
Thanks
It's not a problem for ggplot2 or lattice graphics - you always have to explicitly print them when they are called in non-interactive settings (like from within a script).
Good practise for coding R means wrapping as much of your code as possible into functions. (See, e.g., Chapter 5 of the R Inferno, pdf.) If you place your plotting code inside a function, it need not be displayed when you source it. Compare the following.
File foo.r contains
plot(1:10)
When you call source('foo.r'), the plot is shown.
File bar.r contains
bar <- function() plot(1:20)
When you call source('bar.r'), the plot is not shown. You can display it at your convenience by typing bar() at the command prompt.
Perhaps this might be of some help...
"A package that provides a null graphics device; includes a vignette, "devNull", that documents how to create a new graphics device as an add on package. "
from http://developer.r-project.org/
It's not the best sounding solution, but If you might be running this script often like this, you could declare a boolean whether graphics are required (graphics_required=TRUE) and then enclose all your plot commands in if/then clauses based on your boolean, then just change the boolean to change the behaviour of the script