0

This case won't get correctly interpreted. I doubt it must be to do something with my writing style but I cannot notice what it is. You can run it with R CMD Sweave Test.Rnw; pdflatex Test.tex produces the Test.tex file and then compiling to Test.pdf.

What is causing the running to result into gibberish? Why are the Sweave blocks not properly interpreted?

Test.Rnw causing gibberish

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{pdfpages}

\usepackage{Sweave}
\begin{document}
.
\begin{figure}
\centering
   \begin{subfigure}[b]{0.55\textwidth}
   <<>>=
    all.lab <- c("Arr./A", "Sin", "Digox")
    all.dat <- c(2274959, 1531001, 2406739)
    barplot(all.dat,
            names.arg=all.lab,
            col="darkblue",
            ylab="Average byte size",
            xlab="Groups")
    grid()
   @
   %\includepdf{fig1.pdf}
   \caption{a}
   \label{fig:Ng1}
\end{subfigure}
\begin{subfigure}[b]{0.55\textwidth}
   <<>>=
    age.lab <- c("Arr. +65", "Arr. [45,65]", "Arr", "Sin", "Sin +33")
    age.dat <- c(2274959, 1481397, 773624, 874208, 1087411)
    barplot(age.dat,
            names.arg=age.lab,
            col="darkblue",
            ylab="Average byte size",
            xlab="Groups")
    grid()
   @
   %\includepdf{fig2.pdf}
   \caption{b}
   \label{fig:Ng2}
\end{subfigure}
\begin{subfigure}[b]{0.55\textwidth}
   %\includepdf{fig3.pdf}
   <<>>=
    gender.lab <- c("Arr. f", "A m", "Sin f", "Sinu", "Digox f", "Digon m")
    gender.dat <- c(1416043, 2448017, 1421385, 537783, 1256545, 1181350)
    barplot(gender.dat,
            names.arg=gender.lab,
            col="darkblue",
            ylab="Average byte size",
            xlab="Groups")
    grid()
   @
   \caption{c}
   \label{fig:Ng2}
\end{subfigure}
\caption{MAIN CAPTION}
\end{figure}
\end{document}

enter image description here

Stefan Pinnow
  • 29,535
hhh
  • 8,743
  • 1
    You need to have something outside a float for it to compile. Put a dot before begin figure. – Hugh Nov 22 '16 at 22:22
  • 2
    You also need to supply a mandatory argument to caption. – Hugh Nov 22 '16 at 22:23
  • @Hugh I carried out the changes but the same error message stays: `Underfull \hbox (badness 10000) in paragraph at lines 43--54 \T1/aer/m/n/10 names.arg=gender.lab, col="darkblue", ! TeX capacity exceeded, sorry [input stack size=5000]. \caption@ifstrut #1#2->#1

    l.56 \end {figure} ! ==> Fatal error occurred, no output PDF file produced! Transcript written on....`

    – hhh Nov 23 '16 at 22:10
  • 2
    Use \caption{MAIN CAPTION} ie {} not [], Then it compiles fine. And the dot 'period' before \begin{figure} is not required. – R. Schumacher Nov 23 '16 at 22:30
  • 4
    I'm voting to close this question as off-topic because is solved in the comments. – Fran Sep 30 '18 at 11:09
  • @Fran it is not solved in the comments, the same output stays, have you tried to run it? – hhh Sep 30 '18 at 15:15
  • @hhh Yes, It can compiled as is actually with RStudio, showing code and the figures, but instead of using Sweave, with the default knitr. This way the chunk indentation and the lack of fig=TRUE is not longer a problem, the output is better (there are syntax highlight) and the only wrong (but not a fatal error) is use \usepackage{Sweave} that can be omitted using knitr as well as Sweave. – Fran Sep 30 '18 at 18:11
  • Anyway the result is not very good because you omit also echo=FALSE. – Fran Sep 30 '18 at 18:24

2 Answers2

2

The error is that you have indented the Sweave block, you must unindent the block. Sweave seems to be sensitive to indentation.

enter image description here

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{pdfpages}

\usepackage{Sweave}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.55\textwidth}
<<fig=TRUE>>=
all.lab <- c("Arr./A", "Sin", "Digox")
all.dat <- c(2274959, 1531001, 2406739)
barplot(all.dat,
    names.arg=all.lab,
    col="darkblue",
    ylab="Average byte size",
    xlab="Groups")
grid()
@
\caption{a}
\label{fig:Ng1}
\end{subfigure}
\begin{subfigure}[b]{0.55\textwidth}
<<fig=TRUE>>=
age.lab <- c("Arr. +65", "Arr. [45,65]", "Arr", "Sin", "Sin +33")
age.dat <- c(2274959, 1481397, 773624, 874208, 1087411)
barplot(age.dat,
    names.arg=age.lab,
    col="darkblue",
    ylab="Average byte size",
    xlab="Groups")
grid()
@
\caption{b}
\label{fig:Ng2}
\end{subfigure}
\begin{subfigure}[b]{0.55\textwidth}
<<fig=TRUE>>=
gender.lab <- c("Arr. f", "A m", "Sin f", "Sinu", "Digox f", "Digon m")
gender.dat <- c(1416043, 2448017, 1421385, 537783, 1256545, 1181350)
barplot(gender.dat,
    names.arg=gender.lab,
    col="darkblue",
    ylab="Average byte size",
    xlab="Groups")
grid()
@
\caption{c}
\label{fig:Ng2}
\end{subfigure}
\caption{MAIN CAPTION}
\end{figure}
\end{document}

Minimal Working Examples

The way I solved the issue was to encapsulate the issue, I noticed that there seems to be some conflicting character, block or space in the code. I provide below small working examples that made me realise that the error must be in the indentation.

Example 1. OP's subcase working with Sweave

\documentclass{article}
\usepackage{graphicx}
\usepackage{Sweave}

\begin{document}
\begin{figure}
<<fig=TRUE>>=
library(ggplot2)
all.lab <- c('Arr./A', 'Sin', 'Digox')
all.dat <- c(2274959, 1531001, 2406739)
ggplot() + geom_bar(aes(x=all.lab, y=all.dat), stat='identity')
@
\end{figure}
\end{document}

enter image description here

Example 2. Ggplot working with Sweave

\documentclass{article}
\usepackage{graphicx}
\usepackage{Sweave}
\begin{document}
\SweaveOpts{concordance=TRUE}

\begin{figure}
\centering
<<fig=TRUE>>=
library(ggplot2)
p <- ggplot(diamonds, aes(x=carat))
p <- p + geom_histogram()
print(p)
@
\end{figure}
\end{document}

enter image description here

Example 3. Base commands working with Sweave

\documentclass{article}
\usepackage{graphicx}         
\usepackage{Sweave}
\begin{document}
\begin{figure}
\centering
<<fig=TRUE>>=
plot(1:10)
@
\end{figure}
\end{document}

enter image description here

Run

  1. R CMD Sweave test.Rnw
  2. pdflatex test.txt
  3. open test.pdf
hhh
  • 8,743
2

With Rstudio using knitr, your code as is can be compiled showing the plots, but you have to add echo=FALSE to hide the R code and fit the three images in a page. Of course, the \usepackage{Sweave} is not necessary (nor using Sweave with Rstudio). You can omit also any text outside the float.

If you need to stay with Sweave then you should care of the chunk indentation, fig=TRUE option, and of course, at least for a float with three subfigures, omit the echo.

mwe

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{pdfpages}
\begin{document}
\begin{figure}
\centering
   \begin{subfigure}[b]{0.55\textwidth}
   <<echo=F>>=
    all.lab <- c("Arr./A", "Sin", "Digox")
    all.dat <- c(2274959, 1531001, 2406739)
    barplot(all.dat,
            names.arg=all.lab,
            col="darkblue",
            ylab="Average byte size",
            xlab="Groups")
    grid()
   @
   %\includepdf{fig1.pdf}
   \caption{a}
   \label{fig:Ng1}
\end{subfigure}
\begin{subfigure}[b]{0.55\textwidth}
   <<echo=F>>=
    age.lab <- c("Arr. +65", "Arr. [45,65]", "Arr", "Sin", "Sin +33")
    age.dat <- c(2274959, 1481397, 773624, 874208, 1087411)
    barplot(age.dat,
            names.arg=age.lab,
            col="darkblue",
            ylab="Average byte size",
            xlab="Groups")
    grid()
   @
   %\includepdf{fig2.pdf}
   \caption{b}
   \label{fig:Ng2}
\end{subfigure}
\begin{subfigure}[b]{0.55\textwidth}
   %\includepdf{fig3.pdf}
   <<echo=F>>=
    gender.lab <- c("Arr. f", "A m", "Sin f", "Sinu", "Digox f", "Digon m")
    gender.dat <- c(1416043, 2448017, 1421385, 537783, 1256545, 1181350)
    barplot(gender.dat,
            names.arg=gender.lab,
            col="darkblue",
            ylab="Average byte size",
            xlab="Groups")
    grid()
   @
   \caption{c}
   \label{fig:Ng2}
\end{subfigure}
\caption{MAIN CAPTION}
\end{figure}
\end{document}

Moreover, knitr is more powerful: Just consider you "example 2" with knitr:

enter image description here

\documentclass{article}
\begin{document}
\begin{figure}
    <<>>=
        library(ggplot2)
        p <- ggplot(diamonds, aes(x=carat))
        p <- p + geom_histogram(bins=30)
        print(p)
    @
\end{figure}
\end{document}
Fran
  • 80,769
  • Can you compile knitr on commandline like Sweave? Or do you need Rstudio to run this? I found an answer here: Rscript -e "library(knitr); knit('my_sweave_file.Rnw')" pdflatex my_sweave_file.tex so it is similar, why would you use Knitr over Sweave? – hhh Sep 30 '18 at 18:45
  • 1
    @hhh Of course: https://tex.stackexchange.com/a/132724/11604 – Fran Sep 30 '18 at 18:49
  • 1
    @Why knitr? Please my updated answer (but is not only for the syntax highlight, there are many advantages and many chunk options.) – Fran Sep 30 '18 at 18:54
  • It seems that knitr could be understood as a progression of Sweave while current new projects such Radix (built on Distill NPM/Node framework) built on top of Knitr? So if I want to visualise R results on the web, it seems that I could do that with Knitr and Radix?

    https://rstudio.github.io/radix/

    – hhh Sep 30 '18 at 19:03
  • 1
    @hhh With knitr you can make LaTeX files that can produce a PDF or a web page, or HTML page directly, or a markdown file that with pandoc could be in several formats. As far I saw Radix is only for markdow to produce web pages and it does not depend on knitr. – Fran Sep 30 '18 at 19:51