Tables in R-markdown not shown in Moodle - exams2moodle - latex

I have a serious problem with rendering tables in Moodle once I upload the Moodle XML file generated by R-exams package to Moodle. I am using exercise files in .Rmd format handed to me from a previous professor's assistant. These .Rmd files contain lots of tables using Latex language. Once I upload the Moodle XML file, all of these tables are simply missing. I have tried to look up a solution, but could not find anything helpful on this.
Interestingly, these tables are rendered properly into PDF when using "exams2pdf" command in R-studio. However, when using "exams2moodle", these tables are simply not shown in Moodle.
I am showing an example of the table in one of these .Rmd files below:
I would appreciate if anyone could provide any guidance, as I got really stuck with this issue.

TL;DR: The problem is that you mix an exercise in Markdown format with LaTeX content. This only works if the rendering goes via LaTeX (see below for details). Instead you could convert your question from R/Markdown (.Rmd) to R/LaTeX (.Rnw) - or convert the formatting of the table from LaTeX to Markdown. Both would be feasible in this case. If you need help with this, please share (a link to) the code for the exercise and not just a screenshot of parts of it.
Hint: If you're using at least version 2.4-0 of exams, exams2moodle() gained an argument table where you can set a class for formatting the table a bit more nicely, e.g., table = "table_shade" or "table_rule" or "table_grid".
Details: You have a question in R/Markdown format which contains a table in LaTeX format. When you use exams2pdf() this works because rendering works in the following steps:
knit R/Markdown to Markdown (preserving the LaTeX table).
Convert Markdown to LaTeX via pandoc (preserving the LaTeX table).
Render LaTeX (including the table) to PDF using texi2pdf or latexmk.
Thus, it is crucial that you do a rendering step from LaTeX to the output format. This is not the case though for exams2moodle() or other engines based on HTML output. The steps are just:
knit R/Markdown to Markdown (preserving the LaTeX table).
Convert Markdown to HTML via pandoc (which does not know how to render the LaTeX table and hence drops it).
Embed HTML in XML for Moodle.
Thus, the problem is that there is no rendering or transformation step from LaTeX in this case. The same would be true if you had R/Markdown exercises with HTML content. These could be rendered with exams2moodle() because the HTML would just need to be preserved - but not with exams2pdf() because the HTML would not be converted to LaTeX.

Related

Rmarkdown journal template rendering inverted exclamation mark (¡) in table, while .tex has correct less than symbol "<"

I'm writing a paper using the ACM manuscript latex template in Rmarkdown. I have a table including p values, which have been formatted with scales::pvalue() to get the < symbols in there instead of long almost-zero numbers.
The problem I'm having is that even though in the .tex file that gets produced from my Rmarkdown has the correct symbol, it still gets rendered as (¡) on the pdf.
I'm aware this is probably related to https://tex.stackexchange.com/questions/2369/why-do-the-less-than-symbol-and-the-greater-than-symbol-appear-wrong-as, but as the class I'm using as part of the template already uses T1 font encoding, I'm at a loss for how to solve this.
I am also unable to recreate the problem in overleaf. Pasting what the latex script for the table into https://www.overleaf.com/latex/templates/acm-conference-proceedings-primary-article-template/wbvnghjbzwpc works as expected.
Turns out that there was a TinyTex package missing - I compiled with MikTex and it worked, so then I ran tinytex::install_yuhui_pkgs() and it finally worked.
Thanks #samcarter_is_at_topanswers.xyz for your help.

Markdown equivalent to LaTeX chapters and parts?

I am thinking about the relationships between markdown and LaTeX, on of (among many) of the differences between the two is sectioning. Markdown uses hashtags as equivalents to HTML header tags, similarly LaTeX has \section{}, \subsection{}, etc. for sectioning. However, LaTeX also has \chapter{} and \part{}, but I can't think about what these would equate to in Markdown. Ideas?
One thought is to turn separate parts and chapters into separate Markdown documents, but that doesn't really feel right.
One of Markdown's design goals is to be very simple:
Markdown is not a replacement for HTML, or even close to it. Its syntax is very small, corresponding only to a very small subset of HTML tags.
Since LaTeX is more expressive than HTML, Markdown syntax corresponds to an even smaller subset of LaTeX than of HTML.
For any markup that is not covered by Markdown’s syntax, you simply use HTML itself.
If you are targeting HTML, you should be looking for HTML that corresponds to a chapter or a part. One option is the <article> tag:
The <article> HTML element represents a self-contained composition in a document, page, application, or site, which is intended to be independently distributable or reusable (e.g., in syndication). Examples include: a forum post, a magazine or newspaper article, or a blog entry, a product card, a user-submitted comment, an interactive widget or gadget, or any other independent item of content.
Another option is <section>:
The <section> HTML element represents a generic standalone section of a document, which doesn't have a more specific semantic element to represent it. Sections should always have a heading, with very few exceptions.
If you are targeting LaTeX (or PDF via LaTeX) you'd want to similarly use LaTeX directly. Exactly how you do that depends on the tool you are using. Pandoc, for example, supports raw LaTeX via the raw_tex extension.

Rmarkdown with pandoc templates, apply lua filter on intermediate .tex

I'm trying to use lua filters to capture images in my manuscript and list their caption in a special \section at the end of it.
I am working on a rmarkdown document that itself uses a .tex template.
I wasn't able to get anywhere, so I run a very simple filter:
function Header (head) print(pandoc.utils.stringify(head)) end
and noticed that just the headers in the markdown were recognized, not the ones in the ones in the template.
The only way I found to have lua filters recognize the elements in the template was to rerun the produced .tex file with pandoc:
pandoc -f latex -t latex -o test2.tex --lua-filter=my_filters.lua test.tex
but that removed all latex formatting and structure content outside the body, e.g., \documentclass, \usepackage and other custom commands. So it's a no go.
So the question is, is there a way to force lua filter to be applied after the integration of a latex template when knitting a rmarkdown document?
There might be a way, but it most likely won't do what you need.
When pandoc reads a document, it parses it and converts it into it's internal data structure. That internal structure can then be modified with a filter. LaTeX is a very expressive and complex document format, and any conversion from LaTeX into pandoc's internal format will result in a loss of (layout) information. That's good enough in most cases, but would be a problem in your case.
There are two possible ways to do this: one is to post-process the output, which is probably tedious and error-prone. The other is to find a way to generate the desired output, e.g. via a pandoc filter, without adding it to the template first.
I believe your other question is the right way to go.

R --markdown to latex - tables do not show up

I am using Knitr in Rstudio, to generate markdown files. I display the tables via xtable package and it shows up nicely in html file. However, when I converd .md to latex via pandoc - the latex file does not contain the tables as it is supposed to be, but only the values in table without any command.
Markdown - Knitr input
In order to give a better idea, the following table provides a sample of
data rows:
```{r table, results='asis', echo=FALSE}
r = read.table("C:/aR_files/data.txt",sep=",", header=TRUE,as.is=TRUE)
r$X = NULL;
print(xtable(r), type='html')
```
Latex
In order to give a better idea, the following table provides a sample of
data rows:
Row1
Row2
Val1
Val1
I thought I may be missing a latex package, so I downloaded ctable.sty, but still I get the same output. Any ideas appreciated, thanks!
I use a very similar workflow to yours and your best bet is to abandon the often clunky xtable package and use the pander package to print your tables. You can wrap any object that you might want to display as a table in the generic pander() function. This is a wrapper for the pandoc.table() function which has several options. If you specify the option style = "XXX" you can achieve what you are asking about here. There are 4 different styles you can choose from; "multiline" (the default), "grid", "simple", or "rmarkdown". I frequently knit rmarkdown documents from within Rstudio and then convert them to Word documents using the pander package:
library(pander)
Pandoc.convert("C:/Users/BlahBlahBlah/Document.md", format="docx")
All of the 4 table styles get turned into table objects upon conversion to .docx format, but only one table style looks right in the .docx document and the .html file that results from the initial "knit". That style is "rmarkdown". You can implement this 2 ways. One would be:
```{r table, results='asis'}
pandoc.table(myTable, style = "rmarkdown")
```
I prefer to set the table style globally at the beginning of my document however, ensuring that all my tables have the same formatting and also allowing me to use the more succinct pander(x) instead of the more verbose pandoc.table(x, style = "someStyle"):
```{r table, results='asis'}
panderOptions("table.style", "rmarkdown")
pander(myTable)
```
There are some side effects of using the rmarkdown style however. Mainly, it does not support newline characters within cells, so buyer beware. I experimented with the different styles and eventually decided that I liked the default style of "multiline" because of it's flexibility with line breaks within cells, even though the .html files I generate look silly. This doesn't bother me though, as I really only use the .docx files that I convert from the .md files. I wrote a blog post about making nice tables that you might find useful. It weighs the pros and cons of several methods including xtable() and several pander() scenarios.

Including full LaTeX documents within others

I'm currently finishing off my dissertation, and would like to be able to include some documents within my LaTeX document.
The files I'd like to include are weekly reports done in LaTeX to my supervisor. Obviously all documents are page numbered seperately.
I would like them to be included in the final document.
I could concatenate all the final PDFs using GhostScript or some other tool, but I would like to have consistent numbering throughout the document.
I have tried including the LaTeX from each document in the main document, but the preamble etc causes problems and the small title I have in each report takes a whole page...
In summary, I'm looking for a way of including a number of 1 or 2 page self-complete LaTeX files in a large report, keeping their original layouts, but changing the page numbering.
For a possible solution of \input-ing the original LaTeX files while skipping their preamble, the newclude package might help.
Otherwise, you can use pdfpages for inserting pre-existing PDFs into your dissertation. I seem to recall that it has a feature of "suppressing" the original page numbers by covering them up with white boxes.
The suggestion from #Will Robertson works great. I'd just like to add an example for all lazy people:
\usepackage{pdfpages}
...
% Insert _all_ pages from some_pdf.pdf:
\includepdf[pages=-]{some_pdf} % the .pdf extension may be omitted
From the documentation of the package:
To include a specific range of pages, you could do pages={4-9}. If start is omitted, it defaults to the first page, if end is omitted, it defaults to the last page.
To include it in landscape mode, do landscape=true
Maintaining the original formatting per document will be difficult if they're using different formats. For example, concatenating different document classes will be near impossible.
I would suggest you go with the GhostScript solution with a slight twist. Latex allows you to set the starting page number using \setcounter{page}{13} for example. If you can find an application that can count the pages of a PDF document (pdfinfo in the pdfjam Ubuntu package is one example), then you can do the following:
Compile the next document to PDF
Concatenate the latest PDF with the current full PDF
Find the page count of the full PDF
Use sed to pluck in a \setcounter{page}{N} command into the next latex file
Go back to the beginning
If you need to do any other processing, again use sed. You should (assuming you fix the infinite loop in the above algorithm ;-) ) end up with a final PDF document with all original PDFs concatenated and continuous line numbers.
Have a look a the combine package, which seems to be exactly what you're searching for.
Since it merges documents at the source level, I guess the page numbers will be correct.

Resources