6
6
Formatter for LaTeX fancyvrb output.
8
:copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
8
:copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9
9
:license: BSD, see LICENSE for details.
20
20
def escape_tex(text, commandprefix):
21
return text.replace('@', '\x00'). \
22
replace('[', '\x01'). \
23
replace(']', '\x02'). \
24
replace('\x00', '@%sZat[]' % commandprefix).\
25
replace('\x01', '@%sZlb[]' % commandprefix).\
26
replace('\x02', '@%sZrb[]' % commandprefix)
21
return text.replace('\\', '\x00'). \
22
replace('{', '\x01'). \
23
replace('}', '\x02'). \
24
replace('^', '\x03'). \
25
replace('_', '\x04'). \
26
replace('\x00', r'\%sZbs{}' % commandprefix). \
27
replace('\x01', r'\%sZob{}' % commandprefix). \
28
replace('\x02', r'\%sZcb{}' % commandprefix). \
29
replace('\x03', r'\%sZca{}' % commandprefix). \
30
replace('\x04', r'\%sZus{}' % commandprefix)
29
33
DOC_TEMPLATE = r'''
177
187
*New in Pygments 0.7.*
179
189
*New in Pygments 0.10:* the default is now ``'PY'`` instead of ``'C'``.
192
If set to ``True``, enables LaTeX comment lines. That is, LaTex markup
193
in comment tokens is not escaped so that LaTeX can render it (default:
194
``False``). *New in Pygments 1.2.*
197
If set to ``True``, enables LaTeX math mode escape in comments. That
198
is, ``'$...$'`` inside a comment will trigger math mode (default:
199
``False``). *New in Pygments 1.2.*
182
202
aliases = ['latex', 'tex']
192
212
self.verboptions = options.get('verboptions', '')
193
213
self.nobackground = get_bool_opt(options, 'nobackground', False)
194
214
self.commandprefix = options.get('commandprefix', 'PY')
215
self.texcomments = get_bool_opt(options, 'texcomments', False)
216
self.mathescape = get_bool_opt(options, 'mathescape', False)
196
218
self._create_stylesheet()
260
282
realoutfile = outfile
261
283
outfile = StringIO()
263
outfile.write(r'\begin{Verbatim}[commandchars=@\[\]')
285
outfile.write(r'\begin{Verbatim}[commandchars=\\\{\}')
265
287
start, step = self.linenostart, self.linenostep
266
288
outfile.write(',numbers=left' +
267
289
(start and ',firstnumber=%d' % start or '') +
268
290
(step and ',stepnumber=%d' % step or ''))
291
if self.mathescape or self.texcomments:
292
outfile.write(r',codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8}')
269
293
if self.verboptions:
270
294
outfile.write(',' + self.verboptions)
271
295
outfile.write(']\n')
273
297
for ttype, value in tokensource:
274
value = escape_tex(value, self.commandprefix)
298
if ttype in Token.Comment:
300
# Try to guess comment starting lexeme and escape it ...
302
for i in xrange(1, len(value)):
303
if start[0] != value[i]:
307
value = value[len(start):]
308
start = escape_tex(start, self.commandprefix)
310
# ... but do not escape inside comment.
311
value = start + value
312
elif self.mathescape:
313
# Only escape parts not inside a math environment.
314
parts = value.split('$')
316
for i, part in enumerate(parts):
318
parts[i] = escape_tex(part, self.commandprefix)
319
in_math = not in_math
320
value = '$'.join(parts)
322
value = escape_tex(value, self.commandprefix)
324
value = escape_tex(value, self.commandprefix)
276
326
while ttype is not Token:
285
335
spl = value.split('\n')
286
336
for line in spl[:-1]:
288
outfile.write("@%s[%s][%s]" % (cp, styleval, line))
338
outfile.write("\\%s{%s}{%s}" % (cp, styleval, line))
289
339
outfile.write('\n')
291
outfile.write("@%s[%s][%s]" % (cp, styleval, spl[-1]))
341
outfile.write("\\%s{%s}{%s}" % (cp, styleval, spl[-1]))
293
343
outfile.write(value)