JelteF / PyLaTeX

A Python library for creating LaTeX files
https://jeltef.github.io/PyLaTeX/
MIT License
2.24k stars 286 forks source link

Multi-Line Strings #290

Closed Czarified closed 4 years ago

Czarified commented 4 years ago

Please excuse my ignorance, because I'm new to pylatex. If I have a chunk of text that is rather long, but always the same, I may want to write this out as a multi-line string in my source code, and pass that string value to pylatex to be included in the report. The pylatex.Document.append() method doesn't seem to accept this, because I'm getting a Latex error when it tries to build (see cause below).

Let's say I have a paragraph of analysis methods, and I always need to include this paragraph.

import pylatex

doc = pylatex.Document()
analysis = doc.create(pylatex.Section('Analysis'))
with analysis:
    doc.append(
        '''
        This text is in the analysis section and discussing all the really super duper
        interesting bits about how I took some numbers and turned them into other numbers.
        When I want to talk about something else, sometimes I can do this in the python string
        and separate the text. I do this, because now I want to talk about puppies, and how
        they affect my thought process towards composite elasticity.
        '''
    )

My solution so far is to use NoEscape() to encapsulate the whole thing in a raw string. This seems to get the job done, since pylatex interprets a multi-line string with newline characters at each return.

with analysis:
    doc.append(pylatex.NoEscape(
       r'''
        This text is in the analysis section and discussing all the really super duper
        interesting bits about how I took some numbers and turned them into other numbers.
        When I want to talk about something else, sometimes I can do this in the python string
        and separate the text. I do this, because now I want to talk about puppies, and how
        they affect my thought process towards composite elasticity.
        '''
        )
    )

Is there a more elegant way to accomplish this?

I also thing this might be useful in the basic documentation page, but I don't have a development environment set up...

JelteF commented 4 years ago

That shouldn't be necessary, what is the latex error that you get?

Czarified commented 4 years ago

I guess I should have included this bit too: OS: Windows 10 (x64 1803) Python version: 3.7.6 PyLaTeX version: 1.3.3

This the snippet generated without the NoEscape():

\section{Analysis}%
\label{sec:Analysis}%
\newline%
        This text is in the analysis section and discussing all the really super duper\newline%
        interesting bits about how I took some numbers and turned them into other numbers.\newline%
        \newline%
        When I want to talk about something else, sometimes I can do this in the python string\newline%
        and separate the text. I do this, because now I want to talk about puppies, and how\newline%
        they affect my thought process towards composite elasticity.\newline%

%

So I don't want the newlines at the end of each line. My intent was for it to be formatted like a docstring.

And the error I get when I try to generate pdf instead of just tex:

Traceback (most recent call last):
  File "reporting.py", line 63, in <module>
    doc.generate_pdf('__report', clean_tex=False)
  File "D:\Anaconda3\lib\site-packages\pylatex\document.py", line 245, in generate_pdf
    **check_output_kwargs)
  File "D:\Anaconda3\lib\subprocess.py", line 411, in check_output
    **kwargs).stdout
  File "D:\Anaconda3\lib\subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['latexmk', '--pdf', '--interaction=nonstopmode', 'report.tex']' returned non-zero exit status 12.
Czarified commented 4 years ago

Closing this issue. I have resorted to using the NoEscape with my multi-line strings or manually writing them to the file after creation. It's not too difficult, and I ended up finding some examples in the docs where you/they do the same thing.

Hopefully anyone else having this problem will find this closed issue in the future.