How to find errors after post-processing with Pandoc?

I use pandoc to post-process the markdown files that I make with Scrivener. Sometimes there will be an error and the Error Log will produce something like:

Error producing PDF.
! Missing $ inserted.
<inserted text> 
                $
l.1582 \$\eta

Sometimes what comes after that “l.1582” be unique in my document, and I can use that to hunt down the error in the source Scrivener documents. But sometimes it won’t be, and I’d like to know whether I can use the “l.1582” tag to determine the location of the error in the markdown document that Pandoc is processing. But I can’t figure out what that “l.1582” means, or how to use the information it conveys.

I’d be grateful for any/all help! Thanks!

This has nothing to do with Pandoc, but with the LaTeX engine that is converting the .tex to .PDF

So lets make a test doc we know will fail (an unknown environment called mystery):

---
title: LaTeX Test
author: Jane Doe
---

\mystery{Hello!}

These are the voyages of the Starship Enterprise. Its continuing mission, to explore strange new worlds, to seek out new life and new civilizations, to boldly go where no one has gone before.  

And lets compile it:

▷ pandoc -s --pdf-engine=xelatex -o test.pdf test.md
Error producing PDF.
! Undefined control sequence.
l.59 \mystery

Exception: pandoc exited with 43

l.59 refers to the line from the .tex file source Pandoc produced from the .md file, but the error is coming from the TeX engine as it tries to make the PDF:

56. \begin{document}
57. \maketitle
58.
59. \mystery{Hello!}
60.
61. These are the voyages of the Starship Enterprise...

The error is telling us the control sequence is undefined (as we knew it would be).

If you want to see more details, you should pass --verbose to pandoc:

▷ pandoc --verbose -s --pdf-engine=xelatex -o test.pdf test.md
[makePDF] temp dir:
/private/var/folders/bf/l6n8fnvd7zl5qsvlwgy7sk7w0000gn/T/tex2pdf.-da421d9db3324ed9
[makePDF] Command line:
xelatex "-halt-on-error" "-interaction" "nonstopmode" "-output-directory" "/private/var/folders/bf/l6n8fnvd7zl5qsvlwgy7sk7w0000gn/T/tex2pdf.-da421d9db3324ed9" "/private/var/folders/bf/l6n8fnvd7zl5qsvlwgy7sk7w0000gn/T/tex2pdf.-da421d9db3324ed9/input.tex"
...
! Undefined control sequence.
l.59 \mysterious
                {Hello!}
No pages of output.
Transcript written on /private/var/folders/bf/l6n8fnvd7zl5qsvlwgy7sk7w0000gn/T/
tex2pdf.-da421d9db3324ed9/input.log.

Error producing PDF.
! Undefined control sequence.
l.59 \mysterious

The detailed log file is saved, you can also redirect the verbose output from the terminal to a log so you can examine it at your leisure later on. This is one thing scrivomatic does with the -b option (redirect all verbose output to a latex log saved alongside your main output).

2 Likes

Thanks so much! That helps tremendously. Now I can just compile the raw tex source file and then navigate to the line of the error to investigate. Exactly what I needed!

Just wanted to add here for folks who find this thread that one can output for investigation the temporary latex file that Pandoc works on. To do that, add this argument to your pandoc call: --pdf-engine-opt=-outdir=foo. The temp latex files will then be in foo . Found that little protip here.

2 Likes

@nontroppo This is a thread from a while ago, but: I’ve just noticed that the trick I noted in my post just above no longer works. That is, it doesn’t give me the temporary latex file from which the beamer presentation is ultimate produced. Do you have any other tips for getting my hands on that file? Without it, I can’t quite figure out how to trace an error I’m notified of by Scrivener when it tells me that pandoc failed to create the final pdf.

Any help is much appreciated!

Did you change your PDF engine? The easiest solution is to run pandoc outside of Scrivener, so compile to MD and then run Pandoc in the terminal to LaTeX first, then to PDF. Normally if you redirect all pandoc output to a log file you should at least see the errors. This two step post-process lets you keep the tex, and scrivomatic automates this as it will then run latexmk as a step outside Scrivener’s post-processor…

2 Likes

Thank you! This feels like a silly question, but: when I use pandoc directly on the command line to convert the md file to tex, do I invoke ‘beamer’? Or do I invoke ‘beamer’ only when converting the resulting tex file to pdf? — It’s been so long since I’ve needed to use pandoc on the command line!

UPDATE: I’ve figured out that I can get a tex file via the command line by processing the md file and using the command “pandoc -st beamer … -o test.tex”. When I process directly to pdf I have to use “pandoc -t beamer …”. If I use “pandoc -t beamer … -o test.tex”, I don’t get the full header. I’d be interested to learn what the difference is here between “-st” and “-t”.

1 Like

-s means standalone, which includes all headers. It should be used whenever you want a full doc, not a fragment. And yes you want to invoke beamer as the output, so in this case you should be using pandoc -s -t beamer -o test.tex ...

2 Likes

Thanks so much. I’ve just created a new project format that creates just the tex file and then opens it immediately. Now I can just compile to that if I get an error compiling straight to pdf from Scrivener, and then inspect the resulting tex file.

Appreciate it!

1 Like