How to Control Margin of R Plot in Sweave Using Eclipse with Yap (.dvi)? - eclipse

I'm having an issue with TeXlipse where I'm generating a plot in Eclipse via Sweave, and the TeX file seems to be jamming all of my PDF plots in the left most corner of the page. This is driving me crazy. I'm not sure what SweaveOpts might fix this, but \oddsidemargin, \evensidemargin, \begin{centering}, and \SweaveOpts{width=x, height=y} does not help. My code is below:
Sweave Code:
\documentclass{article}
\usepackage{Sweave}
\usepackage{graphicx}
\SweaveOpts{prefix.string=C:/SweavePlots/bar}
\DeclareGraphicsExtensions{.pdf}
...
\begin{figure}
\begin{center}
<<figname, fig=TRUE, include=TRUE, echo=FALSE>>=
plot(1:10)
#
\caption{I hope this Works}
\end{center}
\end{figure}
TeX Code:
\begin{figure}
\begin{center}
\includegraphics{C:/SweavePlots/bar-figname}
\caption{I hope this Works}
\end{center}
\end{figure}
Resultant Figure:

I found out that since the more recent versions of R only produce PDFs and I'm trying to use DVI, I need to explicitly set \SweaveOpts{eps=true}. The bottom line is that PDF images just don't get along well with DVI viewers like Yap. My successful code is as follows:
%
\documentclass{article}
\usepackage{Sweave}
\usepackage{graphicx}
\SweaveOpts{prefix.string=C:/SweavePlots/bar}
\SweaveOpts{eps=true}
\DeclareGraphicsExtensions{.eps}
\title{SweavePlotWithEPS}
\author{user992432}
\begin{document}
\maketitle
\begin{figure}
\begin{center}
<<figname, fig=TRUE, include=TRUE, echo=FALSE>>=
plot(1:10)
#
\caption{I hope this Works}
\label{fig:one}
\end{center}
\end{figure}
\end{document}

Related

Saving figure to pdf prints # instead of tabs in titles

if I create a plot with some tabs in the titles and try saving that to a local pdf file (via print function), I get some hashtags instead of tabs in the pdf. This does not occur in the visible figure.
For example, I plotted the residuals of an approximant to the runge function on a grid:
plot(g, f(g) - runge(g));
title(sprintf('residuals,\t max_x(s-f) = %.3f', max(f(g)-runge(g))));
Then after some axes manipulation (grid, boxes, etc..) I execute
h = gcf;
set(h,'Units','Inches');
pos = get(h,'Position');
set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
print(h,'data/runge_example.pdf','-dpdf','-r200')
close(h)
Is somebody aware of that behaviour or better able to found that as already solved than I am?
EDIT: Same behaviour with saveas and saving to eps. This does not happen with \n
EDIT2: I'm using Matlab Version R2017b (9.3.0.7...)
So, after a long discussion we figured out that we have no idea of how to use tabs in matlab titles in combination with latex mode.
To clarify: use math environments and escape tex directives, otherwise it will throw an error.
so
sprintf('residuals,\t $max_x(\\vert s-f\\vert) = %.3f$', max(f(g)-runge(g))));
will give you a nice string:
residuals, $max_x(\vert s-f\vert) = 0.264$
The problem is, that it really is a tab. And the matlab latex interpreter (don't know which one that uses, the system or an own) crashes on that. I copy-pasted that to a tex document and pdflatex ran fine on it (but not showing that much space unfortunately).
So, I came up with the following fix:
use the latex directive \quad or \qquad:
title(sprintf('residuals,\\quad $max_x(\\vert s-f\\vert) = %.3f$', max(abs(f(g)-runge(g)))));
This will give you more space than a normal space.
EDIT: For this to work you need the interpreter of matlab to be set to "latex" instead of the default "tex". Do this by changing the title to
title(title_string, 'Interpreter', 'latex')
or by setting (globally for that script)
set(groot, 'defaultTextInterpreter', 'latex')

MATLAB eps export doesn't include legend box

I am trying to export figures from MATLAB in publication quality for importing into LaTeX documents.
I am able to set all the graphics properties I would like, however when I export my figure as a .eps, the box around the legend disappears.
MWE below (I'm using MATLAB 2014b):
figure
plot(1:4)
legend('A line!')
print('LineTest','-depsc2','-tiff')
In LaTeX:
\usepackage{graphicx}
\usepackage{epstopdf} %converting to PDF
\begin{document}
\includegraphics[width=\textwidth]{LineTest}
\end{document}
This gives the following result, which has no legend box:
It works fine for me with using Matlab 2016a and your TeX document. The following TeX document also works, try it, maybe it is an issue with your TeX compiler:
\documentclass[a4paper]{article}
\usepackage[pdftex]{graphicx}
\begin{document}
\includegraphics[width=\textwidth]{LineTest.eps}
\end{document}
Running epstopdf manually on the EPS image also gives a PDF with the legend in a box. Can you run it from the command line and see whether the problem is still there for you?
FYI my version of pdflatex:
pdfTeX 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian)
kpathsea version 6.2.1
Copyright 2015 Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
There is NO warranty. Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
Compiled with libpng 1.6.17; using libpng 1.6.17
Compiled with zlib 1.2.8; using zlib 1.2.8
Compiled with poppler version 0.41.0
I had a similar issue. I was saving two figures as eps files and using them in Latex. The figures were saved automatically using:
print(filename, '-depsc2', '-tiff')
I was using TexStudio and Miktex 2.9 with the command:
\includegraphics[width=\textwidth]{filename.eps}
The first file did not have the bounding box around the legend but the second file did.
The work around I found was to manually save the figure (Figure → Save As...) as filename.eps. The bounding box then showed up in TexStudio.

LaTeX in MATLAB multi-line titles

I need to create a two-line title in a MATLAB plot, using LaTeX in each of the lines.
title({'first line','second line'})
works, but not with LaTeX. In a single line MATLAB title, LaTeX is understood as in the example:
title(['$y=x^2$'],'interpreter','latex')
I have tried many things, but I haven't managed to have MATLAB produced a multi-line title with LaTeX in those lines.
If you run
title({'$y=x^2$','$y=x^2$'},'interpreter','latex')
you will get a two-line title with correct LaTeX-ification.
Up to version R2017a, using a cell array, as suggested by other answers,
forced left alignment. This seems to have been fixed in R2017b.
You can wrap the title in a LaTeX tabular environment:
figure;
plot((1:5).^2);
title('\begin{tabular}{c} first_line \\ second_line \end{tabular}', ...
'interpreter', 'latex')
This will let you choose text alignment. Replace {c} with either {r} or {l}, for right and left aligned text, respectively.
You can use sprintf to create the string for the title, explicitly with a newline character, '\n'.
title(sprintf('$y=x^3$\n$sin(x)$'), 'interpreter', 'latex');

Issue Matlab EPS figures

I have a problem when I create a EPS figure which I want to include in Latex. Somehow I am unable to place a caption above the figure created with Matlab. It gets overwritten by whitespace. I have created a minimum working example. The figure in matlab is created according the following code:
plot(1:10)
title('A line')
print('test','-depsc2')
And the corresponding latex file:
\documentclass[a4paper]{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}[h]
\caption{Caption is overwritten}
\includegraphics[width=\textwidth]{test.eps}
\end{figure}
\end{document}
No caption is visible in the output pdf. I am a dvi-ps-pdf chain.
It seems to be an issue with the bounding box values. If I add go to the draft mode of graphicx (\usepackage[draft]{graphicx} ) the caption appears.
Thanks for any help.
Best, Rob
Try the clip option in includegraphics:
\includegraphics[width=\textwidth, clip]{test.eps}
You may also want to include some vertical space between caption and figure:
\caption{Caption is overwritten}
\vspace{5mm}
\includegraphics[width=\textwidth, clip]{test.eps}

rmarkdown vector graphics for knit word

When using rmarkdown with knitr in Rstudio to knit a Microsoft Word document, the graphics generally look crappy because the usual vector graphics formats, such as PDF are not supported in Microsoft Word.
Fortunately, the devEMF package in R generates the microsoft vector format EMF, and I almost have this working for my rmarkdown application. The problem is that my image is showing up way too small. How can I control the image size?
I've tried the standard things below:
---
title: "Plot size trouble demo"
author: "for stackoverflow"
output: word_document
---
Here I include graphics in a word document using the `emf` printing device.
```{r dev = 'emf', fig.ext = 'emf'}
require(devEMF)
plot(1:10, 1:10)
```
The plot is small, so I try to make it bigger with the `fig.height` and fig.width` options:
```{r dev='emf', fig.ext='emf', fig.height=10, fig.width=10}
require(devEMF)
plot(1:10, 1:10)
```
The plot region stayed the same size, and now I have super-small labels and points!
Finally, I try to use the `out.width` and `out.height` options to rescale the figure to a different size, but this produces nothing at all:
```{r dev='emf', fig.ext='emf', fig.height=10, fig.width=10, out.width = '5in', out.height= '5in'}
require(devEMF)
plot(1:10, 1:10)
```
Update: I noticed here that rmarkdown supports win.metafile, but the following also produces nothing:
```{r dev = 'win.metafile', out.width = '5in', out.height= '5in'}
plot(1:10, 1:10)
```