Coder Social home page Coder Social logo

latex's Introduction

latex

Documentation can be found at https://pythonhosted.org/latex .

Allows calling LaTeX from Python without leaving a mess. Similar to the (officially obsolete) tex package, whose successor is not PyPi-installable:

min_latex = (r"\documentclass{article}"
             r"\begin{document}"
             r"Hello, world!"
             r"\end{document}")

from latex import build_pdf

# this builds a pdf-file inside a temporary directory
pdf = build_pdf(min_latex)

# look at the first few bytes of the header
print bytes(pdf)[:10]

Also comes with support for using Jinja2 templates to generate LaTeX files.

make_env can be used to create an Environment that plays well with LaTex:

Variables can be used in a LaTeX friendly way: Hello, \VAR{name|e}.

Note that autoescaping is off. Blocks are creating using the block macro:

\BLOCK{if weather is 'good'}
Hooray.
\BLOCK{endif}

\#{comments are supported as well}
%# and so are line comments

To keep things short, line statements can be used:

%- if weather is good
Yay.
%- endif

Example use

from jinja2.loaders import FileSystemLoader
from latex.jinja2 import make_env

env = make_env(loader=FileSystemLoader('.'))
tpl = env.get_template('doc.latex')

print(tpl.render(name="Alice"))

The base.latex demonstrates how \BLOCK{...} is substituted for {% ... %}:

\documentclass{article}
\begin{document}
\BLOCK{block body}\BLOCK{endblock}
\end{document}

Finally, doc.latex shows why the %- syntax is usually preferable:

%- extends "base.latex"

%- block body
Hello, \VAR{name|e}.
%- endblock

Translations using Babel

Strings from .latex-templates can be extracted, provided your babel.cfg is setup correctly:

[jinja2: *.latex]
block_start_string = \BLOCK{
block_end_string = }
variable_start_string = \VAR{
variable_end_string = }
comment_start_string = \#{
comment_end_string = }
line_statement_prefix = %-
line_comment_prefix = %#
trim_blocks = True
autoescape = False

latex's People

Contributors

arvindch avatar grimmick avatar jeltef avatar mbr avatar mklappir avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

latex's Issues

jinja2 inheritance is working?

On the README you said that jinja2 is supported but there is so (none) example of how effectively use this, like: template inheritance?

Error on PYTHON 3.5 / Windows when testing example

There is no output because it has problems with the temporary directory

subprocess.CalledProcessError: Command '['latexmk', '-pdf', '-pdflatex=pdflatex -interaction=batchmode -halt-on-error -no-shell-escape -file-line-error %O %S', 'C:\\Users\\sruiz\\AppData\\Local\\Temp\\tmpfroqoypt\\tmpt171wtiv.latex']' returned non-zero exit status 12

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\data\__init__.py", line 252, in temp_saved
    yield tmp
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\latex\build.py", line 98, in build_pdf
    raise_from(LatexBuildError(base_fn + '.log'), e)
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\future\utils\__init__.py", line 399, in raise_from
    exec(execstr, myglobals, mylocals)
  File "<string>", line 1, in <module>
latex.exc.LatexBuildError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\data\__init__.py", line 255, in temp_saved
    os.unlink(tmp.name)
PermissionError: [WinError 32] El proceso no tiene acceso al archivo porque est▒ siendo utilizado por otro proceso: 'C:\\Users\\sruiz\\AppData\\Local\\Temp\\tmpfroqoypt\\tmpt171wtiv.latex'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\latex\build.py", line 100, in build_pdf
    return I(open(output_fn, 'rb'), encoding=None)
  File "c:\python35\Lib\contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\data\__init__.py", line 258, in temp_saved
    reraise(e)
TypeError: reraise() missing 1 required positional argument: 'value'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tmp.py", line 10, in <module>
    pdf = build_pdf(min_latex)
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\latex\build.py", line 203, in build_pdf
    return builder.build_pdf(source, texinputs)
  File "<decorator-gen-1>", line 2, in build_pdf
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\data\decorators.py", line 82, in _
    return f(*bvals.args, **bvals.kwargs)
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\latex\build.py", line 100, in build_pdf
    return I(open(output_fn, 'rb'), encoding=None)
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\tempdir.py", line 25, in __exit__
    return self.dissolve()
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\tempdir.py", line 30, in dissolve
    shutil.rmtree(self.name)
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\shutil.py", line 488, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\shutil.py", line 383, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\shutil.py", line 381, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 32] El proceso no tiene acceso al archivo porque est▒ siendo utilizado por otro proceso: 'C:\\Users\\sruiz\\AppData\\Local\\Temp\\tmpfroqoypt\\tmpt171wtiv.latex'
Exception ignored in: <bound method TempDir.__del__ of <tempdir.TempDir object at 0x0000000000B4C9E8>>
Traceback (most recent call last):
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\tempdir.py", line 19, in __del__
    self.__exit__(None, None, None)
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\tempdir.py", line 25, in __exit__
    return self.dissolve()
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\site-packages\tempdir.py", line 30, in dissolve
    shutil.rmtree(self.name)
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\shutil.py", line 488, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\shutil.py", line 383, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Users\sruiz\Projects\ManageQualityBetter\env\lib\shutil.py", line 381, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 32] El proceso no tiene acceso al archivo porque est▒ siendo utilizado por otro proceso: 'C:\\Users\\sruiz\\AppData\\Local\\Temp\\tmpfroqoypt\\tmpt171wtiv.latex'

Adding/Including External files to build directory

Hi, I'm trying to include an image in my python based latex file however, I've realised that since the latex file is build in a temporary location, it is unable to include images that are in a relative location to the python script.

Is there any way to easily get around this? I'd prefer to not use a full path, as it makes the script less portable.

One way I thought could be the simplest solution would be to have a function that allows adding files to the temporary build space (be that a symbolic link or a copy)

UnicodeDecodeError: 'utf-8'

Running the example

min_latex = (r"\documentclass{article}"
             r"\begin{document}"
             r"Hello, world!"
             r"\end{document}")

from latex import build_pdf

# this builds a pdf-file inside a temporary directory
pdf = build_pdf(min_latex)

# look at the first few bytes of the header
print (str(pdf)[:10])

I get

Traceback (most recent call last):
  File "/home/fariza/workspace/Scripts/temp/pdf.py", line 27, in <module>
    print (str(pdf)[:10])
  File "/usr/local/lib/python3.4/dist-packages/data/__init__.py", line 125, in __str__
    return self.__unicode__()
  File "/usr/local/lib/python3.4/dist-packages/data/__init__.py", line 133, in __unicode__
    return self.file.read()
  File "/usr/lib/python3.4/codecs.py", line 313, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 10: invalid continuation byte

As you can see, I am running in a Python3.4, any suggestions to make this work?

Import Error

I using Python 3.4.3 64 bit on windows 8.1 64 bit.

>>> from latex import build_pdf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\py34\lib\site-packages\latex\__init__.py", line 3, in <module>
    from .build import build_pdf
  File "C:\py34\lib\site-packages\latex\build.py", line 4, in <module>
    from future.utils import raise_from
ImportError: cannot import name 'raise_from'

\BLOCK do not work inside the \input{another_latex_file.tex}

i have multiple latex file so thats a reason.
report_base.tex hold multiple latex file.
for example.
report_base.tex contains :

\begin{document}
\input{\VAR{report_dir}titlepage.tex}
\end{document}

====================================================
titlepage.tex contains :

\section{Project Overview}
\label{sec:proj-overview}
\begin{xltabular}[l]{\linewidth}{cXcX}
\rowcolor{\colorthG} ID & Vulnerability & Severity Level & Recommendations \\
\BLOCK{ for task, list_of_vulnerabilities in vulnerabilities.items() }
\rowcolor{\colorvul}\multicolumn{4}{c}{\textcolor{white}{\VAR{task}}}\\
\BLOCK{ endfor }
\end{xltabular}

But it gives error.

Screenshot 2019-11-25 at 9 53 01 AM

ImportError: cannot import name wraps (Python 2.7.6)

when trying to do but this

from latex import build_pdf

I get an import error.

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from latex import build_pdf
  File "/usr/local/lib/python2.7/dist-packages/latex/__init__.py", line 4, in <module>
    from .build import build_pdf
  File "/usr/local/lib/python2.7/dist-packages/latex/build.py", line 6, in <module>
    from data import Data as I
  File "/usr/local/lib/python2.7/dist-packages/data/__init__.py", line 9, in <module>
    from six import text_type, PY2, reraise, StringIO, BytesIO, Iterator, wraps
ImportError: cannot import name wraps

(Python 2.7.6, Ubuntu)

htlatex builder support

Is it possible to build html from latex instead of pdf's with the htlatex builder without making changes to the source code?

Error on Windows 2016

Hello,

I used the same code on LInux, Windows 10 and Windows 2016.
On Windows 2016 the function build_pdf fails, without any log or output, as shown below for this simple example:

PS C:\Users\Administrator> python -c "import latex; latex.build_pdf(r'\documentclass{article}\begin{document}Hello\end{document}')"
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\latex\build.py", line 175, in build_pdf
    stdout=open(os.devnull, 'w'), )
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 363, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['pdflatex', '-interaction=batchmode', '-halt-on-error', '-no-shell-escape', '-file-line-error', 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\3\\tmptebd6l1k\\tmptnlokmm8.latex']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\latex\build.py", line 232, in build_pdf
    return builder.build_pdf(source, texinputs)
  File "<C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\decorator.py:decorator-gen-2>", line 2, in build_pdf
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\data\decorators.py", line 82, in _
    return f(*bvals.args, **bvals.kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\latex\build.py", line 177, in build_pdf
    raise_from(LatexBuildError(base_fn + '.log'), e)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\future\utils\__init__.py", line 403, in raise_from
    exec(execstr, myglobals, mylocals)
  File "<string>", line 1, in <module>
latex.exc.LatexBuildError: None

Flag to select unicode encoding

build_pdf returns a a Data instance containing the generated PDF, but I see no option to set the encoding flag of the Data instance documented here:
http://pythonhosted.org/data/api.html#data.Data

In particular I get unicode errors with LANG="C" on linux which could be circumvented by explicitly setting LANG="en_US.UTF-8".. However, I could find no such solution on Windows where I still get unicode errors:

Traceback (most recent call last):
  File "C:\Users\eda\AppData\Roaming\Python\Python35\site-packages\data\__init__.py", line 249, in temp_saved
    self.save_to(tmp)
  File "C:\Users\eda\AppData\Roaming\Python\Python35\site-packages\data\__init__.py", line 223, in save_to
    dest.write(self.__bytes__())
  File "C:\Users\eda\AppData\Roaming\Python\Python35\site-packages\data\__init__.py", line 91, in __bytes__
    return self.readb()
  File "C:\Users\eda\AppData\Roaming\Python\Python35\site-packages\data\__init__.py", line 16, in _
    rv = f(self, *args, **kwargs)
  File "C:\Users\eda\AppData\Roaming\Python\Python35\site-packages\data\__init__.py", line 192, in readb
    return self.stream.read(*args, **kwargs)
  File "c:\local\anaconda3\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 680667: character maps to <undefined>

I can run pdflatex.exe on the unicode-containing tex source with no problem, so the issue is really with the Data instance.. I would like a flag in build_pdf to set the encoding explicitly..

Use with utf8 string

I'm trying to use this package to call latex using a utf8 string.

I need to make something python 2 and 3 compatible.

Here is the repo and traceback:

        def test_cli_pdf(tmpdir):
            from catex.cli import _cli
            tmp_file = tmpdir.join("test.pdf")
            files = ["tests/data/latex1.tex", "tests/data/latex2.tex"]
    >       _cli(files, tmp_file.strpath)
    tests/test_cli.py:6: 
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    .tox/py33-test/lib/python3.3/site-packages/catex/cli.py:22: in _cli
        pdf = build_pdf(text)
    .tox/py33-test/lib/python3.3/site-packages/latex/build.py:206: in build_pdf
        return builder.build_pdf(source, texinputs)
    <decorator-gen-2>:2: in build_pdf
        ???
    .tox/py33-test/lib/python3.3/site-packages/data/decorators.py:82: in _
        return f(*bvals.args, **bvals.kwargs)
    .tox/py33-test/lib/python3.3/site-packages/latex/build.py:164: in build_pdf
        raise_from(LatexBuildError(base_fn + '.log'), e)
    .tox/py33-test/lib/python3.3/site-packages/latex/exc.py:15: in __init__
        self.log = open(logfn).read()

Have you seen PyLaTeX?

I happened to stumble on this package through a Pypi search for my own project PyLaTeX. Did you see it before creating your own similar library? If so what were you missing and couldn't it have been added with a pull request?

Switching the -no-shell-escape flag

Is it possible to override -no-shell-escape flag somehow? I have an use case where I have to use this flag because of inline images and similar weirdness :)

If it's not possible, would you mind if I send an extremely simple pull request which would allow switching this flag?

Looping?

Is there a way to loop through an iterable object similar to how it is done in jinja?

Package under Windows with Anaconda Python fails

Although pdflatex is added to the path variable I cannot get this package to work under Windows 10. The traceback when running the example is:

(base) C:\Users\David\git\latex>python sample_test.py
Traceback (most recent call last):
  File "sample_test.py", line 3, in <module>
    r = build_pdf(open('sample.latex'))
  File "C:\Users\David\git\latex\latex\build.py", line 232, in build_pdf
    return builder.build_pdf(source, texinputs)
  File "<decorator-gen-1>", line 2, in build_pdf
  File "C:\Users\David\Miniconda2\lib\site-packages\data\decorators.py", line 82, in _
    return f(*bvals.args, **bvals.kwargs)
  File "C:\Users\David\git\latex\latex\build.py", line 113, in build_pdf
    raise_from(LatexBuildError(base_fn + '.log'), e)
  File "C:\Users\David\Miniconda2\lib\site-packages\future-0.16.0-py2.7.egg\future\utils\__init__.py", line 454, in raise_from
    raise e
latex.exc.LatexBuildError: None

My environment:

  • Windows 10
  • Anaconda Python 2.7.15
  • pdfTeX, Version 3.14159265-2.6-1.40.19 (MiKTeX 2.9.6800 64-bit)

Log parsing fails on Windows with MikTeX

When running on Windows and using MikTeX, the filenames that are part of error messages look as follows:

c:/some/path/tmpcn8jq0.latex:12: Package pdftex
.def Error: File `./images/vs_logo_old.png' not found.

Specifically, they begin with drive letter followed by a colon. As such the current regular expression doesn't detect them.

The following monkey patch of the regexp mitigates the problem:

import latex.errors
latex.errors.LATEX_ERR_RE = re.compile(r'(?P<filename>([a-z]:)?[^:]+):(?P<line>[0-9]+):\s*(?P<error>.*)')

Possible to hide Windows command window (cmd) when building?

First off, your package has been working quite well. Thanks for creating it.

I'm usually a Linux person, but I have a need to create a Windows-based LaTeX report-building application using Python/GTK and Jinja2.

What I want to do is let the user pick some build options via GUI, call into your latex package to do the compile, and then present the completed PDF to the user.

At the moment, when the call is made to build_pdf(), a separate process is created to start the latexmk (using MikTeX) build, which flashes a Windows command window (cmd).

Is there any way to hide the command window during this build process?

As I'm not familar Windows coding, I'm not even sure if this is controllable from your package, or if it's a larger issue at an architectural level.

Thanks

RIch

local .sty file

Hello

Finally I am trying to use the compiler in my real code, and it doesn't work ;( the reason, is that I am including \usepackage{emvadatasheet} and I have the local emvadatasheet.sty in the same directory as the document.tex file.
If i compile using pdflatex it works, but it doesn't using your package.
I tried setting enviroment variables to include the directory but no luck so far.
Do you have any suggestions?

Thanks
Federico

SyntaxWarning: invalid escape sequence '\B'

There are three SyntaxWarnings that show up when running using this library.

.venv\Lib\site-packages\latex\jinja2.py:28: SyntaxWarning: invalid escape sequence '\B'
  'block_start_string': '\BLOCK{',
.venv\Lib\site-packages\latex\jinja2.py:30: SyntaxWarning: invalid escape sequence '\V'
  'variable_start_string': '\VAR{',
.venv\Lib\site-packages\latex\jinja2.py:32: SyntaxWarning: invalid escape sequence '\#'
  'comment_start_string': '\#{',

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.