Skip to content
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

Can not add lang zh-CN when convert md to pdf by xelatex #7945

Closed
humbinal opened this issue Mar 2, 2022 · 8 comments
Closed

Can not add lang zh-CN when convert md to pdf by xelatex #7945

humbinal opened this issue Mar 2, 2022 · 8 comments
Labels

Comments

@humbinal
Copy link

humbinal commented Mar 2, 2022

pandoc version: 2.17.1.1

Steps to reproduce:

this works well:

doc: test.md

---
title: 文档标题
abstract: 摘要信息
mainfont: Arial
CJKmainfont: Microsoft YaHei
---

hello,你好

cmd:

pandoc --pdf-engine=xelatex test.md -o test.pdf

the output pdf:

2022-03-02_205546

Error condition:

when I add lang: zh-CN (or similarly lang: zh-HK, lang: zh-Hans, lang: zh-Hant) as follows:

---
title: 文档标题
abstract: 摘要信息
mainfont: Arial
CJKmainfont: Microsoft YaHei
lang: zh-CN
---

hello,你好

It fails:

$ pandoc --pdf-engine=xelatex test.md -o test.pdf
Error producing PDF.
! Package babel Error: There is no ini file for the requested language
(babel)                (: ). Perhaps you misspelled it or your
(babel)                installation is not complete..

See the babel package documentation for explanation.
Type  H <return>  for immediate help.
 ...

l.63 \babelprovide[main,import]{}

It looks like pandoc unable to set correct language parameters to babel, I wonder how to set them correctly!

I test this on windows10 and windows7 with latest version of MiKTeX and TeX Live , the errors were consistent.

pandoc version: 2.17.1.1
tex version: MiKTeX-TeX 4.4 (MiKTeX 22.1)
latex version: MiKTeX-pdfTeX 4.10 (MiKTeX 22.1)
xelatex version: MiKTeX-XeTeX 4.7.1 (MiKTeX 22.1)

I really need to set the language to Chinese for abstract title or figure legend ...

Thanks to anyone for helping me !

@humbinal humbinal added the bug label Mar 2, 2022
@jgm
Copy link
Owner

jgm commented Mar 2, 2022

I think that this change to the default latex template should do the trick:

diff --git a/data/templates/default.latex b/data/templates/default.latex
index 3874813c7..438f84690 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -368,7 +368,9 @@ $if(lang)$
 \else
 \usepackage[bidi=default]{babel}
 \fi
+$if(babel-lang)$
 \babelprovide[main,import]{$babel-lang$}
+$endif$
 $for(babel-otherlangs)$
 \babelprovide[import]{$babel-otherlangs$}
 $endfor$

@jgm
Copy link
Owner

jgm commented Mar 2, 2022

Note: this change will bypass babel for Chinese, relying only on xeCJK.
I suspect you want the babel things like the Chinese localization of "abstract," so let me investigate further.

jgm added a commit that referenced this issue Mar 2, 2022
@jgm
Copy link
Owner

jgm commented Mar 2, 2022

When I change the code so that

\babelprovide[main,import]{chinese}

is generated, the document compiles but we get "?abstract?" instead of a Chinese localization. Not sure why.

@jgm
Copy link
Owner

jgm commented Mar 2, 2022

One way to get the localization of "Abstract" is to use the ctexart documentclass instead of article. Add this to your pandoc invocation:

-Vdocumentclass=ctexart

@humbinal
Copy link
Author

humbinal commented Mar 3, 2022

One way to get the localization of "Abstract" is to use the ctexart documentclass instead of article. Add this to your pandoc invocation:

-Vdocumentclass=ctexart

@jgm thanks, I only add documentclass config as follows, it works

doc: test.md

---
title: 文档标题
abstract: 摘要信息
mainfont: Arial
CJKmainfont: Microsoft YaHei
documentclass: ctexart
---

hello,你好

cmd:

pandoc --pdf-engine=xelatex test.md -o test.pdf

I got the correct Chinese localization of "Abstract", "Figure" and so on.

But I don't know how that works, I don't tell pandoc anything of which language need to use,it output Chinese automatically ?

For example, I want to output a mix of Chinese Simplified and Chinese Traditional document,“Abstract“” localization us zh-Hant(Chinese Traditional), and how to configure the markdown metadata ?

@jgm
Copy link
Owner

jgm commented Mar 3, 2022

But I don't know how that works, I don't tell pandoc anything of which language need to use,it output Chinese automatically ?

The default template includes

$endif$
$if(CJKmainfont)$
  \ifXeTeX
    \usepackage{xeCJK}
    \setCJKmainfont[$for(CJKoptions)$$CJKoptions$$sep$,$endfor$]{$CJKmainfont$}
  \fi
$endif$

so it is detecting CJKmainfont and loading xeCJK for that reason. Maybe that package can figure out the language by looking at the characters? I'm not sure, actually. Maybe looking at the documentation for xeCJK https://ctan.org/pkg/xecjk will reveal something you could pass in with CJKoptions?

Oddly, Chinese + its variants seems left out of the mappings defined in Text.Pandoc.LaTeX.Lang. Is there a reason? Well, when I tried above to add Chinese manually, I didn't get the right localized abstract, so maybe someone figured out that it works best just to include xeCJK without using babel?

This is something I'm really ignorant about: could anyone out there who is familiar with babel and Asian languages help out?
@tstenner @alerque @ickc @mb21 ?

@ickc
Copy link
Contributor

ickc commented Mar 3, 2022

From my limited experience, CJK is in practice handled very differently in LaTeX comparing to other languages. The ctexart is I believe from the CTeX package. CTeX specialize in producing Chinese documents (so not general CJK) but it seems it is what you're after. I recommend reading the CTeX documentation for their recommended practices (and adapt to pandoc using the template.)

For the question

But I don't know how that works, I don't tell pandoc anything of which language need to use,it output Chinese automatically ?

It is because ctex is specialized in Chinese documents so by choosing the document class you already inform what language you are after.

@humbinal
Copy link
Author

Thank‘s for everyone, After your guidance, I know ctex , babel, ctexart ...
Now, I use documentclass: ctexart or lang: zh-Hans and babel-lang: chinese-simplified with pandoc metadata, it all works well!

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

No branches or pull requests

3 participants