Skip to content

Escape # character in matplotlibrc #19288

Closed

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

  1. timhoffm commented on Jan 13, 2021

    @timhoffm
    Member

    I'd rather not expand the matplotlibrc syntax with escaping. Escaping is not very user friendly.

    Instead, I propose to add quoted strings as proposed in #15813 (comment). Then you could do:

    text.latex.preamble: "\newcommand{\foo}[1]{\bar{\#1}}" # add foo as shorthand for bar
    
  2. janniklasrose commented on Jan 14, 2021

    @janniklasrose
    Author

    That sounds reasonable! Is that support for quoted strings being worked on?

    I can't find a (draft) PR for it, but I did find this now stale PR #9528 for a MEP32 from 3 years ago, which discusses different parsers/formats for matplotlibrc files.

  3. timhoffm commented on Jan 22, 2021

    @timhoffm
    Member

    Currently, nobody is working on any of these. #9528 was discussed controversially and stalled at some point.

    From the likelihood of getting this forward, I'd estimate:

    quoted string support > YAML config > python-based config

  4. added 3 commits that reference this issue on Mar 3, 2022
    88b4a0d
    fc41ba4
    7c378a8
  5. added this to the v3.6.0 milestone on Mar 7, 2022
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