\documentclass{article} \usepackage{scrtime} \usepackage{hyperref} \usepackage{url} \usepackage{verbatim} \usepackage{listings} \lstset{language=R} \usepackage{graphicx} \DeclareGraphicsExtensions{.pdf} \newcommand{\code}[1]{{\tt #1}} \SweaveOpts{warning=FALSE,dev="png"} \title{\code{knitr} $\to$ \LaTeX $\to$ HTML testing} \date{\today @ \thistime} \begin{document} \maketitle \section{introduction} I have been trying to find an Sweave $\to$ \LaTeX\ $\to$ HTML pathway that works via \code{knitr} \ldots I think I can get it to work now, although it's not as pretty as I'd like. The bottom line is that to get this working you need to \begin{itemize} \item include \verb+render_listings()+ in an R chunk near the top of your file (I prefer the default highlighting on aesthetic grounds, but this works better with existing \LaTeX-to-HTML tools) \item add \verb+\usepackage{graphicx}+; \verb+\DeclareGraphicsExtension{.pdf}+; \verb+\usepackage{listings}+; \verb+\lstset{language=R}+ to the preamble of your document, and make sure that the \href{http://biostat.mc.vanderbilt.edu/wiki/pub/Main/SweaveTemplate/Sweavel.sty}{Sweavel.sty} file is available. \item make sure you have \code{myhtml.cfg} file available (see the end of this document) \item run \code{htlatex} as follows: \begin{verbatim} htlatex $*.tex myhtml "" "" "-interaction=nonstopmode" \end{verbatim} where (of course) \verb+$*+ is the basename of your document (this line is from a Makefile) \end{itemize} \section{example} <>= ## normally also include=FALSE render_listings() @ (An equation) \begin{equation} a+b \end{equation} Some code: <<>>= ## here's a comment ... (x <- 5^2) y <- 17 @ A figure: <<>>= set.seed(101) plot(rnorm(10),rnorm(10)) @ Inline R expression: the value of $x$ is \Sexpr{x}. Session info: <<>>= sessionInfo() @ \section{issues} \begin{itemize} \item The assignment arrow \verb+<-+ inside the \code{Sinput} chunk confuses \code{htlatex} --- have to run \code{htlatex} with \code{interaction=nonstopmode} (see below) --- but the final document seems to come out OK. Is there a fix to \code{Sweavel.sty}, or something else, that will make it work? \item I don't have pretty syntax highlighting working for htlatex yet. \end{itemize} \section{references} \begin{itemize} \item From Yihui Xie, about using \verb+render_listings()+ (and the fact that he prefers to start with a markdown document): \url{https://groups.google.com/forum/#!msg/knitr/Dv2JmcDcuBo/dYkp2rEuSlAJ} \item{htlatex image processing: \url{http://tex.stackexchange.com/questions/46156/pdf-image-files-and-htlatex}} \item{htlatex formatting (I don't have this working right yet): \url{http://tex.stackexchange.com/questions/7216/how-to-get-tex4ht-to-respect-listings-styling-better}} \item{more on htlatex/listings formatting (haven't tried this yet): \url{http://tex.stackexchange.com/questions/64054/latex-to-html-preserving-code-coloring-from-listings}} \end{itemize} \section{latexml} \code{latexml} is an alternative conversion program that I haven't managed to get working equally well \ldots <>= system("latexml --dest='knitr_listings_test.xml' knitr_listings_test.tex") system("latexmlpost --dest='knitr_listings_test.xhtml' knitr_listings_test.xml") @ \section{\code{myhtml.cfg}} \VerbatimInput[frame=single]{myhtml.cfg} \end{document}