The problem is that the \begin{...}
and \end{...}
pair commands automatically create a "group" so that, in effect, the &
and \\
are "out of scope" for the tabular, while inside the "production" environment they just show up at a place where the compiler is not expecting them.
A second problem with your definitions is that, even if they would work, they would be adding an extra \\
at the end of the tabular, adding an unwanted space at the end. Perhaps some more appropriate definitions would be
\documentclass[a4paper,10pt]{article}
\newcommand{\production}[1]{#1 ::= &}
\newenvironment{grammar}{\tabular{p{3cm}l}}{\endtabular}
\begin{document}
\begin{grammar}
\production{XmlStartTag} ... \\
\production{XmlOtherTag} ... \\
\production{XmlEndTag} ...
\end{grammar}
\end{document}
Note no \\
at the end of the last production. Also in the definition of the grammar
you don't need to repeat the work of \begin
/\end
, and you can instead directly use \tabular
and \endtabular
.
Here is a quick demo of linking R and LaTeX using the 'knitr' package. This has been run on Windows 8.1
, with MikTeX 2.9
, and TeXmaker 4.4.1
as the IDE. The following code is saved as knit02.Rnw
(and this is case sensitive). With the package 'knitr' installed in R 3.1.3 you run the command: knit("knit02.Rnw")
. This will generate the file 'knit02.tex' which you now compile with pdflatex
and view as a pdf
.
The example has three parts. First, a simple longtable; second, a simple tablular; and third, a multi data.frame combined into one longtable. The output is still not optimal, especially in the headings. However at this point, you will have to migrate over to https://stackoverflow.com/questions/tagged/r and ask specific questions on how to use xtable to pass the customizations of longtable.
\documentclass[10pt,letterpaper]{article}
\usepackage{longtable}
\usepackage[tmargin=2in,bmargin=2in]{geometry} % Done to force early page changes for demonstration.
\begin{document}
\textbf{Note: In all three of these examples the \LaTeX{} environment is determined in the R code block. }
Demonstration of a very large R table exported to \LaTeX{] using the package \textit{longtable}.
<<echo=FALSE>>=
library(xtable)
set.seed(1)
@
<<echo=FALSE,results='asis'>>=
## Demonstration of longtable support.
## Remember to insert \usepackage{longtable} on your LaTeX preamble
x <- matrix(rnorm(500), ncol = 10)
x.big <- xtable(x, label ='tabbig',caption ='Example of longtable spanning several pages')
print(x.big, tabular.environment ='longtable', floating = FALSE)
@
\clearpage
Now here is a regular (as in fit on one page) table using \textit{tabular}.
<<echo=FALSE,results='asis'>>=
x <- x[1:20, ]
x.small <- xtable(x, label ='tabsmall', caption ='regular table env')
print(x.small) # default, no longtable
@
\clearpage
Demonstration of a large R table composed of multiple data.frames exported to \LaTeX{] using the package \textit{longtable}.
<<echo=FALSE,results='asis'>>=
## Demonstration of longtable support with combining two data.frames.
## Remember to insert \usepackage{longtable} on your LaTeX preamble
x <- rnorm(100)
y <- rnorm(100)
id<- rep(letters[1:25],4)
spacecolumn<- rep(" ",100)
dtableone<-data.frame(id,x,y)
dtabletwo<-data.frame(id,y,x)
fulltable<-data.frame(dtableone,spacecolumn,dtabletwo)
names(fulltable)<-c("id","x","y"," ","id","y","x")
x.big <- xtable(fulltable, label ='tabtwo',caption ='Example of longtable spanning several pages build from 2 data.frames')
print(x.big, tabular.environment ='longtable', floating = FALSE)
@
\end{document}
Page 1
Page 2
Page 3
Page 4
Page 5
Page 6
Best Answer
\include
_always forces a page break, use\input
, and then allow the figure to be on the same page by includingh
in its option.