Skip to content

Conversation

Hsins
Copy link
Contributor

@Hsins Hsins commented Nov 4, 2020

This pull request is about the issue #16.

fig14

  • It's a pity that the font "Noto Serif CJK {TC, SC, JP, KR}" is not pre-installed in any operating systems. Users need to install those fonts before using the cjk-fonts style.
  • It's recommended to use backend='pgf' instead of no-latex. I got into some trouble with the no-latex option although there are no missing glyphs in the font.

Finally, the result:

@garrettj403 garrettj403 linked an issue Nov 4, 2020 that may be closed by this pull request
@garrettj403 garrettj403 merged commit ee23ea0 into garrettj403:master Nov 4, 2020
@garrettj403
Copy link
Owner

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
Copy link
Contributor Author

Hsins commented Nov 4, 2020

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.

  • Example 14: There are only characters in Traditional Chinese (電壓 and 電流) and it renders with original matplotlib instead of XeLaTeX Engine.
  • Example 15: There are characters in both Simplified Chinese (电压 and 电流), Traditional Chinese (電壓 and 電流), Japanese (電圧 and 電気) and Korean (전압 and 전류). And it renders with the XeLaTeX Engine so that should use the option backend='pgf'.

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
Copy link
Owner

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
Copy link
Contributor Author

Hsins commented Nov 4, 2020

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
Copy link
Contributor Author

Hsins commented Nov 5, 2020

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 package fontspec for XeLaTeX engine provides users to use certain fonts for rendering texts.
  • The package xeCJK for XeLaTeX engine provides users to set up different font in CJK characters and Latin characters.
    • setmainfont for Latin characters
    • setCJKmainfont for CJK characters.

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
        ]
})

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

Successfully merging this pull request may close these issues.

Is it possible to add support for CJK fonts
2 participants