Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow "real" LaTeX code for pgf.preamble in matplotlibrc #12674

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

Merged
merged 12 commits into from
Nov 11, 2018
Merged

Allow "real" LaTeX code for pgf.preamble in matplotlibrc #12674

merged 12 commits into from
Nov 11, 2018

Conversation

sxntxn
Copy link
Contributor

@sxntxn sxntxn commented Oct 30, 2018

This allows to use the rc key "pgf.preamble" to contain "real" LaTeX code. As of before this patch, commas are used as parser separators, causing the PGF backend call to latex to fail, for example at loading packages which contain options such as:
\usepackage[protrusion=true, expansion=false]{microtype}

Passing a list of strings from within a Python script works fine, and with this patch it stays this way.

Any feedback would be appreciated.

@anntzer
Copy link
Contributor

anntzer commented Oct 30, 2018

Please add an API changes note (as this could technically break rcParams that previously relied on breaking at commas). (But given that this makes possible something that was previously impossible, the change is clearly worth it.)
This should also be used for the latex preamble.
See #4371 as well.

@tacaswell
Copy link
Member

attn @pwuertz

@pwuertz
Copy link
Contributor

pwuertz commented Oct 30, 2018

In the documentation I used a string list as an example preamble. Didn't this already circumvent the problem matplotlib has/had with comma in strings? Or is this PR trying to enable single-string preambles?

pgf_with_pdflatex = {
    "pgf.texsystem": "pdflatex",
    "pgf.preamble": [
         r"\usepackage[utf8x]{inputenc}",
         r"\usepackage[T1]{fontenc}",
         r"\usepackage{cmbright}",
         ]
}
mpl.rcParams.update(pgf_with_pdflatex)

@sxntxn
Copy link
Contributor Author

sxntxn commented Oct 30, 2018

In the documentation I used a string list as an example preamble. Didn't this already circumvent the problem matplotlib has/had with comma in strings? Or is this PR trying to enable single-string preambles?

Yes, from within Python scripts. Using the RC file, LaTeX code with commas in it never actually worked.

@pwuertz
Copy link
Contributor

pwuertz commented Oct 30, 2018

Oh right, that's correct. +1
Should we update the documentation too or keep it as is for the near future? Just in case ppl might run into that problem when using the new single-string option on a non-up-to-date matplotlib.

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please apply the same change to text.latex.preamble (which suffers from the same issue). Also needs to be added in the whatsnew.
Also the description in matplotlibrc.template needs to be updated (right now it mentions splitting on commas explicitly).
Anyone can dismiss once handled.

@anntzer
Copy link
Contributor

anntzer commented Nov 4, 2018

You'll need to rebase on master too (https://matplotlib.org/devel/gitwash/development_workflow.html#rebasing-on-trunk).

sxntxn added 12 commits November 4, 2018 17:54
This allows to use the rc key "pgf.preamble" to contain "real" LaTeX
code. As of before this patch, commas are used as parser separators,
causing the PGF backend call to latex to fail, for example at loading
packages which contain options such as:
\usepackage[protrusion=true, expansion=false]{microtype}

Passing a list of strings from within a Python script works fine, and
with this patch it stays this way.
As per review by anntzer
As per review by anntzer
... to _validate_stringlist_or_string

As per review by anntzer
As per review by tacaswell
Update documentation of text.latex.preamble.
Add reference to text.latex.preamble for pgf.preamble.
Actually a copy of doc/api/next_api_changes/2018-10-30-rcparams-pgf.preamble-full-LaTeX-support.rst
... in order to also accept tuples as input, not only lists.
@sxntxn
Copy link
Contributor Author

sxntxn commented Nov 4, 2018

You'll need to rebase on master too

Let me know if that worked.

@jklymak jklymak merged commit bd7829d into matplotlib:master Nov 11, 2018
@beojan
Copy link

beojan commented Nov 14, 2018

The change applied in backend_pgf.py also needs to be applied in texmanager.py.

bcbnz added a commit to bcbnz/matplotlib-pgfutils that referenced this pull request Mar 23, 2019
The current matplotlib.rcParams validator for the preamble splits the
string on commas. The resulting list is subsequently re-joined with
newlines, causing TeX to fail when the preamble contained commas (e.g.,
key-value configuration of a package). Two pull requests switching to a
proper TeX validator have been accepted and should be included when
matplotlib 3.1.0 is released:

    * matplotlib/matplotlib#12674
    * matplotlib/matplotlib#12805

In the meantime, we can work around this by replacing the validator with
a call to str.splitlines(). The workaround is implemented in a
future-proof manner as it is only installed if the TeX validator could
not be imported, i.e., as soon as it is used with a version of
matplotlib containing the validator the workaround should be ignored.

This also adds a couple of unit tests which use the preamble to set
custom fonts. The font used is Cotham Sans, released under the SIL Open
Font License 1.1.
@ivanovmg
Copy link

Can we pass several usepackage commands in matplotlibrc file? Previously, it was possible by passing comma-separated \usepackage{package1}, \usepackage{package2} to text.latex.preamble option in matplotlibrc file. Now it appears that this is not possible anymore.

I liked the opportunity to create the same-looking graphs using the same matplotlibrc across a project. But now it seems that I need to manually edit every script and include preamble there via rcParams.

Is there still an option to use matplotlibrc for specifying preamble consisting of several lines? I probably just did not catch the idea.

@sxntxn
Copy link
Contributor Author

sxntxn commented Jul 15, 2019

Is there still an option to use matplotlibrc for specifying preamble consisting of several lines? I probably just did not catch the idea.

Just put everything in a single line:
\usepackage{package1} \usepackage{package2}

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

Successfully merging this pull request may close these issues.

7 participants