knitr: Knitting to HTML - add a prefix to figure and table numbers - html

R Markdown with knitr can add a prefix (usually a letter) to figure and table numbers when knitting/compling to PDF. When knitting to PDF, the following code will do the job:
```{=tex}
\renewcommand{\thetable}{S\arabic{table}}
\renewcommand{\thefigure}{S\arabic{figure}}
```
The result will be
Figure S1.1.
rather than
Figure 1.1
Is there a way to achieve the same when knitting to HTML?

Related

Multiple bibliographies in HTML, using keywords - is it possible?

I am making a CV in R Markdown, and I have the following code that uses Latex to render multiple bibliography sections based on the keywords={} argument in the bibtex reference:
---
header-includes: \usepackage{tikz,xcolor,listings,CormorantGaramond,fontawesome,academicons,mfirstuc,fancyhdr,multicol}
output:
bookdown::pdf_document2:
citation_package: biblatex
toc: no
latex_engine: lualatex
biblio-style: apa
bibliography: MyPubs.bib
---
\underline{\textbf{Publications}}
\begin{refsection}
\setlength\bibitemsep{\baselineskip}
\nocite{*}
\noindent\printbibliography[heading=none, keyword = mypapers]
\end{refsection}
\underline{\textbf{Presentations}}
\begin{refsection}
\setlength\bibitemsep{\baselineskip}
\nocite{*}
\printbibliography[heading=none, keyword = mypresentations]
\end{refsection}
The resulting behavior is as follows: R Markdown renders a PDF with two separate bibliographies: a Publication section that pulls references from my .bib file that have the mypublications keywords, and a separate section for Presentations (from refs with mypresentations keywords).
The output is PDF, but how do you duplicate this behavior in HTML?
I'm having a hard time finding info on this; is it possible? Thank you in advance for any guidance you can provide.

In RMarkdown, how to include latex code when using an HTML or Word output

I have a very basic question. I did not find an answer anywhere. My fear is that the answer is simply no.
Using R Markdown, I would like to use LaTex code with an HTML or Word output.
A very simple MVE :
I would like to execute the following code :
This text has been written with both \LaTeX{} and Rmd codes.
With a LaTex/PDF output, no problem. I get 'This text has been written with both Latex and Rmd codes.' (with the nice LaTeX logo !)
With an HTML or Word output, I get 'This text has been written with both and Rmd codes.'
So the LateX code disappears with non-LateX output.
Do you know if there is a way to fix this?
Thanks in advance,
Eric
EDIT: Here is another example using a simple table
\begin{table}[h]
\centering
\caption{A simple table}
\begin{tabular}{|l|c|c|}
\hline
a & b & c \\
d & e & f \\
\hline
\end{tabular}
\end{table}
It will work if you put the LaTeX output between dollar signs. In HTML, it will not work if you are offline though! :
This text has been written with both $\LaTeX{}$ and Rmd codes.
In R Markdown, Pandoc converts LaTeX into Office Math Markup (OMML). So you can get LaTeX output in Word, but you won't get the logo, as far as I know.
For LaTeX output in Word, remove the backslash and add the dollar signs, like this:
This text has been written with both $LaTeX{}$ and Rmd codes.
Or leave a space between the backslash and the output:
This text has been written with both $\ LaTeX{}$ and Rmd codes
Here is a snapshot from a document generated in Word:

Word html format: insert a custom TOC via field code

I am generating Word docs from html. Basically, I build a file with html and save it as a .doc. Then I open it in Word and apply a template. All good so far.
I would like to automatically generate a custom TOC via the HTML ie when I am building the document. I need to insert a field code to do that, in the same way I do to add page numbering via the HML. eg:
<span style="mso-field-code: PAGE " class="page-field"></span>
If I save my html doc as docx and apply a template, I can make a TOC based in the styles in the way one would normally create a TOC in Word. I customised the TOC so the Title style is the top level followed by H1, H2 then H3. If I then toggle the field code on the TOC, the field code looks like this:
{ TOC \t "Heading 1,2,Heading 2,3,Heading 3,4,Title,1" }
Now, I can add HTML like this to insert the TOC:
<div style="mso-field-code: TOC " class="toc-field">TOC goes HERE</div>
When I do that, if I right click the text "TOC goes HERE" I get the option to "Update field" and if I do that a TOC is generated using the default H1,H2,H3 tags.
But, what I can't work out is how to include the
\t "Heading 1,2,Heading 2,3,Heading 3,4,Title,1"
part so my custom style sequence is applied. I have tried all sorts of combinations and it seems that adding anything after TOC causes Word to not make a field code.
Does anyone have any suggestions?
Update:
Based on the essential help from #slightlysnarky below, I thought I would summarise the outcome here because the information I needed was in a Microsoft chm file that was taken down many years ago. If you read the following extract from that help manual and compare it to the solution below you will see how this all works.
Word marks and stores information for simple fields by means of the Span element with the mso-field-code style. The mso-field-code value represents the string value of the field code. Formatting in the original field code might be lost when saving as HTML if only the string value of the code is necessary for its calculation.
Word has a different way of storing field information to HTML for more complex fields, such as ones that have formatted text or long values. Word marks these fields with so the data is not displayed in the browser. Word uses the Span element with the mso-element: field-begin, mso-element: field-separator, and mso-element: field-end attributes to contain the three respective parts of the field code: the field start, the separator between field code and field results, and the field end. Whenever possible, Word will save the field to HTML in the method that uses the least file space.
So, basically, add tags as shown below to your HTML at the point you wish the TOC to appear.
:-)
Word recognises a "complex field format" in HTML, along the same lines as it does in the Office Open XML format. So you can use
<span style='mso-element:field-begin'></span>TOC \t "Heading 1,2,Heading 2,3,Heading 3,4,Title,1"
<span style='mso-element:field-separator'></span>This text will show but the user will need to update the field
<span style='mso-element:field-end'></span>
This construct is outlined in a Microsoft document called "Microsoft Office HTML and XML Reference". It's a Windows .exe that unpacks to a .chm Help file. You can get it here
The info. on encoding fields is in Getting Started with Microsoft Office 2000 HTML and XML->Microsoft Word->Fields
There may be a later version but that's the only one I could find.

Get code output nicer in R Markdown Knit? Breaking into two parts

I am knitting R Markdown (Rmd) to HTML, but some code outputs are not pretty, e.g., like the picture attached.console
How can I make output in one part if possible? Thanks.

ruby tags for Sphinx/rst

I create HTML documents from a rst-formated text, with the help of Sphinx. I need to display some Japanese words with furiganas (=small characters above the words), something like that :
I'd like to produce HTML displaying furiganas thanks to the < ruby > tag.
I can't figure out how to get this result. I tried to:
insert raw HTML code with the .. raw:: html directive but it breaks my line into several paragraphs.
use the :superscript: directive but the text in furigana is written beside the text, not above.
use the :role: directive to create a link between the text and a CSS class of my own. But the :role: directive can only be applied to a segment of text, not to TWO segments as required by the furiganas (=text + text above it).
Any idea to help me ?
As long as I know, there's no simple way to get the expected result.
For a specific project, I choosed not to generate the furiganas with the help of Sphinx but to modify the .html files afterwards. See the add_ons/add_furiganas.py script and the result here. Yes, it's a quick-and-dirty trick :(