2 pygments.formatters.latex
3 ~~~~~~~~~~~~~~~~~~~~~~~~~
5 Formatter for LaTeX fancyvrb output.
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
11from io
import StringIO
19__all__ = [
'LatexFormatter']
24 replace(
'{',
'\x01'). \
25 replace(
'}',
'\x02'). \
26 replace(
'\x00',
r'\%sZbs{}' % commandprefix). \
27 replace(
'\x01',
r'\%sZob{}' % commandprefix). \
28 replace(
'\x02',
r'\%sZcb{}' % commandprefix). \
29 replace(
'^',
r'\%sZca{}' % commandprefix). \
30 replace(
'_',
r'\%sZus{}' % commandprefix). \
31 replace(
'&',
r'\%sZam{}' % commandprefix). \
32 replace(
'<',
r'\%sZlt{}' % commandprefix). \
33 replace(
'>',
r'\%sZgt{}' % commandprefix). \
34 replace(
'#',
r'\%sZsh{}' % commandprefix). \
35 replace(
'%',
r'\%sZpc{}' % commandprefix). \
36 replace(
'$',
r'\%sZdl{}' % commandprefix). \
37 replace(
'-',
r'\%sZhy{}' % commandprefix). \
38 replace(
"'",
r'\%sZsq{}' % commandprefix). \
39 replace(
'"',
r'\%sZdq{}' % commandprefix). \
40 replace(
'~',
r'\%sZti{}' % commandprefix)
44\documentclass{%(docclass)s}
47\usepackage[%(encoding)s]{inputenc}
100\def\%(cp)s@reset{\let\%(cp)s@it=\relax \let\%(cp)s@bf=\relax%%
101 \let\%(cp)s@ul=\relax \let\%(cp)s@tc=\relax%%
102 \let\%(cp)s@bc=\relax \let\%(cp)s@ff=\relax}
103\def\%(cp)s@tok#1{\csname %(cp)s@tok@#1\endcsname}
104\def\%(cp)s@toks#1+{\ifx\relax#1\empty\else%%
105 \%(cp)s@tok{#1}\expandafter\%(cp)s@toks\fi}
106\def\%(cp)s@do#1{\%(cp)s@bc{\%(cp)s@tc{\%(cp)s@ul{%%
107 \%(cp)s@it{\%(cp)s@bf{\%(cp)s@ff{#1}}}}}}}
108\def\%(cp)s#1#2{\%(cp)s@reset\%(cp)s@toks#1+\relax+\%(cp)s@do{#2}}
112\def\%(cp)sZbs{\char`\\}
113\def\%(cp)sZus{\char`\_}
114\def\%(cp)sZob{\char`\{}
115\def\%(cp)sZcb{\char`\}}
116\def\%(cp)sZca{\char`\^}
117\def\%(cp)sZam{\char`\&}
118\def\%(cp)sZlt{\char`<}
119\def\%(cp)sZgt{\char`>}
120\def\%(cp)sZsh{\char`\#}
121\def\%(cp)sZpc{\char`\%%}
122\def\%(cp)sZdl{\char`\$}
123\def\%(cp)sZhy{\char`\-}
124\def\%(cp)sZsq{\char`\'}
125\def\%(cp)sZdq{\char`\"}
126\def\%(cp)sZti{\char`\~}
127%% for compatibility with earlier versions
141 aname = ttype[-1] + aname
149 Format tokens as LaTeX code. This needs the `fancyvrb` and `color`
152 Without the `full` option, code is formatted as one ``Verbatim``
153 environment, like this:
155 .. sourcecode:: latex
157 \begin{Verbatim}[commandchars=\\\{\}]
158 \PY{k}{def }\PY{n+nf}{foo}(\PY{n}{bar}):
162 Wrapping can be disabled using the `nowrap` option.
164 The special command used here (``\PY``) and all the other macros it needs
165 are output by the `get_style_defs` method.
167 With the `full` option, a complete LaTeX document is output, including
168 the command definitions in the preamble.
170 The `get_style_defs()` method of a `LatexFormatter` returns a string
171 containing ``\def`` commands defining the macros needed inside the
172 ``Verbatim`` environments.
174 Additional options accepted:
177 If set to ``True``, don't wrap the tokens at all, not even inside a
178 ``\begin{Verbatim}`` environment. This disables most other options
179 (default: ``False``).
182 The style to use, can be a string or a Style subclass (default:
186 Tells the formatter to output a "full" document, i.e. a complete
187 self-contained document (default: ``False``).
190 If `full` is true, the title that should be used to caption the
191 document (default: ``''``).
194 If the `full` option is enabled, this is the document class to use
195 (default: ``'article'``).
198 If the `full` option is enabled, this can be further preamble commands,
199 e.g. ``\usepackage`` (default: ``''``).
202 If set to ``True``, output line numbers (default: ``False``).
205 The line number for the first line (default: ``1``).
208 If set to a number n > 1, only every nth line number is printed.
211 Additional options given to the Verbatim environment (see the *fancyvrb*
212 docs for possible values) (default: ``''``).
215 The LaTeX commands used to produce colored output are constructed
216 using this prefix and some letters (default: ``'PY'``).
218 .. versionadded:: 0.7
219 .. versionchanged:: 0.10
220 The default is now ``'PY'`` instead of ``'C'``.
223 If set to ``True``, enables LaTeX comment lines. That is, LaTex markup
224 in comment tokens is not escaped so that LaTeX can render it (default:
227 .. versionadded:: 1.2
230 If set to ``True``, enables LaTeX math mode escape in comments. That
231 is, ``'$...$'`` inside a comment will trigger math mode (default:
234 .. versionadded:: 1.2
237 If set to a string of length 2, enables escaping to LaTeX. Text
238 delimited by these 2 characters is read as LaTeX code and
239 typeset accordingly. It has no effect in string literals. It has
240 no effect in comments if `texcomments` or `mathescape` is
241 set. (default: ``''``).
243 .. versionadded:: 2.0
246 Allows you to pick an alternative environment name replacing Verbatim.
247 The alternate environment still has to support Verbatim's option syntax.
248 (default: ``'Verbatim'``).
250 .. versionadded:: 2.0
253 aliases = [
'latex',
'tex']
254 filenames = [
'*.tex']
258 self.
nowrap = get_bool_opt(options,
'nowrap',
False)
261 self.
linenos = get_bool_opt(options,
'linenos',
False)
280 t2n = self.ttype2name = {Token:
''}
281 c2d = self.cmd2def = {}
286 return ','.join([
'%.2f' % (int(col[i] + col[i + 1], 16) / 255.0)
291 for ttype, ndef
in self.
style:
295 cmndef +=
r'\let\$$@bf=\textbf'
297 cmndef +=
r'\let\$$@it=\textit'
298 if ndef[
'underline']:
299 cmndef +=
r'\let\$$@ul=\underline'
301 cmndef +=
r'\let\$$@ff=\textrm'
303 cmndef +=
r'\let\$$@ff=\textsf'
305 cmndef +=
r'\let\$$@ff=\textsf'
307 cmndef += (
r'\def\$$@tc##1{\textcolor[rgb]{%s}{##1}}' %
310 cmndef += (
r'\def\$$@bc##1{{\setlength{\fboxsep}{\string -\fboxrule}'
311 r'\fcolorbox[rgb]{%s}{%s}{\strut ##1}}}' %
314 elif ndef[
'bgcolor']:
315 cmndef += (
r'\def\$$@bc##1{{\setlength{\fboxsep}{0pt}'
316 r'\colorbox[rgb]{%s}{\strut ##1}}}' %
326 Return the command sequences needed to define the commands
327 used to format text in the verbatim environment. ``arg`` is ignored.
331 for name, definition
in self.cmd2def.items():
332 styles.append(
r'\@namedef{%s@tok@%s}{%s}' % (cp, name, definition))
334 'styles':
'\n'.join(styles)}
338 t2n = self.ttype2name
342 realoutfile = outfile
350 (start
and ',firstnumber=%d' % start
or '') +
351 (step
and ',stepnumber=%d' % step
or ''))
354 '\\catcode`\\_=8\\relax}')
359 for ttype, value
in tokensource:
365 if start[0] != value[i]:
369 value = value[
len(start):]
373 value = start + value
381 in_math =
not in_math
382 value =
'$'.join(parts)
401 while ttype
is not Token:
408 styleval =
'+'.join(
reversed(styles))
411 for line
in spl[:-1]:
429 'iso_8859_1':
'latin1',
442 This lexer takes one lexer as argument, the lexer for the language
443 being formatted, and the left and right delimiters for escaped text.
445 First everything is scanned using the language lexer to obtain
446 strings and comments. All other consecutive tokens are merged and
447 the resulting text is scanned for escaped segments, which are given
448 the Token.Escape type. Finally text that is not escaped is scanned
449 again with the language lexer.
473 return do_insertions(insertions,
477 """ find escape tokens that are not in strings or comments """
489 """ Keep only the tokens that match `pred`, merge the others together """
506 """ Find escape tokens within text, give token=None otherwise """