Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a LaTeX string in python?

I'm writing an application, part of whose functionality is to generate LaTeX CVs, so I find myself in a situation where I have strings like

\begin{document}
\title{Papers by AUTHOR}
\author{}
\date{}
\maketitle
\begin{enumerate}

%%   LIST OF PAPERS
%%   Please comment out anything between here and the
%%   first \item
%%   Please send any updates or corrections to the list to
%%   XXXEMAIL???XXX

%\usepackage[pdftex, ...

which I would like to populate with dynamic information, e.g. an email address. Due to the format of LaTeX itself, .format with the {email} syntax won't work, and neither will using a dictionary with the %(email)s syntax. Edit: in particular, strings like "\begin{document}" (a command in LaTeX) should be left literally as they are, without replacement from .format, and strings like "%%" (a comment in LaTeX) should also be left, without replacement from a populating dictionary. What's a reasonable way to do this?

like image 427
Valkyrie Savage Avatar asked Jul 12 '11 22:07

Valkyrie Savage


1 Answers

Why won't this work?

>>> output = r'\author{{email}}'.format(email='[email protected]')
>>> print output
\author{email}

edit: Use double curly braces to "escape" literal curly braces that only LaTeX understands:

>>> output = r'\begin{{document}} ... \author{{{email}}}'.format(
... email='[email protected]')
>>> print output
\begin{document} ... \author{[email protected]}
like image 166
Santa Avatar answered Oct 14 '22 13:10

Santa