executablebooks / jupyter-book

Create beautiful, publication-quality books and documents from computational content.
http://jupyterbook.org
BSD 3-Clause "New" or "Revised" License
3.76k stars 652 forks source link

Unable to change pygments_style in _config.yml #2105

Open martinSDT opened 5 months ago

martinSDT commented 5 months ago

Hello all

Describe the bug

When I add to the _config.yml file

sphinx:
  recursive_update: true
  config:
    pygments_style: pygments.styles.xcode.XcodeStyle

I expect the style "xcode" to be used to highlight syntax in code blocks But instead code blocks are always colored the same way, whatever the provided pygments_style

I tried to assign a class that does not exist (pygments.styles.xcode.XcodeStyle2) and this results in an error during book building

Exception occurred:
  File "C:\Users\Martin\AppData\Roaming\Python\Python39\site-packages\sphinx\highlighting.py", line 89, in get_style
    return getattr(import_module(module), stylename)
AttributeError: module 'pygments.styles.xcode' has no attribute 'XcodeStyle2'

=> This suggests that the pygments style is somehow "read" (when an existing class is provided), but not used as I would expect.

I would like to customize the syntax highlighting and thus first need to check the possibility to change pygments_style before diving into custom lexer, custom style, etc.

Reproduce the bug

  1. Unzip the sample source code mynewbook.zip It contains a MATLAB code block in the file mynewbook/markdown.md And the option below in _config.yml
    sphinx:
    recursive_update: true
    config:
    pygments_style: pygments.styles.xcode.XcodeStyle
  2. Build the book with
    jupyter-book build mynewbook/
  3. Observe that the code highlighting is the same with and without the pygments_style option.
  4. To see the expected output, you can go to this page https://pygments.org/demo/ Select Language = Matlab Style = xcode And paste the following code under Enter some code :
    rxnList = {'PCHOLP_hs_f', 'PLA2_2_f', 'SMS_f','PCHOLP_hs_b', 'PLA2_2_b', 'SMS_b'};
    c = [1, 1, 1, 1, 1, 1]; % This is a comment
    d = 10;
    ineqSense = 'G';
    modelConstrained = constrainRxnListAboveBound(modelIrrev, rxnList, C, d, ineqSense);

    image

Thanks in advance for your help and time ! :)

List your environment

Jupyter Book : 0.15.1 External ToC : 0.3.1 MyST-Parser : 0.18.1 MyST-NB : 0.17.2 Sphinx Book Theme : 1.0.1 Jupyter-Cache : 0.6.1 NbClient : 0.7.4

Python 3.9 OS : Windows 11

welcome[bot] commented 5 months ago

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:

72757373656c6c commented 2 months ago

I see that you are using the Sphinx Book Theme which inherits from pydata. The pydata doc says "Configure pygments themes" using:

html_theme_options = {
   ...
   "pygment_light_style": "tango",
   "pygment_dark_style": "monokai"
}

And,

Danger The native Sphinx option pygments_style will be overwritten by this theme.

Any difference if you use pygment_{light,dark}_style?

I am having a similar issue with the sphinx_book_theme. Can't seem to change the default styles tango and monokai to anything else. If I use a nonexistent style, I get a message, "Highlighting style atest not found by pygments, falling back to tango.". But if I use a valid style from the output of pygmentize -L styles, there is no message but pygment style doesn't change either in the html output.

mwiesenberger commented 4 weeks ago

What works for me is

sphinx:
  config:
    html_theme_options :
      pygment_light_style : "xcode"
      pygment_dark_style : "monokai"