pystitch / stitch

Write reproducible reports in Markdown
https://pystitch.github.io
MIT License
441 stars 20 forks source link

PDF output fails with missing \begin{document} error #71

Open robintw opened 6 years ago

robintw commented 6 years ago

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?

kiwi0fruit commented 5 years ago

I confirm the bug. Though knitty and pandoctools work well:

---
pandoctools:
  out: "*.pdf"
...

# H1

t1

But this very file cannot be stitched to pdf...

f1se4 commented 4 years ago

Somebody has find way to solve this issue? I have no problem with html, but when importing to pdf I have the same 43 error.

robintw commented 4 years ago

Sorry @f1se4, I never found a way around this.

kiwi0fruit commented 4 years ago

Switch from stitch to knitty is the only workaround that I know.

kiwi0fruit commented 4 years ago

May be you can try downgrading pandoc to some earlier version - from the time when stitch was last updated.

f1se4 commented 3 years ago

Thank you all!!!! I am also stuck with knitty ^^ hahaha, I think that have some issues with py3.8... well... What I don't know is why R-Markdown alternative in Python (without have to be coding with jupyter etc...) is not something interesting for the comunity. Quick fine and quick non-format and post-compiled report... Who has worked with R or Latex for example, love to write in plain text (with highlight gimmiks etc.. but in plain text) and compile at the end... :-m

kiwi0fruit commented 3 years ago

Please elaborate on knitty issues with py3.8

augustecolle commented 2 years ago

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)