4

I can't get the following to work:

\begin{minipage}
<<percentile, echo=FALSE>>=
your_rank <- percentile  ## calculated earlier
rank_message <- ifelse(is.na(score), '   ', paste0("You are in the top ", your_rank, "% of participants."))
@
\begin{center} 
\fbox{
\parbox{15cm}{\centering
\textbf{\Sexpr{rank_message}} \\
} % End parbox
} % End fbox
\end{center}  
\end{minipage}

I get the error:

File ended when scanning use of \fbox

Then, if I put \ before the % ie

paste0("You are in the top ", your_rank, "\% of participants.")

then the R part gives me an error:

Error in eval(parse_only(code), envir = envir) :

How can I get around this?

Update - putting the R chunk in a group doesn't work. Here is a working example - with the percentage symbol removed:

\documentclass[a4paper]{article}
\usepackage{fullpage}
\usepackage{pdflscape}  % landscape format
\begin{document}
\begin{landscape}
\begin{minipage}[t][5cm]{0.6\linewidth} 
\begingroup\catcode`\%=12
<<percentile, echo=FALSE>>=
score <- 10
your_rank <- 30
rank_message <- ifelse(is.na(score), '   ', paste0("You are in the top ", your_rank, " of participants"))
@
\endgroup
\begin{center} 
\fbox{
\parbox{15cm}{\centering
\textbf{\Sexpr{rank_message}} \\
} % End parbox
} % End fbox
\end{center}  
\end{minipage}
\end{landscape}
\end{document}

Update: Ross's comment did the trick. Thanks Ross.

  • Try surrounding the R chunk with \begingroup\catcode%=12 <>= ... @ \endgroup`. If that doesn't work we need a minimal working example (MWE) – Henri Menke May 07 '18 at 02:18
  • "\% of participants" should work. – Ross May 07 '18 at 04:40
  • @Ross Would you like to add an answer? – CarLaTeX May 08 '18 at 19:29
  • Others may discover this happening when using the {lscape} package to make a page landscape. It seems to turn everything within its tags to LateX, so you'll have to escape any characters within your code chunks AND markdown like they would be in the Tex file. Text that should be below your table is now above it, check your new lines (\ must become \). Same for any symbols that must be escaped, %. – Anonymous coward Aug 11 '21 at 20:00

1 Answers1

3

Quoting from Yuhai Xie's book, "Dynamic Documents with R and knitr" (2015, 2nd ed., p1):

The basic idea behind dynamic documents stems from literate programming, a programming paradigm conceived by Donald Knuth (Knuth, 1984). The original idea was mainly for writing software: mix the source code and documentation together; we can either extract the source code out (called tangle) or execute the code to get the compiled results (called weave). A dynamic document is not entirely different from a computer program: for a dynamic document, we need to run software packages to compile our ideas (often implemented as source code) into numeric or graphical output, and insert the output into our literal writings (like documentation).

This question asks how do I weave the output of my R code into my LaTeX document when a line in the R code includes a % symbol?

This code needs to be run in the editor, RStudio, which provides the ability to compile the code. The result of the compilation is that the R code and its output, is weaved into the LaTeX document.

The R code is delimited between <<>>= and @. The solution to the OP's problem is that % must be escaped twice i.e. \\%. The first backslash preserves the second backslash, which needs to be written to the TeX file produced by compilation. Thus, on compilation, the R string:

paste0("You are in the top ", your_rank, "\\% of participants"))

will write the following string to the TeX file:

You are in the top 30\% of participants

When the TeX file is compiled by pdfLaTeX or XeLaTeX, it compiles correctly.

This is the complete code:

\documentclass[a4paper]{article}
%\usepackage{fullpage}
\usepackage{pdflscape}  % landscape format
\usepackage[utf8]{inputenc}
\pagestyle{empty}
\begin{document}
\begin{landscape}
\begin{minipage}[t][5cm]{0.6\linewidth} 
%\begingroup\catcode`\%=12
<<percentile, echo=FALSE>>=
  score <- 10
  your_rank <- 30
  rank_message <- ifelse(is.na(score), '   ', paste0("You are in the top ", your_rank, "\\% of participants"))
  @
%    \endgroup
  \begin{center} 
  \fbox{
    \parbox{15cm}{\centering
      \textbf{\Sexpr{rank_message}} \\
    } % End parbox
  } % End fbox
  \end{center}  
  \end{minipage}
  \end{landscape}
  \end{document}

This is the output:

enter image description here

Ross
  • 5,656
  • 2
  • 14
  • 38