Greek letters in Octave plot not exporting to EPS - plot

I can't make Octave export my EPS figures with the greek symbols in title. I mean, it does displays inside Octave, but when I print it to an EPS file (print -depsc2 filename.eps), it displays alpha instead of the symbol alpha.

I guess you want the EPS for inclusion into a latex document? There are numerous ways to do this. One popular way is using psfrag.
I suggest using
print -depslatex yourfile.eps
which will create yourfile.eps only with the graphic part and yourfile.tex (which includes yourfile.eps). You can then add it to your main latex document with \include.
If you just want the plot without having a main latex document you can use
print -depslatexstandalone yourfile.eps
and let latex process the code. Here is a simple example:
close all
graphics_toolkit fltk
sombrero ();
title ("Plot the familiar 3-D sombrero function:\
$z = \\frac{\\sin\\left(\\sqrt{x^2 + y^2}\\right)}{\\sqrt{x^2 + y^2}}$");
print -depslatexstandalone test_epslatex
system("latexmk test_epslatex.tex -ps");
system("latexmk -c");
With this you have a great flexibility and use all Latex commands.
Another, very easy way is to use gnuplot:
graphics_toolkit gnuplot
plot(1:2);
title('\alpha')
print -depsc out.eps

Related

Adding text string to existing pdf doc

Is anyone aware of a way to add a text string to an existing pdf (or multiple pdfs) with R? For example, one might want to add a title in a specified position on the page or a watermark.
These similar-sounding questions aren't what I am looking for, as I would like to add text to an existing pdf - not one that I am knitting in Rmd, which would be more straightforward:
Watermark in rmarkdown
Add a 'DRAFT' watermark on pdf result of R markdown)
If (free command line) version of pdftk is an acceptable auxiliary tool, then you can automate the solution from this question in R (i.e., using the stamp operation in pdftk):
Generate PDF to experiment with:
pdf("test1.pdf")
plot(1:10,1:10)
dev.off()
Generate "watermark" overlay:
pdf("stamp.pdf")
par(fg="white")
## to match spacing etc - 'phantom' plot
plot(1:10,1:10, type="n", axes=FALSE, ann=FALSE)
par(fg="black")
text(5,5, "watermark", col=adjustcolor("gray", alpha.f=0.2), cex=5)
dev.off()
Call pdftk to do the overlay:
system("pdftk test1.pdf stamp stamp.pdf output test1S.pdf")
Results:
I think the fussy part will be getting the spacing in the overlay file the way you want it ... although you could use grid graphics, or create a zero-margin/zero-annotation plot so that the plotting area had bounds (0,1) x (0,1) ...

How to make newline (\n,\break,\\ ...) in octave plot (figure) when printing (saving)?

Task is simple. I would like to make this legend work:
h = legend ('measured', 'linear fit R(0)$\break$ = 2$\Omega$$\mu$m');
Special characters are working, but not a new line. Simple '\n' is working with qt. However, qt doesn't know greek symbols ... . Gnuplot doesn't recognise lot of linewidth settings and uses ugly font for symbols.
So I ended up with graphics_toolkit fltk. And for save I use:
print -depslatexstandalone test_epslatex
system("latexmk test_epslatex.tex -ps");
system("latexmk -c");
However, I'm not able to tell latex that I want a new line. It would look much better.
Update:
Using graphics_toolkit fltk, and this legend configuration:
h = legend ('measured', cstrcat('linear fit',"\n",'R(0) = ',num2str(p(2),5),'$\Omega$'));
it looks like this in the window:
screen shot
And after saving (XLSfile is string with name of input XLSfile):
% saving to png and ps
pom = XLSfile;
pom = strrep(pom, '.xls', '_Rdist');
print(pom,'-depslatexstandalone');
system(cstrcat('latexmk ',pom,'.tex -ps'));
system("latexmk -c");
pom1 = 'gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m ';
pom2 = '-dGraphicsAlphaBits=4 -r800s600 -sOutputFile=';
pom3 = cstrcat(pom,'.png ',pom ,'.ps');
system(cstrcat(pom1,pom2,pom3));
% remove unwanted:
system(cstrcat('rm ',pom,'.dvi ',pom,'.tex ',pom,'-inc.eps'));
It looks like this: saved png image
I realized few things: "\noTexWorksHere", 'no new line character works here\n'. So combination of them should work ...
The dollar signs have to be there since export is using Latex no Tex.
Also there is an issue with legend :( . And It is wrong in the first export to eps from print command. In export, Octave is drawing legend background directly after graph background. That's why the grid is on the legend. I should fill the bug report but don't know how yet (I installed octave few days ago).
Some technical details: Ubuntu 16.04, Octave 4.0.0, hopefully well configured latex (it's working, but I'm a bit new user)

inline Latex code inside knitr R block

I am looking for a way to put inline latex code into a R code chunk in Knitr.
Here is my example code from the knitr example site :
\documentclass{article}
\begin{document}
Example text outside R code here; we know the value of pi is \Sexpr{pi}.
<<my-label, echo=FALSE, eval=TRUE>>=
set.seed(1213) # for reproducibility
x = cumsum(rnorm(100))
m <- mean(x) # mean of x
print(m)
cat(m)
plot(x, type = 'l') # Brownian motion
#
\textit{Mean is :} \textbf{\Sexpr{m}}
\end{document}
For something simple like this is I could use result='asis' but for a more complicated piece of code, where you want to periodically write the result out to the document, (especially you have complex ggplot graphs), that solution does not work very well.
In the given example, I have 3 queries :
How would I use inline latex code for the output from line 8, in case I wanted to color, bold etc. that text.
Can one eliminate the grey box that appears when we use the cat or print command.
Can the numbering which appears with the print command, which is eliminated with the cat command be eliminated for the print command as well, since print has many variants in many packages for data frames data tables etc. and might be more commonly used to print a portion of data.
In summary, I am mainly looking for the inverse of line 12 in the code.
I have also unsuccessfully tried knit_print with printr, and asis_output, in lieu of print. Although I may have been incorrectly using them.
Thanks!

Referring to other figures from within a chunk

Inside the plot generated in a chunk in a knitr/LaTeX document I want to position a text string with a reference to a particular figure elsehwere in the document, something like "See Fig. 10", based on the label of a that particular figure, lets say "fig:sim1". So I wrote the following R function which reads the .aux file of the LaTeX-document I'm writing and extracts the counter of the figure matching a specific label
ref <- function(label) {
lines <- scan("mismatch-final.aux","character",sep="\n")
line <- grep(paste("\\{",label,"\\}",sep=""),lines,value=TRUE)
strsplit(strsplit(line,"\\{\\{")[[1]][2],"\\}\\{")[[1]][1]
}
I can then generate the desired text string from within the chunk and place it inside the plot with something like
text(5,10,paste("See Fig.~",ref("fig:sim1")))
This should work but perhaps there is a cleaner way of doing this? Also, is there a way to get at the .aux filename from within the chunk such that the ref function above would be made more general?
Another way to do that is to use the tikzDevice package and TikZ plots, so you can write raw LaTeX expressions in your plot, e.g
\begin{figure}
....
\caption{Simulation of something. \label{fig:sim1}}
\end{figure}
<<test, dev='tikz', external=FALSE>>=
plot(rnorm(10), rnorm(10))
text(0, 0, 'See Fig \\ref{fig:sim1}')
#
Depending on the complexity of your plot, this chunk may take a few seconds to compile each time. The plot is saved as a LaTeX file, and any LaTeX commands will work in it.

Generating PDF from multiple JPEGs in R

I have a folder with multiple JPEG files. How do I generate a PDF file from these JPEGs in R?
One JPEG = 1 PDF page. Images are of the same size.
Thanks a lot in advance.
You can do this easily using Latex. Which is nice, because then you can just use Sweave to do the whole thing.
You can do something along the lines of :
% This is some Sweave file
\documentclass{article}
\usepackage{graphicx}
\begin{document}
<<results=tex,echo=FALSE>>=
mypics <- dir('mypics')
for(i in mypics){
cat("\\includegraphics{", i, "}\n\n", sep = "")
}
#
\end{document}
OK, you'll have to set up your Sweave pipeline, but with a bit of tweaking you can automate the whole process easily.
if you insist on using R (other tools are more suitable), try something like this (slow, untested):
lf = list.files(pattern = "jpeg") # image filenames
library(jpeg)
jpegs = lapply(lf, readJPG)
library(grid)
pdf("output.pdf", width=8, height=4)
grid.raster(jpegs[[1]])
lapply(jpegs[-1], function(x) {grid.newpage() ; grid.raster(x)} ) -> bquiet
dev.off()
If you insist on using R to do this then you can open a pdf plotting device, par to set the margins (default will probably be to big and not centering), then in a loop use plot.new to start a new page and plot.window to set up the coordinates, etc. without plotting axes etc., use the read.jpeg function from the ReadImages package (or other tool/package to read, EBImage is another possibility) then rasterImage to plot the jpeg to the pdf device (or replace some of those steps with other image plotting functions, such as the plot method in ReadImages).
But overall it is probably easier/quicker/better/... to use a tool better designed for this type of thing. The ImageMagick suite of programs comes to mind, LaTeX has also been mentioned, and there are probably other tools as well.

Resources