Using LaTeX with Tryton

At first I had the same idea, so I digged into Genshi and tried a lot but without luck.

Unfortunately it’s harder because of the translation system. When you want to update the translations Tryton will also read all the reports and tries to get the strings which can be translated.

After spending some hours to get LaTeX working with Genshi and failed, I decided to move to Jinja.

Why is Jinja more suitable:

  1. Loading the LaTeX template into memory Python or Genshi is automatically escaping the different special characters. Jinja has the possibility to disable escaping (no idea how they do it, but it works flawlessly).
  2. Writing the generated document to a LaTeX file Python or Genshi is again escaping and removing special characters. From characters like // or //* the first / is removed which causes an error when the file is processed. Because of the disabled escaping Jinja outputs a perfect LaTeX file
  3. With Jinja you can change the default strings for variables, blocks etc. This makes it possible to have kind of the same syntax for LaTeX. I know not much of a ‘more suitable’ but I want to mention it.
  4. Because LaTeX templates are plain text, Genshi doesn’t know which text is translatable. Jinja has an i18n extension which makes it possible to enclose your strings which should be translated. This makes is easier for the user, but also keeps the translation a bit more clean because characters like #, : or & can be left outside the enclosing and out of the translation.
  5. very simple escape filter for the dynamic data which can be called by the user.

For me, loading the template and outputting it into a faultless LaTeX file was the biggest issue. By moving to Jinja this was working right out of the box. Also the possibility to change the start and endstrings of the different blocks and variables made the template more readable. In the end translation setup was very easy.

I’ve looked into that as well, but walked into the same problem with escaping.

My code including comments and empty lines is now around 200 lines. This includes:

  1. adding MIME type
  2. adding LaTeX to the selection in the UI
  3. own translate factory (unfortunately needed because Jinja wants gettext and ngettext functions)
  4. extend the TranslationSet wizard to get the translatable strings
  5. report mixin to escape data, render and convert the template into a document

I have to clean up things and make parts more configurable before I release it so it can be tested.

I do not understand that. The basic.tex example shows clearly that Genshi does not break the syntax.

This is also possible with Genshi.

It is explicitly documented that on plain text, it must use gettext: Genshi: Internationalization and Localization
Now it will be a good improvement for Tryton if the Report and Translation used the proper mechanism for plain text.

I do not see the point as it is to escape string for HTML.

Too bad it could have been part of the standard Tryton with probably less effort and improving Tryton.

Please do some research what LaTeX actually is and how a LaTeX file looks like. From the man-page of texexec:
texexec, a ruby(1) script, is the command-line front end to the ConTeXt typesetting system.
And ConTeXt - Wikipedia . So the basic.tex is NOT a LaTeX file, but a ConTeXt file which has another markup then LaTeX.

I couldn’t find anything about this. With Jinja I set the different variables

JINJA_SETTINGS = Environment(
    autoescape = False,
    block_start_string = r'\BLOCK{',
    block_end_string = r'}',
    variable_start_string = r'\VAR{',
    variable_end_string = r'}',
    comment_start_string = r'\#{',
    comment_end_string = r'}',
    line_statement_prefix = r'%%',
    line_comment_prefix = r'%#',
    trim_blocks = True,
    extensions = ['jinja2.ext.i18n']
)

So in my template I can use

\VAR{ record.line.product.rec_name }
\BLOCK{ for line in record }
  \VAR{ line.name }
\BLOCK{ endfor }

Can I change the {% and %} into \BLOCK{ and } in Genshi?

Did look into this one and with minor changes it can work with gettext in plain text templates.

This function only escapes the LaTeX special chars

    @classmethod
    def _escape_data(cls, value):
        """
        The special characters are:
            \ = command character
            { = open group
            } = end group
            & = table column separator
            # = parameter specifier
            % = comment character
            _ = subscript
            ^ = superscript
            ~ = non-breakable space
            $ = mathematics mode
        """
        return (value.replace(r'{', r'\{')
                .replace(r'}', r'\}')
                .replace(r'&', r'\&')
                .replace(r'#', r'\#')
                .replace(r'%', r'\%')
                .replace(r'_', r'\_')
                .replace(r'^', r'\^')
                .replace(r'~', r'\~')
                .replace(r'$', r'\$')
               )

So when you wan to print the dollar currency symbol, it will be escaped so LaTeX sees it as text and not a command.

Again, this is pure LaTeX no ConTeXt. It needs another approach to get it working.

It all started because of the page subtotals in LaTeX. I’m not an experienced developer like you, so I take my time to understand things, take some different approaches to the problem. But I don’t want to waste too much time on something which I can’t get to work like escaping with Genshi. That’s why I putted it into a module with one file as a proof-of-concept to get it working. I hope that parts or the whole module can be integrated, but that’s too much for me and guidance is needed for that.