html to tex conversion with Pandoc does not produce mathematical mode - latex

I'm working with a Windows machine. I use Rstudio/knitr to write and convert a (R)markdown file to an html file. The source markdown file contains some mathematics typed in the LaTeX language hence the html file contains some Mathjax.
For instance there is the following LaTeX code in the markdown file is:
$H_0:= \{|\mu_x-\mu_y|>S\}$
hence the expected output is:
This output correctly appears in the html file.
But now I try to convert this html file to a tex file:
pandoc -s myfile.html -o myfile.tex
And then I get the following code in the output tex file:
\textbackslash{}( H\_0:=
\textbackslash{}\{\textbar{}\textbackslash{}mu\_x-\textbackslash{}mu\_y\textbar{}\textgreater{}S\textbackslash{}\}
\textbackslash{}) où \textbackslash{}( S \textbackslash{})
which gives the following rendering after compiling with (pdf)latex:

Related

Can we use the bib file from web for pdf_book in Bookdown?

In Rmarkdown,
we can generate pdf file if we use the bib file from a url.
For example, we set
bibliography: [https://raw.githubusercontent.com/ChoCho66/test/main/text.bib]
In bookdown, we can also generate html book (gitbook, bs4_book)
if we use the bib file from a url.
But pdf_book doesn't work.
It has the following message.
! Undefined control sequence.
\hyper#normalise ...M{ }\catcode `\%\active \let %
\#percentchar \let \%\#per...
l.430 ...content.com/ChoCho66/test/main/text.bib}}
The tex file has wrong since the following:
\bibliography{\url{https://raw.githubusercontent.com/ChoCho66/test/main/text.bib}}
Is there any way to solve this problem?
I want a common code that can compile both html book and pdf book if we use bib url file.

Knit RMarkdown Image ! Package pdftex.def Error: File `~/folder/folder/filename.png' not found: using draft setting

I am having trouble knitting a pdf document with RMarkdown when embedding images. When using the markdown syntax below:
![Image name](~/folder/folder/filename.png)
I get the error:
Package pdftex.def Error: File `~/folder/folder/filename.png' not found: using draft setting.
I have also tried:
``` {r results = 'asis'}
knitr::include_graphics(path = "~/folder/folder/filename.png")
```
And, I get the error:
Unescaped left brace in regex is passed through in regex; marked by <-- HERE in m//\nobreakspace { <-- HERE }/folder/folder/filename/ at C:\Users\NAME~1\AppData\Roaming\TinyTeX\texmf-dist\scripts\texlive\tlmgr.pl line 1847.
Error in grep(paste0("/", x[j], "$"), l) :
invalid regular expression '/\nobreakspace {}/folder/folder/filename$', reason 'Invalid contents of {}'
Calls: ... system2_quiet -> on_error -> parse_packages -> grep
In addition: Warning message:
In grep(paste0("/", x[j], "$"), l) :
TRE pattern compilation error 'Invalid contents of {}'
Warning: LaTeX Warning: Reference `LastPage' on page 1 undefined on input line 153.
Execution halted
I have tinytex installed, but maybe I am missing another LaTeX package? Any guidance would be appreciated.
First, as #samcarter_is_at_topanswers.xyz recommended above, the tilde character cannot be interpreted from Markdown to LaTeX.
Additionally, I had my markdown file in one subfolder and my images in another subfolder. When I was knitting the markdown file, the working directory changes to the subfolder that the markdown file is in causing the file path provided for the images to no longer be understood when using the path folder/folder/filename.png. You can either move the markdown file to the main directory or set the entire file path.
If you move the markdown file to the main directory the file path can look like:
![Image name](folder/folder/filename.png)
or keep in the subfolder and use:
![Image name](entire path/folder/folder/filename.png)
The same goes for using knitr's include_graphics function.

Latex to HTML with Pandoc, how do I include the Lua script output to the conversion?

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.

\imagegraphics doesn't interpret \ escape in filename

I'm trying to insert a background image with the beamer package using the following:
\usebackgroundtemplate{%
\includegraphics[width=\paperwidth,keepaspectratio]{/path/to_file/background.png}%
}
The problem I'm having is that my background file path has an underscore in it, as I put in the example above. (This is my home directory on OS X, so I can't really change it.) When I generate this via pandoc the resultant latex file has the underscore escaped with a \, so it looks like this:
\usebackgroundtemplate{%
\includegraphics[width=\paperwidth,keepaspectratio]{/path/to\_file/background.png}%
}
but this makes it impossible for includegraphics to find the file and so it fails at that point.
If I manually remove the \ from the latex, or move the file to, say, the root directory, so that there is no underscore in the path, all is well. Here's the exact error:
LaTeX Warning: File `/Users/my\_name/Downloads/background.png' not fou
nd on input line 102.
! Package pdftex.def Error: File `/Users/my\T1\textunderscorename/Down
loads/background.png' not found: using draft setting.
So is there something I can do to tell includegraphics that the name is escaped, or to remove the escaping?
Here's the YAML:
---
name: John Doe
backgroundImage: /Users/my_name/Downloads/background.png
---
Text to add to template:
$if(backgroundImage)$
\usebackgroundtemplate{%
\includegraphics[width=\paperwidth]{$backgroundImage$}%
}
YAML values are interpreted as Markdown, unless specified otherwise. The backgroundImage is hence translated into LaTeX when used within $backgroundImage$.
The solution is to force literal interpretation of the input by telling pandoc that you know what you are doing, and that the input is already formatted as LaTeX:
---
name: John Doe
backgroundImage: '`/Users/my_name/Downloads/background.png`{=latex}'
---
This is based on the raw attribute extension, which is enabled by default for pandoc flavored Markdown.

Pandoc Error producing PDF with markdown body text and YAML metadata file

I'm running the following Pandoc 2.0.3 command on the Mac Terminal command line:
pandoc one.md "metadata.yaml" -o two.pdf
This should take the markdown file one.md and output two.pdf using the yaml file metadata.yaml, a minimal version of which is:
---
header-includes:
- \usepackage{fancyhdr}
...
This Pandoc run produces a PDF as expected for the following version of one.md:
# Report
However, it fails to produce a PDF for the following version of one.md, which contains body text:
# Report
Lorem.
The resulting error message is:
Error producing PDF.
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.65 header-includes: - \usepackage
I don't understand why including that one word creates a failure.
Your one.md likely doesn't end with a newline. Pandoc concatenates all input files, adding a single newline between files. So the resulting input will be:
# Report
Lorem.
---
header-includes:
- \usepackage{fancyhdr}
...
As a result, the opening dashes of the YAML block are interpreted as underlines for Lorem., which is then read as a second-level header. This doesn't happen if the line above the --- dashes is an ATX-style header.
Just add a newline to the end of one.md and everything should work the way you expected.

Resources