Skip to content

Escape # character in matplotlibrc #19288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
janniklasrose opened this issue Jan 13, 2021 · 3 comments · Fixed by #22589
Closed

Escape # character in matplotlibrc #19288

janniklasrose opened this issue Jan 13, 2021 · 3 comments · Fixed by #22589
Milestone

Comments

@janniklasrose
Copy link

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
janniklasrose added a commit to janniklasrose/matplotlib that referenced this issue Jan 13, 2021
Fixes matplotlib#19288 

When parsing matplotlibrc files, the octothorpe ('#') indicates a comment. Escaping with '\#' allows the parameter value to contain '#'
@timhoffm
Copy link
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

@janniklasrose
Copy link
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.

@timhoffm
Copy link
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

timhoffm added a commit to timhoffm/matplotlib that referenced this issue Mar 3, 2022
This enables using the comment character # within strings.

Closes matplotlib#19288.
Superseeds matplotlib#22565.
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Mar 4, 2022
This enables using the comment character # within strings.

Closes matplotlib#19288.
Superseeds matplotlib#22565.
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Mar 4, 2022
This enables using the comment character # within strings.

Closes matplotlib#19288.
Superseeds matplotlib#22565.
@QuLogic QuLogic added this to the v3.6.0 milestone Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment