Skip to content

Commit c4ac789

Browse files
committed
Fix-up latex styling
1 parent ad5ae04 commit c4ac789

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

Tools/scripts/highlight.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,25 @@ def build_html_page(classified_text, title='python',
145145

146146
#### LaTeX Output ##########################################
147147

148-
default_latex_colors = {
149-
'comment': 'red',
150-
'string': 'green',
151-
'docstring': 'green',
152-
'keyword': 'orange',
153-
'builtin': 'purple',
154-
'definition': 'orange',
155-
'defname': 'blue',
156-
'operator': 'brown',
148+
default_latex_commands = {
149+
'comment': '{\color{red}#1}',
150+
'string': '{\color{ForestGreen}#1}',
151+
'docstring': '{\emph{\color{ForestGreen}#1}}',
152+
'keyword': '{\color{orange}#1}',
153+
'builtin': '{\color{purple}#1}',
154+
'definition': '{\color{orange}#1}',
155+
'defname': '{\color{blue}#1}',
156+
'operator': '{\color{brown}#1}',
157157
}
158158

159159
default_latex_document = r'''
160160
\documentclass{article}
161161
\usepackage{alltt}
162+
\usepackage{upquote}
162163
\usepackage{color}
163164
\usepackage[usenames,dvipsnames]{xcolor}
164165
\usepackage[cm]{fullpage}
166+
%(macros)s
165167
\begin{document}
166168
\center{\LARGE{%(title)s}}
167169
\begin{alltt}
@@ -181,17 +183,18 @@ def latex_escape(s):
181183
return re.sub(r'[\\#$%^&_{}~]', lambda mo: xlat[mo.group()], s)
182184

183185
def latex_highlight(classified_text, title = 'python',
184-
colors = default_latex_colors,
186+
commands = default_latex_commands,
185187
document = default_latex_document):
186188
'Create a complete LaTeX document with colorized source code'
189+
macros = '\n'.join(r'\newcommand{\py%s}[1]{%s}' % c for c in commands.items())
187190
result = []
188191
for kind, text in classified_text:
189192
if kind:
190-
result.append(r'{\color{%s}' % colors[kind])
193+
result.append(r'\py%s{' % kind)
191194
result.append(latex_escape(text))
192195
if kind:
193196
result.append('}')
194-
return default_latex_document % dict(title=title, body=''.join(result))
197+
return default_latex_document % dict(title=title, macros=macros, body=''.join(result))
195198

196199

197200
if __name__ == '__main__':

0 commit comments

Comments
 (0)