Escape # character in matplotlibrc · Issue #19288 · matplotlib/matplotlib · GitHub
Skip to content

Escape # character in matplotlibrc #19288

Description

@janniklasrose

Problem

It currently seems impossible to escape the # character inside matplotlibrc files. I would like to include it as part of the parameter value for text.latex.preamble to add a LaTeX command definition such as \newcommand{\foo}[1]{\bar{#1}}.

When reading the file, each line is split at the # character and everything after it assumed as comment and thus discarded.

Proposed Solution

One solution would be to replace the line

strippedline = line.split('#', 1)[0].strip()

with a function that escapes # character(s). It would look something like this:

def strip_comment_with_escaped_octothorpe(line):
    splitline = line.split('#')
    escaped_line = []
    for i in range(len(splitline)):
        segment = splitline[i]
        if segment[-1] is '\':  # the character was escaped with '\#'
            segment = segment[:-1]  # remove the escape character '\'
        escaped_line.append(segment)
    if splitline[-2][-1] is '\':  # the last segment was an actual comment
        escaped_line = escaped_line[:-1]
    return ''.join(escaped_line)  # join back

strippedline = strip_comment_with_escaped_octothorpe(line).strip()

Additional context and prior art

Potential other use cases:

  • text.latex.preamble and pgf.preamble: Not just limited to \newcommand's #1
  • savefig.directory: File paths might contain #
  • keymap: the # key cannot be remapped currently

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions