Clone wiki

pygments-main / Text Color Ignored

Text Color Ignored

This is a bug fix for Issue #897. Knowledge of the issue may be useful to understanding this page.

Some Tokens are not styled by the LaTeX formatter correctly, due to a shortcoming in the way the formatter uses Token shortnames when outputting style definitions.

Consider: PY@tok@ is the style for both Token.Text and Token. The style definition must be set via \PY{}{token}, which is never called on any token. Tokens without styles are simply output, as this avoids overzealous use of the \PY command.

Moreover: Many of the default styles define Token.Text, but not Token, or vice versa. In the monokai style, for example, this leads Generic tokens to be colored incorrectly.

One could add a special case to _structure_tokens_to_lines() to always apply the \PY command, as long as there is a token (not the case with spaces and newlines), but this does nothing to solve the second problem.

An easier way to solve both issues is to globally change the text color of the Verbatim environment to the text color of Token.Text, or if that does not exist, to Token. This may now be achieved via the applytxtcolor boolean option.

However, this causes any line numbers produced by fancyvrb to be colored as well, which may not be desirable (with monokai, for example, the line numbers become white if applytxtcolor is True). To mitigate this problem, the linenocolor option has been created: if set to a 6-digit RBG hex value, then line numbers are colored accordingly, regardless of any other option.

Both of these new options piggy-back on the method used by applybgcolor. A new environment is created to style the text or line number color, and is then called directly around the Verbatim environment.

Please note that only one environment is ever defined, even if 2 or 3 of the options are enabled; the environment is carefully constructed so that any of the options may be used independently of one another.

Finally, a thank you to Gonzalo Medina on TeX StackExchange for providing the necessary code to change the coloring of line numbers.

Example Usages: ( may be found here)

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import LatexFormatter

lexer = PythonLexer()
formatter = LatexFormatter(full=True,
                           title='Text and Lineno Coloring',

with open('','r') as inputFile:
    code =

with open("test.tex","w") as outputFile:
    highlight(code, lexer, formatter, outputFile)