I am creating R notebooks that contain equations. I am using RStudio 1.2.5033 on Windows 10, R 3.5.1, and rmarkdown 2.1. When my R notebooks are rendered as HTML, MathJax (v2.7.2) uses the "HTML-CSS" output processor to render the equations. But I think that the output from the "CommonHTML" output processor looks better. So I want to include a directive, in my R notebooks, that forces MathJax to use the CommonHTML output processor. How may I do this?
If I were rendering an ordinary R Markdown document with output format html_document, I could solve the problem via the mathjax option in my YAML header. For example, when the following file is rendered to HTML, MathJax will use the CommonHTML output processor:
---
title: "Trouble with MathJax"
output:
html_document:
mathjax: "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_CHTML.js"
self_contained: false
---
\begin{equation}
R_3 = \alpha
\end{equation}
But this solution doesn't work well when I change the output format from html_document to html_notebook. In that case, I get output that looks like this:
The equation is rendered with CommonHTML, but there is a lot of cruft at the top of the page (note the four bullet points), and the default R Notebook CSS doesn't seem to be implemented.
The problem seems to be general to rendering R notebooks with self_contained: FALSE, as suggested in R notebooks don't render properly when "self_contained" is FALSE because the "files" directory is deleted after rendering. But I can't see a good workaround for that problem.
Dead Ends
The MathJax documentation seems to indicate that I can specify the output processor by adding the jax array in a call to MathJax.Hub.Config(). But when I've done that, my equations are still displayed via the HTML-CSS output processor. Here is a minimal example of an R Markdown document that exhibits the problem:
---
title: 'Trouble with MathJax'
output: html_notebook
---
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
jax: ["input/TeX", "output/CommonHTML"],
});
</script>
\begin{equation}
R_3 = \alpha
\end{equation}
The call to MathJax.Hub.Config() seems to do nothing here. In both Chrome and Edge, the equation is rendered via HTML-CSS, not CommonHTML. How can I change the rendering to Common HTML?
Related Posts
One year-old post, Is there a way in markdown to override default mathjax renderer?, is about Jupyter notebooks, but it's relevant. It hasn't received an answer.
Adapting the script in this post from the MathJax Google Group –– mainly by changing "HTML-CSS" to "CommonHTML" –– doesn't seem to have any effect.
The solution is simply to omit the self_contained line in the YAML header or, equivalently, to set self_contained to true. Here is a minimal example of an R notebook for which the user has chosen the mathjax renderer:
---
title: "Self-contained notebook with non-default Mathjax config"
output:
html_notebook:
mathjax: "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_CHTML.js"
---
$R_3 = 2$.
When the file is rendered to HTML, the equation is displayed with CommonHTML, not with HTML-CSS. And the Mathjax script is contained within the "nb.html" file that is produced.
I was surprised that this works, because the documentation for rmarkdown::html_document() says that "even for self contained documents MathJax is still loaded externally (this is necessary because of its size)." But Section 3.1.8 of the R Markdown book indicates that the restriction applies only when Mathjax is loaded from a local file. So perhaps it shouldn't be a surprise.
Side note: the default Mathjax configuration used by the rmarkdown package is given by rmarkdown:::mathjax_config(). As of rmarkdown v2.1, the function returns "MathJax.js?config=TeX-AMS-MML_HTMLorMML".
Related
I'm using pandoc to convert LaTeX to HTML. However, I have a lua script included in the latex file (which pulls some data from a JSON file and formats the data to LaTeX). As I convert to HTML, the script is not executed but appears as lua in the output.
Is there either a way to get a pure latex output for the conversion or to run the script during the conversion?
Unfortunately, the answer is "yes, but actually: no".
What I mean is that you can run the Lua code, but it is very likely to contain code which is luatex specific and will not work in pandoc.
Let's look at an example:
\documentclass{article}
\usepackage{luacode}
\begin{document}
You are runnig:
\begin{luacode}
tex.print(_VERSION)
\end{luacode}
\end{document}
The script, when run through lualatex, will report the Lua version used to execute the code (currently "Lua 5.3"). The tex.print command is provided by lualatex.
To see how pandoc handles this, we can convert it into pandoc's internal format with pandoc --to=native. Pandoc doesn't know the luacode environment, so it treats it like normal text.
[Para [Str "You",Space,Str "are",Space,Str "runnig:"]
,Div ("",["luacode"],[])
[Para [Str "tex.print(_VERSION)"]]]
We see that the block becomes a div with class luacode. It's possible to run a Lua filter and execute it's content:
-- file: run-luacode.lua
function Div(d)
local code = pandoc.utils.stringify(d)
load(code)()
end
Using this with
pandoc my-test.latex --to=html --lua-filter=run-luacode.lua
Will lead to an error, because tex.print is undefined in pandoc's Lua.
Error running filter run-luacode.lua:
[string "tex.print(_VERSION)"]:1: attempt to index a nil value (global 'tex')
stack traceback:
[string "tex.print(_VERSION)"]:1: in main chunk
run-luacode.lua:3: in function 'Div'
Of course, we could define tex.print in the pandoc filter. E.g., setting
tex = {['print'] = print}
will at least print the result to the console. You could devise a mechanism that actually converts it to pandoc's internal document format. See https://pandoc.org/lua-filters.html for details.
It might also be beneficial to call pandoc with --from=latex+raw_tex, which makes pandoc keep the unknown luacode environment verbatim in a RawBlock element. This can be easier to process in the filter.
I have an SW manual that I'm documenting with Doxygen+Markdown, the output being a PDF file. The default Doxygen LaTeX header uses \setlength{\parindent}{0cm}. I tried to change that to \setlength{\parindent}{20pt}, but the output PDF still doesn't have indented paragraphs. I was wondering if I need to change the doxyparagraph style.
Thanks!
EDIT:
I'm using text and other content from Markdown files. Looking at a TeX file from one of these MD files, I can't see anything special(environment, etc) aboout how thery're being defined. In a MD file I have:
### Boot Initialization {#prom_rom_boot_initialization}
The boot ROM performs basic system initialization required for booting the
system. The system initialization consists of the following:
This generates:
\hypertarget{program_rom_chapter_prom_rom_boot_initialization}{}\doxysubsubsection{Boot Initialization}\label{program_rom_chapter_prom_rom_cfx_boot_initialization}
The boot R\+OM performs basic system initialization required for booting the system. The system initialization consists of the following\+:
The doxysubsection is defined in the default Doxygen LaTeX style as:
\newcommand\doxysubsection{\#startsection{subsection}{2}{\z#}%
{-3.25ex\#plus -1ex \#minus -.2ex}%
{1.5ex \#plus .2ex}%
{\raggedright\normalfont\large\bfseries}}
So I tried to do the following in my header:
\renewcommand{\paragraph}{%
\#startsection{paragraph}{4}{0ex}{-1.0ex}{1.0ex}{%
\setlength{\parindent}{20pt}%
\normalfont\normalsize\bfseries\SS#parafont%
}%
}
\renewcommand{\subparagraph}{%
\#startsection{subparagraph}{5}{0ex}{-1.0ex}{1.0ex}{%
\setlength{\parindent}{20pt}%
\normalfont\normalsize\bfseries\SS#subparafont%
}%
}
\makeatother
That didn't work.
So I tried setting parindent in my document:
\documentclass[parindent=20pt,10pt,twoside,openany]{book}
And use the ragged2e package. I've added
\usepackage[document]{ragged2e}
\setlength{\RaggedRightParindent}{\parindent}
To my header. And set \parindent=20pt instead of \setlength{\parindent}{0cm}.
I still don't get indented paragraphs.
Is it possible to process the text markdown for the listing captions in pandoc?
Pandoc works well for figure captions and translates the emphasis and inline code, but apparently fails to do so in listing captions.
For example, in the code below, I have some formatting and reference in the captions for the second listing and the figure.
However, only the formatting in the figure is correctly transformed into latex.
The listing caption is left as it is, which breaks the tex processer later in the pipeline.
Is it possible to process the listing captions just like the figure captions?
```{#lst:first .C caption="Hi"}
int hi() {
return ((int)'h'<<8) | 'i';
}
```
```{#lst:second .C caption="Code *using* the function `hi` from [#lst:first]"}
x = hi();
```
![Picture *with* `inline code` and reference [#lst:first]](picture.png)
pandoc example.so.md -o example.so.tex --listings --filter=pandoc-crossref
Produces:
\begin{codelisting}
\caption{Hi}
\begin{lstlisting}[language=C, caption=Hi, label=lst:first]
int hi() {
return ((int)'h'<<8) | 'i';
}
\end{lstlisting}
\end{codelisting}
\begin{codelisting}
\caption{Code *using* the function `hi` from {[}#lst:first{]}}
\begin{lstlisting}[language=C, caption={Code *using* the function `hi` from [#lst:first]}, label=lst:second]
x = hi();
\end{lstlisting}
\end{codelisting}
\begin{figure}
\centering
\includegraphics{picture.png}
\caption{Picture \emph{with} \passthrough{\lstinline!inline code!} and
reference lst.~\ref{lst:first}}
\end{figure}
I use pandoc 2.7.3 and pandoc-crossref v0.3.4.1
P.S. as https://github.com/jgm/pandoc/issues/673 suggests, there may still be no native support for this. Is there a workaround?
K4zuki's response on the pandoc google group worked for me.(https://groups.google.com/forum/#!msg/pandoc-discuss/DItTuL5S1EM/L1Ou25gTCAAJ)
Use the option for pandoc-crossref explained here: http://lierdakil.github.io/pandoc-crossref/#table-style-captions
add codeBlockCaptions: true in your metadata block or run pandoc with -M codeBlockCaptions=true
This worked:
Listing: Code *using* the function `hi` from [#lst:first]
```{#lst:second .C}
x = hi();
```
This is my Rmarkdown code:
---
title: "My Title"
header-includes:
- \usepackage{calligra}
- \usepackage[T1]{fontenc}
output:
pdf_document:
latex_engine: xelatex
---
# Section 1
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
I was studying the best way to change fonts in Rmarkdown. In texlive the installed calligra package appears and calligra-type1 also.
When I run the script with the calligra package it generates the pdf but the font does not match the calligra font.
And when I run the script for calligra-type1 the pdf is not even generated.
Any help, guys?
The calligra.sty does not set-up the Calligra as default font, but you can do so by adding
\renewcommand{\rmdefault}{calligra}
to header-includes.
BTW, there is no need to call fontenc.sty, since calligra.sty does so already. In addition, using xelatex is not necessary here.
I'd like to use pandoc lua filter when I convert multiple markdown files from markdown to pdf. I'd like the titles of individual markdown files to be used as chapters (first-level headers).
I learned the existing examples, and I think this one is close to what I need - basically I need to add pandoc.Header(1, doc.meta.title) to all my markdown files, however I'm struggling to write the lua filter and make it work.
I think this question is doing a similar action pandoc filter in lua and walk_block
The pandoc command:
pandoc -N --lua-filter add-title.lua blog/*.md --pdf-engine=xelatex --toc -s -o my_book.pdf
The add-title.lua (this is just wrong, no exceptions but nothing happens to the output):
function add_header (header)
return {
{Header = pandoc.Header(1, meta.title)}}
end
Input files:
1.md
---
title: Topic1
---
## Sample Header from file 1.md
text text text
2.md
---
title: Topic2
---
## Sample Header from file 2.md
text text text
Expected output equivalent to this markdown (but my final format is pdf)
---
title: Title from pandoc latex variable
---
# Topic1
## Sample Header from file 1.md
text text text
# Topic2
## Sample Header from file 2.md
text text text
I think the key problem is that the lua filters only run once the full set of documents have been parsed into a single AST. So the individual files are effectively concatenated prior to parsing to create a single document with a single set of metadata. The individual title settings in the yaml metadata blocks are being overridden before the filter has a chance to run. Assuming that you need to get the heading from each separate metadata block (and can't just put the header in directly) this means that you cannot let pandoc join the files. You will need to read and parse each file separately. Fortunately this is pretty easy with filters.
The first step is to make a single reference file that contains links to all of the other files.
---
title: Combined title
---
![First file](1.md){.markdown}
![Second file](2.md){.markdown}
Note that the links are specified using images with a special class .markdown. You could use some other method, but images are convenient because they support attributes and are easy to recognise.
Now we just need a filter that will replace these images with the parsed elements from the linked markdown file. We can do this by opening the files from lua, and parsing them as complete documents with pandoc.read (see https://www.pandoc.org/lua-filters.html#module-pandoc). Once we have the documents we can read the title from the metadata and insert the new header. Note that we apply the filter to a Para element rather than the Image itself. This is because pandoc separates Block elements from Inline elements, and the return value of the filter must be of the same type. An Image filter cannot return the list of blocks parsed from the file but a Para can.
So here is the resulting code.
function Para(elem)
if #elem.content == 1 and elem.content[1].t == "Image" then
local img = elem.content[1]
if img.classes:find('markdown',1) then
local f = io.open(img.src, 'r')
local doc = pandoc.read(f:read('*a'))
f:close()
-- now we need to create a header from the metadata
local title=pandoc.utils.stringify(doc.meta.title) or "Title has not been set"
local newHeader=pandoc.Header(1, {pandoc.Str(title)})
table.insert(doc.blocks, 1, newHeader)
return doc.blocks
end
end
end
If you run it on the combined file with
pandoc -f markdown -t markdown -i combined.md -s --lua-filter addtitle.lua
you will get
---
title: Combined title
---
Topic 1
=======
Sample Header from file 1.md
----------------------------
text text text
Topic 2
=======
Sample Header from file 2.md
----------------------------
text text text
as required.
Note that any other yaml metadata in the included files is lost. You could capture anything else by taking it from the individual meta object and placing it into the global one.