\imagegraphics doesn't interpret \ escape in filename - latex

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.

Related

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.

How pandoc lua filters can be used to add the title as the level-1 header?

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.

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.

What does ellipsis at end of .clang-format mean?

When running:
$ clang-format -style=Google -dump-config > .clang-format
The file is appended by a ellipsis (...).
TabWidth: 8
UseTab: Never
...
Does it have any significance? Can I delete it? Someone is asking what it means in a code review.
https://clang.llvm.org/docs/ClangFormatStyleOptions.html says:
The .clang-format file uses YAML format
http://www.yaml.org/refcard.html says:
'...': Document terminator.
Some more from http://yaml.org/spec/1.2/spec.html:
YAML uses three dashes (“---”) to separate directives from document
content. This also serves to signal the start of a document if no
directives are present. Three dots ( “...”) indicate the end of a
document without starting a new one, for use in communication
channels.

doxygen: How to link to an external URL with # (hash) sign?

I am trying to link an URL which contains a # (hash) sign, see upstream code here:
http://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_8.2.html#para_4bcb841e-c6bf-4e26-82a5-3fad3c942da0
Everything works as expected with HTML output, but things gets nasty with LaTeX output (PDF). It fails with:
! Illegal parameter number in definition of \Hy#tempa.
<to be read again>
p
l.153 ...ageref{classgdcm_1_1ImageRegionReader}}{}
?
Looking at the generated code, I see:
\href{http://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_8.2.html#para_4bcb841e-c6bf-4e26-82a5-3fad3c942da0}
I cannot simply escape the # sign with a backslash since it would break the HTML output (but fix the LaTeX output). I found on the web that including bigfoot before hyperref can solve this sort of issue, but this did not work for me using the following trick in the doxyfile:
## -1650,7 +1650,7 ## PAPER_TYPE = letter
# If left blank no extra packages will be included.
# This tag requires that the tag GENERATE_LATEX is set to YES.
-EXTRA_PACKAGES =
+EXTRA_PACKAGES = bigfoot
# The LATEX_HEADER tag can be used to specify a personal LaTeX header for the
# generated LaTeX document. The header should contain everything until the first
Is there anything simple to include a URL with # sign that works for both HTML and LaTeX output ?
EDIT: I found out that doxygen generate the following code:
% Hyperlinks (required, but should be loaded last)
\usepackage{ifpdf}
\ifpdf
\usepackage[pdftex,pagebackref=true]{hyperref}
\else
\usepackage[ps2pdf,pagebackref=true]{hyperref}
\fi
So I cannot explicitly \usepackage{hyperref} after \usepackage{bigfoot} since this creates some sort of conflict.
Package hyperref Message: Driver (autodetected): hpdftex.
(/usr/share/texlive/texmf-dist/tex/latex/hyperref/hpdftex.def
(/usr/share/texlive/texmf-dist/tex/latex/oberdiek/rerunfilecheck.sty))
! LaTeX Error: Option clash for package hyperref.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.101 \else

Resources