15

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?

4
  • Operating here under the assumption that I don't really want to have to type %% for % everywhere... Commented Jul 12, 2011 at 23:23
  • What version of Python are you using? str.format() is new to 2.6 Commented Jul 12, 2011 at 23:34
  • Have you ever finished this library, because I just created something like it: github.com/JelteF/PyLaTeX Commented Jan 18, 2014 at 13:48
  • Cool! I did finish working on this project, the source for which you can find at invenio-software.org (still developed by others, not by me). I didn't split out the LaTeX piece into anything nice, so it's a good thing someone did. :) Commented Jan 18, 2014 at 21:01

2 Answers 2

20

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]}
Sign up to request clarification or add additional context in comments.

4 Comments

out = r''' ...: \begin{document} ...: {{email}}'''.format(email='[email protected]') --------------------------------------------------------------------------- KeyError Traceback (most recent call last) /home/valkyrie/projects/invenio/modules/websearch/lib/<ipython console> in <module>() KeyError: 'document'
the LaTeX contains things with curly braces that should be kept unmodified (including the curly braces themselves).
So use double-curly braces for those (essentially escaping them.
It's sort of a shame that the only outs are doubling on curlies or percents, but as long as it works, I guess it's all good. :)
6

You may not use the new format syntax to avoid escaping the { and }.

That should work:

>>> a = r'''
\title{%(title)s}
\author{%(author)s}
\begin{document}'''

>>> b = a % {'title': 'My Title', 'author': 'Me, Of course'}
>>> print(b)

\title{My Title}
\author{Me, Of course}
\begin{document}

You should use raw strings r'something' to avoid escaping \ as \\.

PS: You should take a look on txt2tags, a Python script to convert t2t formatted text into html, latex, markdown etc. Check the source code to see how these conversions are done.

2 Comments

It complains about LaTeX's comments, e.g. I have a line which says "%\usepackage...." that causes the dictionary formatter to complain. I will look into txt2tags, though, thank you!
You can add comments after formatting... Or escape the % with \%.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.