0

I have a markdown document that defines a LaTeX macro and then calls it. When I use pandoc to convert this document to LaTeX, I don't get the result that I expect. The problem is related to a % sign (LaTeX comment sign) at the end of my macro definition. For example, consider this markdown file:

\newcommand\myMacro{Hello!}%
\myMacro

When I run pandoc example.md --to latex-latex_macros, I expect it to produce a LaTeX file that looks just like the code block above. Instead, I get

\newcommand\myMacro{Hello!}

\% \myMacro

If I add the hard_line_breaks option by calling pandoc example.md --to latex --from markdown-latex_macros+hard_line_breaks, I get better output, but still not what I expect:

\newcommand\myMacro{Hello!}

\%\\
\myMacro

(Using --wrap=preserve produces a similar result.) I am using pandoc 2.9.2, and I get the same result on Ubuntu and on Windows. I've experimented with other pandoc options, and they don't seem to help. In particular, I've used the eol pandoc option; it doesn't make a difference, which leads me to think that this problem isn't due line breaks being LF or CRLF.

Why is pandoc parsing my .md file in this way? And what can I do to get it to produce a LaTeX file that looks like the first code block in this post? I've read SO posts, the pandoc user guide, and issues on the pandoc Github site. Some of them are related: for example, this Github issue and Preserve Line Breaks in Pandoc Markdown -> LaTeX Conversion. But I can't see that any of them speak directly to this problem.

user697473
  • 2,165
  • 1
  • 20
  • 47
  • maybe use ```{=latex}... https://pandoc.org/MANUAL.html#generic-raw-attribute – mb21 Mar 13 '20 at 09:57
  • Thank you, @mb21. Yes, that works nicely. But my ultimate hope is to write an R Markdown file, not just a markdown file, that will be converted into LaTeX as I would like. And I don't see a way to include generic raw attributes in R Markdown files -- do you? (The issue also comes up in the last paragraph of [this post](https://stackoverflow.com/questions/60640002/).) / I've focused here on pandoc because the problem with the R Markdown -> LaTeX conversion seems to lie with pandoc, but perhaps I should write a separate post about using generic raw attributes in R Markdown files. – user697473 Mar 13 '20 at 11:35
  • AFAIK RMarkdown is using pandoc under the hood, so it should work? Maybe check that the pandoc version it's using is new enough.. – mb21 Mar 13 '20 at 13:01
  • Thank you -- it does indeed work. The problem was that, in an R Markdown document, I wanted to create an R code chunk that had, within it, a LaTeX code block that used the "latex" generic raw attribute. I thought that there wasn't a way to do this. But there is: just wrap the string that specifies the LaTeX macro in the `knitr::raw_latex()` function. For example, `knitr::raw_latex(c("\newcommand\myMacro{Hello!}%", "\myMacro"))` will work. – user697473 Mar 14 '20 at 13:47

0 Answers0