question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PDF output fails with missing \begin{document} error

See original GitHub issue

I have tried running the example from here and converting to PDF with stitch basic.md -o basic.pdf and I get the following error:

Traceback (most recent call last):
  File "/Users/robin/anaconda3/envs/reporting-comparison/bin/stitch", line 11, in <module>
    sys.exit(cli())
  File "/Users/robin/anaconda3/envs/reporting-comparison/lib/python3.6/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/Users/robin/anaconda3/envs/reporting-comparison/lib/python3.6/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/Users/robin/anaconda3/envs/reporting-comparison/lib/python3.6/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/robin/anaconda3/envs/reporting-comparison/lib/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Users/robin/anaconda3/envs/reporting-comparison/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/Users/robin/anaconda3/envs/reporting-comparison/lib/python3.6/site-packages/stitch/cli.py", line 60, in cli
    convert(input_text, to, output_file=output_file, extra_args=extra_args)
  File "/Users/robin/anaconda3/envs/reporting-comparison/lib/python3.6/site-packages/stitch/stitch.py", line 480, in convert
    outputfile=output_file)
  File "/Users/robin/anaconda3/envs/reporting-comparison/lib/python3.6/site-packages/pypandoc/__init__.py", line 103, in convert_text
    outputfile=outputfile, filters=filters)
  File "/Users/robin/anaconda3/envs/reporting-comparison/lib/python3.6/site-packages/pypandoc/__init__.py", line 325, in _convert_input
    'Pandoc died with exitcode "%s" during conversion: %s' % (p.returncode, stderr)
RuntimeError: Pandoc died with exitcode "43" during conversion: b'Error producing PDF.\n! LaTeX Error: Missing \\begin{document}.\n\nSee the LaTeX manual or LaTeX Companion for explanation.\nType  H <return>  for immediate help.\n ...                                              \n                                                  \nl.63 \\textbackslash\n\n'

Any idea what’s going on?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
augustecollecommented, Aug 30, 2022

It’s caused by the way the backslash escape sequence gets handled by passing it to pandoc after explicitly being added in stitch/cli.py, here is the relevant block from cli.py:

    if to in ('latex', 'pdf') and not has_booktabs(extra_args):
        extra_args.append('--metadata=header-includes:\\usepackage{booktabs}')

From the pandoc’s man pages:

       -M KEY[=VAL], --metadata=KEY[:VAL]
              Set the metadata field KEY to the value VAL.  A value specified on the command line overrides a value
              specified  in  the  document  using  YAML  metadata blocks.  Values will be parsed as YAML boolean or
              string values.  If no value is specified, the value will be treated as Boolean  true.   Like  --vari‐
              able,  --metadata causes template variables to be set.  But unlike --variable, --metadata affects the
              metadata of the underlying document (which is accessible from filters and may be printed in some out‐
              put formats) and metadata values will be escaped when inserted into the template.

Note the last sentence --> when compiling to latex by using stitch, some characters are indeed escaped and the header-includes metadata thus compiles to \textbackslash usepackage\{booktabs\}.

The fix for me was removing the above if condition and including the booktabs package by specifying it as a YAML header in my stitch markdown file (see pandoc’s man pages):

---
header-includes:
  - \usepackage{booktabs}
---
(your stitch markdown text here)
0reactions
kiwi0fruitcommented, Sep 21, 2020

Please elaborate on knitty issues with py3.8

Read more comments on GitHub >

github_iconTop Results From Across the Web

LaTeX Error: Missing \begin document - Overleaf
This error appears when you have forgotten to include \begin{document} in your document. It can also appear if you have not written \documentclass[...]{....
Read more >
LaTeX Error-Missing Begin Document - TeX
In my case, the problem was solved by avoiding any empty space in the main file title. It is not a real solution,...
Read more >
Missing \begin{document} | The TeX FAQ
If the error recurs, it could well be due to a buggy class or package. However, it may be that none of the...
Read more >
Error: missing \begin{document} in LaTeX - Stack Overflow
When I copy the code you have shown here into Notepad++ with UTF-8 input encoding, I get the following result for the first...
Read more >
Incomprehensible Error about missing Begin of Document
Go to "Document" > "Document Settings" > "Format"; Un-check the box saying "Include Unicode BOM". That fixed it and I no longer get...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found