Package: django-tex
django-texdjango-tex is a simple Django app to render LaTeX templates and compile
them into PDF files.Django-tex requires a local LaTeX installation and uses the jinja2
templating engine for template rendering.Installationdjango-texis available onpypi.org. It can be installed by:pip install django_texQuick startAdd "django_tex" to yourINSTALLED_APPSsetting:INSTALLED_APPS=[...'django_tex',]Configure a template engine namedtexin settings.py:TEMPLATES=[{'NAME':'tex','BACKEND':'django_tex.engine.TeXEngine','APP_DIRS':True,},]Create a LaTeX template in your template directory:# test.tex\documentclass{article}\begin{document}\section{{{- foo -}}}\end{document}Use "compile_template_to_pdf" in your code to get the PDF file as a bytes object:fromdjango_tex.coreimportcompile_template_to_pdftemplate_name='test.tex'context={'foo':'Bar'}PDF=compile_template_to_pdf(template_name,context)Or userender_to_pdfto generate a HTTPResponse containing the PDF file:fromdjango_tex.shortcutsimportrender_to_pdfdefview(request):template_name='test.tex'context={'foo':'Bar'}returnrender_to_pdf(request,template_name,context,filename='test.pdf')Some notes on usageLatex binaryThe default LaTeX interpreter is set tolualatex. This can be changed by the settingLATEX_INTERPRETER, for instance:LATEX_INTERPRETER = 'pdflatex'. Of course, the interpreter needs
to be installed on your system fordjango-texto work properly.Interpreter argumentsYou can pass additional arguments to the latex interpreter by using theLATEX_INTERPRETER_OPTIONSsetting.Whitespace controlSince django-tex uses jinja, you can use jinja's whitespace control in
LaTeX templates. For example,\section{ {{ foo }} }would be rendered as\section{ Bar }with the above context;\section{ {{- foo -}} }, however,
gets rendered nicely as\section{Bar}.Built-in filtersDjango's built-in filters are available. So you can use{{ foo|date('d. F Y') }}to get1. Januar 2018, for instance.Further, django-tex adds the custom filterlocalizeto the jinja environment.
This runs its input throughdjango.utils.formats.localize_inputto
create a localized representation. The output depends on theUSE_L10NandLANGUAGE_CODEsettings. Use the filter like this:{{ foo|localize }}.If you want to convert linebreaks into LaTeX linebreaks (\\), use thelinebreaksfilter ({{ foo | linebreaks }}).Escaping LaTeX special charactersTo escape LaTeX special characters, use thelatex_escapefilter, i.e.{{ foo | latex_escape }}s.
This escapes the following characters:&$%#_{}\^~(see also this SO question)
Using this filter all printable character should lead to a successful LaTeX build.
Spacing for the characters\^~is automatically adopted as an end user would expect it.Please note Jinja's autoescaping is turned off in the defaultdjango-texenvironment.Custom filtersCustom filters can be defined as explained in  the jinja documentationhere. For example, the following filter formats adatetime.timedeltaobject as a hh:mm string:defhhmm_format(value):total_seconds=value.total_seconds()hours,remainder=divmod(total_seconds,3600)minutes,seconds=divmod(remainder,60)return'{:n}:{:02n}'.format(hours,minutes)The filter has to be added to a custom environment and thedjango-textemplating engine has to be made aware
of the environment. This can be achieved, for example, by defining a custom environment callable in anenvironment.pymodule in your app:# environment.pyfromdjango_tex.environmentimportenvironmentdefhhmm_format(value):pass# as abovedefmy_environment(**options):env=environment(**options)env.filters.update({'hhmm_format':hhmm_format})returnenv... and passing the dotted path tomy_environmentto theTEMPLATESsettings:# settings.pyTEMPLATES=[{'NAME':'tex','BACKEND':'django_tex.engine.TeXEngine','APP_DIRS':True,'OPTIONS':{'environment':'myapp.environment.my_environment',}},]Including graphics filesGraphics can be included in LaTeX documents using the\includegraphics{<filename>}command provided
by thegraphicxpackage. Normally, LaTeX looks for graphics files in the current working directory, i.e. the
directory including the source.texfile. The problem here is thatdjango-texcreates a temporary directory to
store the source file so that the LaTeX compiler does not see any graphics files provided by the Django application.
This problem can be solved by specifying the absolute path to one or more directories including the graphics files
using the\graphicspathcommand.Django-texallows the user to specify the absolute paths to one or more directories in theLATEX_GRAPHICSPATHsetting. This setting should contain a list of one or more paths:# settings.pyLATEX_GRAPHICSPATH=['c:\foo\bar','c:\bar\foo']Of course, a good way of constructing those paths is to useos.path.join(BASE_DIR, <path>).Using the template tag{% graphicspath %}, the correct\graphicspathcommand can be inserted into the.textemplate. In the above case,{% graphicspath %}turns into\graphicspath{ {"c:/foo/bar/"} {"c:/bar/foo/"} }. Use{% graphicspath %}like this:\documentclass{article}
\usepackage{graphicx}

{% graphicspath %}

\begin{document}

\includegraphics{foo}

\end{document}IfLATEX_GRAPHICSPATHis not specified,django-textakes theBASE_DIRinstead.Note: There might be a problem if the path to the graphics directory contains whitespaces. To my knowledge,lualatexcannot handle whitespaces in the\graphicspathcommand, butpdflatexcan.