On 18/01/2016 6:12 AM, Naresh Gurbuxani wrote:
Duncan,

Many thanks for looking at my code and for your suggestion.

Your solution works.  But my problem is different.  This code gives me a tex 
file with a lot of tikz code.  If there are several graphs in the document, 
then tex file become very large.  I would like the code to result in a pdf file 
for each graph.  When this pdf file is included in the tex file, the tex file 
is more readable.

You probably don't want to do that -- tikz outputs LaTeX code, so you'd need to run pdflatex once in every figure in your document. And you probably shouldn't care: when using Sweave, the .tex file is not really of interest. Concentrate on the .Rnw file as the source file.
However, sometimes you need to deal with other people...

You can easily redirect tikz output to a file, and \input{} that file. Just change the figure chunk to

<<name = tikzFig, echo = FALSE, results = tex>>=
library(tikzDevice)
# added height and width
tikz(file = "tikzFig.tex", width = 4, height = 3)
plot(sin, -pi, 2*pi, main = "A stand alone TikZ plot", xlab = "x", ylab = "sin(x)")
dummy <- dev.off()
cat("\\input{tikzFig.tex}")
@

As mentioned, this is a bit simpler in knitr.

Duncan Murdoch

Naresh

----------------------------------------
From: dulca...@bigpond.com
To: r-help@r-project.org
Date: Mon, 18 Jan 2016 14:17:42 +1000
Subject: Re: [R] tikzDevice and Sweave

Hi

I use Sweave and some tikz in latex but not in Sweave

Your problem is that you left out Sweave in the preamble
It must be in the preamble of any Sweave document

I added sizing so that it is not off the page.
I do not know if Sweave options will cover this or you have to set it.
eg
\setkeys{Gin}{width=1.0\textwidth}

\documentclass{article}
\usepackage{tikz}
\usepackage{Sweave}

\begin{document}

Figure~\ref{tikzExampleFig} is an example of \texttt{tikzDevice} package.

\begin{figure}
\begin{center}

<<name = tikzFig, echo = FALSE, results = tex>>=
# % <<name = tikzFig, echo = FALSE, fig = TRUE>>=

# %<<tikzFig, echo = FALSE, fig = TRUE>>=
# setwd("/Users/nareshgurbuxani/Documents/tex/tikz")
library(tikzDevice)

# added height and width
tikz(console = TRUE, width = 4, height = 3)
plot(sin, -pi, 2*pi, main = "A stand alone TikZ plot", xlab = "x", ylab =
"sin(x)")
dummy <- dev.off()
@

\caption{Example of tikz graph}
\label{tikzExampleFig}
\end{center}
\end{figure}

\end{document}

If this is an example for a larger document then have a look at the latex
hyperref package

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mac...@northnet.com.au

-----Original Message-----
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Naresh
Gurbuxani
Sent: Monday, 18 January 2016 06:54
To: R-help@r-project.org
Subject: Re: [R] tikzDevice and Sweave

Resending as a useable example

\documentclass{article}
\usepackage{tikz}

\begin{document}

Figure~\ref{tikzExampleFig} is an example of \texttt{tikzDevice} package.

\begin{figure}
\begin{center}

<<name = tikzFig, echo = FALSE, results = tex>>=
# % <<name = tikzFig, echo = FALSE, fig = TRUE>>=

# %<<tikzFig, echo = FALSE, fig = TRUE>>=
# setwd("/Users/nareshgurbuxani/Documents/tex/tikz")
library(tikzDevice)

tikz(console = TRUE)
plot(sin, -pi, 2*pi, main = "A stand alone TikZ plot", xlab = "x", ylab =
"sin(x)")
dummy <- dev.off()
@

\caption{Example of tikz graph}
\label{tikzExampleFig}
\end{center}
\end{figure}

\end{document}

----------------------------------------
Subject: Re: [R] tikzDevice and Sweave
To: naresh_gurbux...@hotmail.com; r-help@r-project.org
From: murdoch.dun...@gmail.com
Date: Sun, 17 Jan 2016 15:40:24 -0500

On 17/01/2016 3:25 PM, Naresh Gurbuxani wrote:
I want to use tikz() function in tikzDevice package so that it generates
a pdf file to be included in the bigger tex file. Below code works, but
directly inserts tikz commands in the output tex file.
This works:
<<name = tikzFig, echo = FALSE, results = tex>>=
This does not work:
<<name = tikzFig, echo = FALSE, fig = TRUE>>=
Full code is given below:
\documentclass{article}\usepackage{tikz}
Figure~\ref{tikzExampleFig} is and an example of \texttt{tikzDevice}
package.
\begin{figure}\begin{center}
<<name = tikzFig, echo = FALSE, results = tex>>=
library(tikzDevice)tikz(console = TRUE)plot(sin, -pi, pi, main = "A stand
alone TikZ plot", xlab = "x", ylab = "sin(x)")dummy <- dev.off()@
\caption{Example of tikz
graph}\label{tikzExampleFig}\end{center}\end{figure}
\end{document}

Your example isn't usable -- please post in plain text, not HTML.

I can't tell whether you are trying to use Sweave or knitr. If you're
using knitr, see the discussion of dev = "tikz" in
<http://yihui.name/knitr/>. If you're using Sweave, you probably need
pgfSweave.

Duncan Murdoch


______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
                                        
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to