garrettj403 / SciencePlots

Matplotlib styles for scientific plotting
MIT License
7.12k stars 709 forks source link

Add style to support CJK fonts. #29

Closed Hsins closed 4 years ago

Hsins commented 4 years ago

This pull request is about the issue https://github.com/garrettj403/SciencePlots/issues/16.

fig14

Finally, the result:

garrettj403 commented 4 years ago

Hi @Hsins

Thank you very much for your contribution. Following your instructions in README, I was able to install the CJK fonts and run example 14. (I haven't tried example 15 yet.)

I'll create a new release of SciencePlots soon to add your changes to the PyPI version.

Hsins commented 4 years ago

Hi @garrettj403

It's really glad to hear that you successfully run example 14 with that instructions. :)


By the way, the little trouble I mentioned is about the difference between example 14 and example 15.

Although the Example 15 works fine in my machine (as you see the result I uploaded that fig15.pdf before). It always gives the error with the message UnicodeEncodeError: 'utf-16-be' codec can't encode character '\ud98a' in position 51204: surrogates not allowed when I try to use the same texts with 电压電壓電圧전압 and 电流電流電気전류 in Example 14.

It seems that there was not a missing glyphs problem because Example 15 works fine and the Guidelines for Using Noto page said each font supports all four languages:

It is recommended to use "Noto Sans CJK {JP, KR, SC, TC}" rather than "Noto Sans {JP, KR, SC, TC}", unless space/latency is very restricted. Each of the font families "Noto Sans CJK {JP, KR, SC, TC}" supports all four languages, but has a different default language. Learn more about Noto CJK

So I'm sorry to say that I can't guarantee the cjk-fonts style would work fine in Japanese, Korean, or even Simplified Chinese...

garrettj403 commented 3 years ago

Although the Example 15 works fine in my machine (as you see the result I uploaded that fig15.pdf before). It always gives the error with the message UnicodeEncodeError: 'utf-16-be' codec can't encode character '\ud98a' in position 51204: surrogates not allowed when I try to use the same texts with 电压電壓電圧전압 and 电流電流電気전류 in Example 14.

I get this error as well when I include the Korean characters. Example 14 works for me with the TC/SC/JP characters.

I can run example 15, but the Korean characters don't render (they just appear as boxes) and I have to use your PGF preamble:

import matplotlib
matplotlib.use('pgf')  # switch backend to pgf
matplotlib.rcParams.update({
    "pgf.preamble": [
        "\\usepackage{fontspec}",
        '\\usepackage{xeCJK}',
        r'\setmainfont{Times New Roman}',  # EN fonts Romans
        r'\setCJKmainfont{SimHei}',  # set CJK fonts as SimSun
        r'\setCJKsansfont{SimHei}',
        r'\newCJKfontfamily{\Song}{SimSun}',
        ]
})
Hsins commented 3 years ago

I get this error as well when I include the Korean characters. Example 14 works for me with the TC/SC/JP characters.

I can run example 15, but the Korean characters don't render (they just appear as boxes) and I have to use your PGF preamble:

import matplotlib
matplotlib.use('pgf')  # switch backend to pgf
matplotlib.rcParams.update({
    "pgf.preamble": [
        "\\usepackage{fontspec}",
        '\\usepackage{xeCJK}',
        r'\setmainfont{Times New Roman}',  # EN fonts Romans
        r'\setCJKmainfont{SimHei}',  # set CJK fonts as SimSun
        r'\setCJKsansfont{SimHei}',
        r'\newCJKfontfamily{\Song}{SimSun}',
        ]
})

I don't think it's a problem with the setting of style params but the problem with the font file or even the LaTeX Engine. So just leave the issue for anyone who needs to use Korean in their plots. 😂😂😂😂😂

Hsins commented 3 years ago

I can run example 15, but the Korean characters don't render (they just appear as boxes) and I have to use your PGF preamble:

import matplotlib
matplotlib.use('pgf')  # switch backend to pgf
matplotlib.rcParams.update({
    "pgf.preamble": [
        "\\usepackage{fontspec}",
        '\\usepackage{xeCJK}',
        r'\setmainfont{Times New Roman}',  # EN fonts Romans
        r'\setCJKmainfont{SimHei}',  # set CJK fonts as SimSun
        r'\setCJKsansfont{SimHei}',
        r'\newCJKfontfamily{\Song}{SimSun}',
        ]
})

I forgot to explain what the PGF preamble does.

The fonts are given to setmainfont and setCJKmainfont can be a font-family name of the system fonts or a path of certain *.ttf/*.otf font file. If you follow the instructions I wrote in the FAQ section (the Noto {Sans/Serif} CJK {TC/SC/JP/KR} fonts would be installed to your machine), you should use the PGF preamble below:

matplotlib.rcParams.update({
    "pgf.preamble": [
        "\\usepackage{fontspec}",
        '\\usepackage{xeCJK}',
        r'\setmainfont{Times New Roman}',  # EN fonts Romans
        r'\setCJKmainfont{Noto Serif CJK TC}',  # set CJK fonts as Noto Serif CJK TC
        ]
})