knitr Rnw file have optional output in child - knitr

I use Rnw files to create exams for my students, I put separate quiz into child Rnw file and include them in the main Rnw in the way
<<child="xx.Rnw">>#
The xx.Rnw contains the problem statement and also the answer. I want to have two versions of the exam, one without the answer and one with the answer. Which means I need something that could conditionally generate two types of pdf.
The answer part is a mixer of <<>># code and latex. Anyone has a good idea how to do this in an agile way? Thanks!

For each problem statement you could create 2 child rnw chunks. The first chunk calls the rnw without the answers and the second chunk calls the rnw with the answer. At the beginning of the rnw document, create 2 variables called hide_answer and show_answer and set the eval chunk option of the child rnw chunks that omit the answer to hide_answer and set the eval chunk option of the child rnw chunks that show the answer to show_answer.
Then all you have to do is set the variables to TRUE or FALSE to generate the desired PDF.
<<r_load>>=
hide_answer = TRUE
if (hide_answer == TRUE) {
show_answer = FALSE
} else {
show_answer = TRUE
}
#
\documentclass{article}
\begin{document}
<<child_hide_answer, child=problem1_no_answer.rnw, eval=hide_answer>>=
#
<<child_shower_answer, child=problem1_with_answer.rnw, eval=show_answer>>=
#
\end{document}

Related

what does means #+keyword: means in org buffer

in emacs org mode, I saw #+ prefix many times. I think this is like option but I don't know what it is. I have tried to know them, I have been googling, but I couldn't find any good document. anybody know these symbol? is this org macros?
It is just plain text that you write in an org mode file. It may have two different functions:
1.- If you write it at the beginning of the file, it serves as a metadata descriptor or to include options for the file [1]. For example:
#+TITLE: This is the title of this file
#+AUTHOR: Loretta
#+DATE: 2022-10-15
#+STARTUP: content <--- option for displaying the contents
#+OPTIONS: H:3 toc:nil \n:nil #:t ::t |:t <--- different export options
2.- To create a block inside the file. This block may be code meant to be executed [2], a quotation, a comment, etc:
#+begin_src emacs-lisp
(+ 1 2 3 4 5 6 7)
#+end_src
#+begin_comment
Check the following paragraph. I am not really sure if it
has taken into account all the variables.
#+end_comment
[1] See https://orgmode.org/manual/In_002dbuffer-Settings.html for buffer settings and https://orgmode.org/manual/Export-Settings.html for export settings.
[2] See https://orgmode.org/manual/Literal-Examples.html for code blocks.

Why doesn't the table generated from r-exams appear in Moodle?

I have defined the variables and data necessary to generate a frequency table in RMarkdown, to export it to Moodle, using R-exams:
xsubio<-c(sample(5:35,5))
xsubi<-sort(xsubio)
frecuencias<-sample(0:30 ,5)
tabla<-data.frame(xsubi,frecuencias)
colnames(tabla)<-c(" Edades" ,"Personas")
Through chunk I generate the table:
```{r, eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE}
library(exams)
print(xtable(tabla), include.rownames=FALSE)
```
When exporting to PDF, using exams2pdf, the table is displayed perfectly:
But, when exporting to Moodle, using exams2moodle, the table disappears:
What do I have to do to make it appear?
Problem: You have an exercise with formatting in Markdown and use xtable() to insert a table in LaTeX. This mixture of Markdown+LaTeX is no problem when converting the exercise to PDF. Internally, this preserves the LaTeX table and just converts the Markdown parts to LaTeX as well before rendering the LaTeX to PDF. However, the same is not possible automatically when converting the Markdown exercise to HTML for Moodle. The converter then does not separate the LaTeX part automatically to convert it to HTML.
In short: The different markups must be sufficiently in sync. Markdown+LaTeX markup for PDF output works and Markdown+HTML for HTML output works. But what would be even better is to have the markup fully in sync, i.e., LaTeX+LaTeX or Markdown+Markdown. Such exercises can then be rendered to either PDF or HTML automatically.
Possible solutions:
Markdown+Markdown: Markdown table markup in R/Markdown (Rmd) exercise
I would recommend that you simply produce tables in Markdown via knitr::kable() rather than xtable::xtable(). Thus, the code chunk for the table simply becomes
knitr::kable(tabla, format = "markdown")
and then the tables are rendered correctly in both PDF and HTML-based formats like Moodle.
LaTeX+LaTeX: LaTeX table markup in R/LaTeX (Rnw) exercise
In case you prefer to keep on generating LaTeX tables with xtable() another route would be to change the markup of the R/exams exercise to Rnw (R/LaTeX). See the First steps tutorial on the R/exams web page for how to do that.
Markdown+Adaptive: Choose the right table markup depending on the output format
Using the match_exams_call() function you can determine in a running exercise which exams2xyz() interface is being used to process it. So you could do
type <- if(match_exams_call() %in% c("exams2pdf", "exams2nops")) "latex" else "html"
print(xtable(tabla), type = type, ...)
Then either LaTeX or HTML format is produced by xtable().
All three solutions work in principle but I think Solution 1 (kable()) is the best and most robust in your case.
Bonus remark: If you are using R/exams >= 2.4-0 you can use exams2moodle(...,table = TRUE) to get nicer table formatting in Moodle.
I use the following code snippet (with type = "latex"), when I require a PDF output:
```{r, eval=TRUE, only.contents=TRUE, echo=FALSE, results='asis',
warning=FALSE, message=FALSE, error=FALSE, comment=FALSE}
library(exams)
library(xtable)
print(xtable(tabla), include.rownames=FALSE, type = "latex", comment=F)
```
And, when I require outputs for Moodle, I make use of the following code snippet (with type = "html"):
```{r, eval=TRUE, only.contents=TRUE, echo=FALSE, results='asis',
warning=FALSE, message=FALSE, error=FALSE, comment=FALSE}
library(exams)
library(xtable)
print(xtable(tabla), include.rownames=FALSE, type = "html",
comment=F)
```
Results in PDF:
Results for Moodle:

Page numbers in scribble/acmart

I'm creating a pdf with the scribble/acmart language. How can I add page numbers to my document?
Make a LaTeX file with the line \settopmatter{printfolios=true}
If the file is named texstyle.tex, invoke Scribble with the command:
scribble ++style texstyle.tex --pdf FILE.scrbl
The rendered FILE.pdf should have line numbers.
(If you already had a ++style file, just add the \settopmatter line to that.)
The solution Ben gave is one way. But you can actually do this without modifying your texstyle.tex file.
If you add the following lines to your document, the appropriate topmatter will be added to your pdf file:
#para[#:style 'pretitle]{
#elem[#:style "settopmatter"]{
printfolios=true}}
You can see it doing this by running:
> scribble --latex myfile.scrbl
If you do this, you will notice the following line in your pdf file:
\settopmatter{printfolios=true}\titleAndVersionAndAuthors{Hello}{6.9.0.4}{\SNumberOfAuthors{1}\SAuthor{World}}
(Where Hello and World is the name and author of your paper, and the \title... macro runs \maketitle.)
This works because the 'pretitle style (when given to a paragraph), pulls its entire body above the title.
And whenever a string is given as the style for an element, it maps to the a latex command.
That is, this scribble code:
#elem[#:style "mycommand"]{Thebody}
Maps to:
\mycommand{Thebody}
The result of composing these two forms together is to drag this to the top of the file.
And because you've done this in scribble rather than latex, you can use Racket's semantics to add page numbers. For example, if you use your own #lang, you can now have the language decide whether or not you want pages.

Writing to complex PDF's in MATLAB

I'm trying to write a MATLAB function that processes a file and writes a report on that file. The report will contain numbers, strings, tables, and images.
After looking at MATLAB's documentation, I can only find functions that save individual items to a file. For example, print saves a plot, write saves a table, etc. How do I create a single file that contains many of these items (e.g. a PDF with images, tables, and text)?
You can use print with the -append option to write multiple pages to a PostScript file in sequence, and then convert the ps to pdf. Using Matlab's handle graphics system, it is possible (if tedious) to design each print page in detail, arrange elements, etc.
However, if your document is going to be really complex, I think it would be better to generate the pdf in another way. One approach would be to write LaTeX code using lots of fprintfs and compile the file using pdflatex.
Btw., I'm not aware of a Matlab function write that generates a pdf.

Processing text inside variable before writing it into file

I'm using Perl WWW::Mechanize package in order to fetch and process data from some websites. Usually my way of action is as follows:
Fetch a webpage
$mech->get("$url");
Save the webpage contents in a variable (BTW, I'm not sure if it's the right way to save this amount of text inside a scalar which, as far as I know, supposed to be used for a single value)
my $list = $mech->content();
Use a subroutine that I've created to write the contents of the variable to a text file. (The writetoFile subroutine includes few more features, like path and existing file validations..)
writeToFile("$filename.tmp","$path",$list);
Processing the text in a file created in the previous step by creating an additional file and save the processed content there (Then deleting the initial temporary file).
What I wonder about, is whether it is possible to perform the processing before storing the text in a file, directly inside the $list variable? The whole process is working as expected but I don't really like the logic behind it and it seems a bit inefficient as well, since I have to rewrite the same file multiple times.
EDIT:
Just to give a bit more information about what I'm actually after when I process the variable contents. So the data I fetch from the website in this case is actually a list of items separated by a blank line and the first line is irrelevant to me. So what I'm doing while processing this data is 2 things:
Remove the empty (CRLF) lines
Remove the first line if it includes a particular text.
Ideally I want to save the processed list (no blank spaces and first line removed) in a file without creating any additional files on the way. In order to save the file I would like to use the writeToFile sub (I wrote) since it also performs validation on whether such file already exists (If a file will be saved before final processing - the writeToFile will always rewrite the existing file).
Hope it makes sense.
You're looking for split. The pattern depends: use (?<=\n) split at a new line character and keep it. If that doesn't matter, use \R to include all sort of line breaks.
foreach my $line (split qr/\R/, $mech->content) {
…
}
Now the obligatory HTML-parsing-with-regex admonishment: if you get HTML source with Mechanize, parsing it line-by-line does not make much sense. You probably want to process the HTML-stripped text version of the document instead, or pass the HTML source to a parser such as Web::Query to declaratively get at the pieces you need.