-1

Let's say I have a simple plot like this:

mu    = matrix(c(0,0),2,1)
sigma = matrix(c(1,0,0,1),2,2)
x = mvrnorm(1000, mu, sigma)

plot(x,pch=20,panel.first = grid(10, lty = 2, lwd = 2))

How can I export this plot to a .tex file fromat? I mean direct output to tex and I don't want to use TIKZ.


Here is how you do it in gnuplot

set terminal latex
set output "eg2.tex"
set size 5/5., 4/3.
set format xy "$%g$"
set title ’This is a plot of $y=\sin(x)$’
set xlabel ’This is the $x$ axis’
set ylabel ’This is\\the\\$y$ axis’
plot [0:6.28] [0:1] sin(x)

and then you will include the tex file in your tex document.


I may be making a mistake but the out put of the above from gnuplot is the following:

% GNUPLOT: LaTeX picture
\setlength{\unitlength}{0.240900pt}
\ifx\plotpoint\undefined\newsavebox{\plotpoint}\fi
\sbox{\plotpoint}{\rule[-0.200pt]{0.400pt}{0.400pt}}%
\begin{picture}(1500,900)(0,0)
\sbox{\plotpoint}{\rule[-0.200pt]{0.400pt}{0.400pt}}%
\put(110.0,82.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(90,82){\makebox(0,0)[r]{$0$}}
\put(1419.0,82.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(110.0,237.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(90,237){\makebox(0,0)[r]{$0.2$}}
\put(1419.0,237.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(110.0,393.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(90,393){\makebox(0,0)[r]{$0.4$}}
\put(1419.0,393.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(110.0,548.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(90,548){\makebox(0,0)[r]{$0.6$}}
\put(1419.0,548.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(110.0,704.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(90,704){\makebox(0,0)[r]{$0.8$}}
\put(1419.0,704.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(110.0,859.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(90,859){\makebox(0,0)[r]{$1$}}
\put(1419.0,859.0){\rule[-0.200pt]{4.818pt}{0.400pt}}
\put(110.0,82.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(110,41){\makebox(0,0){$0$}}
\put(110.0,839.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(322.0,82.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(322,41){\makebox(0,0){$1$}}
\put(322.0,839.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(533.0,82.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(533,41){\makebox(0,0){$2$}}
\put(533.0,839.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(745.0,82.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(745,41){\makebox(0,0){$3$}}
\put(745.0,839.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(956.0,82.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(956,41){\makebox(0,0){$4$}}
\put(956.0,839.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(1168.0,82.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(1168,41){\makebox(0,0){$5$}}
\put(1168.0,839.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(1380.0,82.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(1380,41){\makebox(0,0){$6$}}
\put(1380.0,839.0){\rule[-0.200pt]{0.400pt}{4.818pt}}
\put(110.0,82.0){\rule[-0.200pt]{0.400pt}{187.179pt}}
\put(110.0,82.0){\rule[-0.200pt]{320.156pt}{0.400pt}}
\put(1439.0,82.0){\rule[-0.200pt]{0.400pt}{187.179pt}}
\put(110.0,859.0){\rule[-0.200pt]{320.156pt}{0.400pt}}
\put(110,82){\usebox{\plotpoint}}
\multiput(110.58,82.00)(0.493,1.924){23}{\rule{0.119pt}{1.608pt}}
\multiput(109.17,82.00)(13.000,45.663){2}{\rule{0.400pt}{0.804pt}}
\multiput(123.58,131.00)(0.494,1.782){25}{\rule{0.119pt}{1.500pt}}
\multiput(122.17,131.00)(14.000,45.887){2}{\rule{0.400pt}{0.750pt}}
\multiput(137.58,180.00)(0.493,1.924){23}{\rule{0.119pt}{1.608pt}}
\multiput(136.17,180.00)(13.000,45.663){2}{\rule{0.400pt}{0.804pt}}
\multiput(150.58,229.00)(0.494,1.745){25}{\rule{0.119pt}{1.471pt}}
\multiput(149.17,229.00)(14.000,44.946){2}{\rule{0.400pt}{0.736pt}}
\multiput(164.58,277.00)(0.493,1.845){23}{\rule{0.119pt}{1.546pt}}
\multiput(163.17,277.00)(13.000,43.791){2}{\rule{0.400pt}{0.773pt}}
\multiput(177.58,324.00)(0.494,1.709){25}{\rule{0.119pt}{1.443pt}}
\multiput(176.17,324.00)(14.000,44.005){2}{\rule{0.400pt}{0.721pt}}
\multiput(191.58,371.00)(0.493,1.765){23}{\rule{0.119pt}{1.485pt}}
\multiput(190.17,371.00)(13.000,41.919){2}{\rule{0.400pt}{0.742pt}}
\multiput(204.58,416.00)(0.493,1.726){23}{\rule{0.119pt}{1.454pt}}
\multiput(203.17,416.00)(13.000,40.982){2}{\rule{0.400pt}{0.727pt}}
\multiput(217.58,460.00)(0.494,1.525){25}{\rule{0.119pt}{1.300pt}}
\multiput(216.17,460.00)(14.000,39.302){2}{\rule{0.400pt}{0.650pt}}
\multiput(231.58,502.00)(0.493,1.567){23}{\rule{0.119pt}{1.331pt}}
\multiput(230.17,502.00)(13.000,37.238){2}{\rule{0.400pt}{0.665pt}}
\multiput(244.58,542.00)(0.494,1.415){25}{\rule{0.119pt}{1.214pt}}
\multiput(243.17,542.00)(14.000,36.480){2}{\rule{0.400pt}{0.607pt}}
\multiput(258.58,581.00)(0.493,1.448){23}{\rule{0.119pt}{1.238pt}}
\multiput(257.17,581.00)(13.000,34.430){2}{\rule{0.400pt}{0.619pt}}
\multiput(271.58,618.00)(0.494,1.268){25}{\rule{0.119pt}{1.100pt}}
\multiput(270.17,618.00)(14.000,32.717){2}{\rule{0.400pt}{0.550pt}}
\multiput(285.58,653.00)(0.493,1.250){23}{\rule{0.119pt}{1.085pt}}
\multiput(284.17,653.00)(13.000,29.749){2}{\rule{0.400pt}{0.542pt}}
\multiput(298.58,685.00)(0.493,1.171){23}{\rule{0.119pt}{1.023pt}}
\multiput(297.17,685.00)(13.000,27.877){2}{\rule{0.400pt}{0.512pt}}
\multiput(311.58,715.00)(0.494,0.974){25}{\rule{0.119pt}{0.871pt}}
\multiput(310.17,715.00)(14.000,25.191){2}{\rule{0.400pt}{0.436pt}}
\multiput(325.58,742.00)(0.493,0.972){23}{\rule{0.119pt}{0.869pt}}
\multiput(324.17,742.00)(13.000,23.196){2}{\rule{0.400pt}{0.435pt}}
\multiput(338.58,767.00)(0.494,0.791){25}{\rule{0.119pt}{0.729pt}}
\multiput(337.17,767.00)(14.000,20.488){2}{\rule{0.400pt}{0.364pt}}
\multiput(352.58,789.00)(0.493,0.734){23}{\rule{0.119pt}{0.685pt}}
\multiput(351.17,789.00)(13.000,17.579){2}{\rule{0.400pt}{0.342pt}}
\multiput(365.58,808.00)(0.493,0.616){23}{\rule{0.119pt}{0.592pt}}
\multiput(364.17,808.00)(13.000,14.771){2}{\rule{0.400pt}{0.296pt}}
\multiput(378.00,824.58)(0.536,0.493){23}{\rule{0.531pt}{0.119pt}}
\multiput(378.00,823.17)(12.898,13.000){2}{\rule{0.265pt}{0.400pt}}
\multiput(392.00,837.58)(0.652,0.491){17}{\rule{0.620pt}{0.118pt}}
\multiput(392.00,836.17)(11.713,10.000){2}{\rule{0.310pt}{0.400pt}}
\multiput(405.00,847.59)(1.026,0.485){11}{\rule{0.900pt}{0.117pt}}
\multiput(405.00,846.17)(12.132,7.000){2}{\rule{0.450pt}{0.400pt}}
\multiput(419.00,854.60)(1.797,0.468){5}{\rule{1.400pt}{0.113pt}}
\multiput(419.00,853.17)(10.094,4.000){2}{\rule{0.700pt}{0.400pt}}
\put(432,857.67){\rule{3.373pt}{0.400pt}}
\multiput(432.00,857.17)(7.000,1.000){2}{\rule{1.686pt}{0.400pt}}
\put(446,857.17){\rule{2.700pt}{0.400pt}}
\multiput(446.00,858.17)(7.396,-2.000){2}{\rule{1.350pt}{0.400pt}}
\multiput(459.00,855.93)(1.123,-0.482){9}{\rule{0.967pt}{0.116pt}}
\multiput(459.00,856.17)(10.994,-6.000){2}{\rule{0.483pt}{0.400pt}}
\multiput(472.00,849.93)(0.890,-0.488){13}{\rule{0.800pt}{0.117pt}}
\multiput(472.00,850.17)(12.340,-8.000){2}{\rule{0.400pt}{0.400pt}}
\multiput(486.00,841.92)(0.539,-0.492){21}{\rule{0.533pt}{0.119pt}}
\multiput(486.00,842.17)(11.893,-12.000){2}{\rule{0.267pt}{0.400pt}}
\multiput(499.00,829.92)(0.497,-0.494){25}{\rule{0.500pt}{0.119pt}}
\multiput(499.00,830.17)(12.962,-14.000){2}{\rule{0.250pt}{0.400pt}}
\multiput(513.58,814.29)(0.493,-0.695){23}{\rule{0.119pt}{0.654pt}}
\multiput(512.17,815.64)(13.000,-16.643){2}{\rule{0.400pt}{0.327pt}}
\multiput(526.58,796.21)(0.494,-0.717){25}{\rule{0.119pt}{0.671pt}}
\multiput(525.17,797.61)(14.000,-18.606){2}{\rule{0.400pt}{0.336pt}}
\multiput(540.58,775.52)(0.493,-0.933){23}{\rule{0.119pt}{0.838pt}}
\multiput(539.17,777.26)(13.000,-22.260){2}{\rule{0.400pt}{0.419pt}}
\multiput(553.58,751.26)(0.493,-1.012){23}{\rule{0.119pt}{0.900pt}}
\multiput(552.17,753.13)(13.000,-24.132){2}{\rule{0.400pt}{0.450pt}}
\multiput(566.58,725.26)(0.494,-1.011){25}{\rule{0.119pt}{0.900pt}}
\multiput(565.17,727.13)(14.000,-26.132){2}{\rule{0.400pt}{0.450pt}}
\multiput(580.58,696.63)(0.493,-1.210){23}{\rule{0.119pt}{1.054pt}}
\multiput(579.17,698.81)(13.000,-28.813){2}{\rule{0.400pt}{0.527pt}}
\multiput(593.58,665.55)(0.494,-1.231){25}{\rule{0.119pt}{1.071pt}}
\multiput(592.17,667.78)(14.000,-31.776){2}{\rule{0.400pt}{0.536pt}}
\multiput(607.58,631.11)(0.493,-1.369){23}{\rule{0.119pt}{1.177pt}}
\multiput(606.17,633.56)(13.000,-32.557){2}{\rule{0.400pt}{0.588pt}}
\multiput(620.58,596.08)(0.494,-1.378){25}{\rule{0.119pt}{1.186pt}}
\multiput(619.17,598.54)(14.000,-35.539){2}{\rule{0.400pt}{0.593pt}}
\multiput(634.58,557.48)(0.493,-1.567){23}{\rule{0.119pt}{1.331pt}}
\multiput(633.17,560.24)(13.000,-37.238){2}{\rule{0.400pt}{0.665pt}}
\multiput(647.58,517.35)(0.493,-1.607){23}{\rule{0.119pt}{1.362pt}}
\multiput(646.17,520.17)(13.000,-38.174){2}{\rule{0.400pt}{0.681pt}}
\multiput(660.58,476.48)(0.494,-1.562){25}{\rule{0.119pt}{1.329pt}}
\multiput(659.17,479.24)(14.000,-40.242){2}{\rule{0.400pt}{0.664pt}}
\multiput(674.58,432.96)(0.493,-1.726){23}{\rule{0.119pt}{1.454pt}}
\multiput(673.17,435.98)(13.000,-40.982){2}{\rule{0.400pt}{0.727pt}}
\multiput(687.58,389.13)(0.494,-1.672){25}{\rule{0.119pt}{1.414pt}}
\multiput(686.17,392.06)(14.000,-43.065){2}{\rule{0.400pt}{0.707pt}}
\multiput(701.58,342.58)(0.493,-1.845){23}{\rule{0.119pt}{1.546pt}}
\multiput(700.17,345.79)(13.000,-43.791){2}{\rule{0.400pt}{0.773pt}}
\multiput(714.58,295.89)(0.494,-1.745){25}{\rule{0.119pt}{1.471pt}}
\multiput(713.17,298.95)(14.000,-44.946){2}{\rule{0.400pt}{0.736pt}}
\multiput(728.58,247.45)(0.493,-1.884){23}{\rule{0.119pt}{1.577pt}}
\multiput(727.17,250.73)(13.000,-44.727){2}{\rule{0.400pt}{0.788pt}}
\multiput(741.58,199.33)(0.493,-1.924){23}{\rule{0.119pt}{1.608pt}}
\multiput(740.17,202.66)(13.000,-45.663){2}{\rule{0.400pt}{0.804pt}}
\multiput(754.58,150.77)(0.494,-1.782){25}{\rule{0.119pt}{1.500pt}}
\multiput(753.17,153.89)(14.000,-45.887){2}{\rule{0.400pt}{0.750pt}}
\multiput(768.59,101.42)(0.485,-1.942){11}{\rule{0.117pt}{1.586pt}}
\multiput(767.17,104.71)(7.000,-22.709){2}{\rule{0.400pt}{0.793pt}}
\put(110.0,82.0){\rule[-0.200pt]{0.400pt}{187.179pt}}
\put(110.0,82.0){\rule[-0.200pt]{320.156pt}{0.400pt}}
\put(1439.0,82.0){\rule[-0.200pt]{0.400pt}{187.179pt}}
\put(110.0,859.0){\rule[-0.200pt]{320.156pt}{0.400pt}}
\end{picture}

Now you saved the above tex file and you will include a tex file as graphics in your tex document but it will never be a PDF file I believe.

Ash
  • 336
  • 4
  • 19
  • 4
    Why not save it as a `pdf()` and insert as a figure in your tex file? – erasmortg Jul 01 '15 at 20:51
  • You could also use `knitr` and use R code directly in your tex file – nico Jul 01 '15 at 20:52
  • 3
    This question does not really make sense as it is written. tex files do not include images. They can reference an image to be included in the compiled pdf. – Seth Jul 01 '15 at 20:54
  • Maybe the intent underneath this question is to export a .tex file that contains the plot(s) generated in R regardless of the format of the image itself but considering the .tex file as a whole. Maybe. – SabDeM Jul 01 '15 at 20:56
  • @erasmortg Parameters such as font type, size etc won't be the same as your tex document. – Ash Jul 01 '15 at 21:01
  • @nico the purpose is not to use an extra package. Pretty much like **gnuplot** – Ash Jul 01 '15 at 21:05
  • @Ash I am not quite sure why using a package is a bad thing, especially when it is designed exactly to provide the functionality you are looking for. – nico Jul 01 '15 at 21:07
  • @nico using packages is not necessarily good. They have their own issues. Plain output is the best. – Ash Jul 01 '15 at 21:12
  • @Ash seems to me you are overcomplicating a very simple issue. It's as simple as writing a single Rnw file and you're set. It's all in there, LaTeX and R, and it just works. Otherwise R is simply not the right tool for what you want to do. – nico Jul 01 '15 at 21:24
  • Hey @nico I'm actually simplifying it. I don't think there is any complication involved in getting direct tex output if possible? would you give an example how it overcomplicates it compared to when I use packages? – Ash Jul 01 '15 at 22:24
  • It overcomplicates things because it is much more complex to change the plot and if you increase complexity of the plot it may get very difficult to maintain. With knitr you just write R code inside your LaTeX code, so changing the plot is just a matter of changing the R code – nico Jul 01 '15 at 22:33
  • @nico I'm not sure. What you say means maintaining codes in two different locations and two different languages and I don't think it is better than integrating the related ones together. This makes debugging (although not a big deal for simple plots) difficult. Also my plots are the end results of few algorithms and I need to run each of them several times, it's much easier to just export them as tex figures and simply compile the latex. Your method is fine but I'm not sure how efficient it is. – Ash Jul 01 '15 at 22:49
  • No, you have just one file, with R and LaTeX, that's all. Personally I find it much easier to maintain, that's the way I generate all my reports. Because R code is run every time, you don't have to worry about which plot may be changed, they will be automatically updated in the generated tex file when knitting the output. The purpose of knitr is exactly that. I suggest you have a look at their website – nico Jul 01 '15 at 22:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82141/discussion-between-ash-and-nico). – Ash Jul 01 '15 at 22:57

1 Answers1

1

If for some reason you don't want to use tikz and the excellent tikzDevice package, you could try ?pictex, but its help page suggests that it's not fully operational.

pictex()
plot(1,1)
dev.off()

\hbox{\beginpicture
\setcoordinatesystem units <1pt,1pt>
\setplotarea x from 0 to 361.35, y from 0 to 289.08
\setlinear
\font\picfont cmss10\picfont
\font\picfont cmss10 at 10pt\picfont
\font\picfont cmss10 at 10pt\picfont
\circulararc 360 degrees from 180.68 164.79 center at 180.68 162.54
\setsolid
\plot 58.94 61.20 302.41 61.20 /
\setsolid
\plot 58.94 61.20 58.94 55.20 /
\setsolid
\plot 119.81 61.20 119.81 55.20 /
\setsolid
\plot 180.68 61.20 180.68 55.20 /
\setsolid
\plot 241.54 61.20 241.54 55.20 /
\setsolid
\plot 302.41 61.20 302.41 55.20 /
\put {0.6}  [lB] <0.00pt,0.00pt> at 52.55 37.20
\put {0.8}  [lB] <0.00pt,0.00pt> at 113.42 37.20
\put {1.0}  [lB] <0.00pt,0.00pt> at 174.29 37.20
\put {1.2}  [lB] <0.00pt,0.00pt> at 235.15 37.20
\put {1.4}  [lB] <0.00pt,0.00pt> at 296.02 37.20
\setsolid
\plot 49.20 68.71 49.20 256.37 /
\setsolid
\plot 49.20 68.71 43.20 68.71 /
\setsolid
\plot 49.20 115.62 43.20 115.62 /
\setsolid
\plot 49.20 162.54 43.20 162.54 /
\setsolid
\plot 49.20 209.46 43.20 209.46 /
\setsolid
\plot 49.20 256.37 43.20 256.37 /
\put {\rotatebox{90}{0.6} } [rB] <0.00pt,0.00pt> at 37.20 62.32
\put {\rotatebox{90}{0.8} } [rB] <0.00pt,0.00pt> at 37.20 109.23
\put {\rotatebox{90}{1.0} } [rB] <0.00pt,0.00pt> at 37.20 156.15
\put {\rotatebox{90}{1.2} } [rB] <0.00pt,0.00pt> at 37.20 203.07
\put {\rotatebox{90}{1.4} } [rB] <0.00pt,0.00pt> at 37.20 249.98
\setsolid
\plot 49.20 61.20 312.15 61.20 /
\plot 312.15 61.20 312.15 263.88 /
\plot 312.15 263.88 49.20 263.88 /
\plot 49.20 263.88 49.20 61.20 /
\put {1}  [lB] <0.00pt,0.00pt> at 178.17 13.20
\put {\rotatebox{90}{1} } [rB] <0.00pt,0.00pt> at 13.20 160.04
\endpicture
}

Last resort, write your own device, or use a R–gnuplot interface (TeachingDemos has one, apparently), but I highly doubt it's worth it.

baptiste
  • 75,767
  • 19
  • 198
  • 294