Windows + Jupyter notebook + miktex + pandoc 輸出 pdf

來源:互聯網
上載者:User

標籤:ros   es2017   line   string   更新   sys.argv   ntc   odi   ges   

1.安裝miktex1.下載安裝 MikTeX2.更新 Maintenance (Admin) -> MiKTeX Update (Admin)3.配置中文

Maintenance (Admin) -> Settings (Admin) -> 選擇 Packages 標籤 -> 選擇 "Language Support" 底下的選項 "Chinese,Japanese, Korean" -> 應用

 接下來就是開啟命令視窗(以管理員身份執行)"

initexmf -u

 

initexmf --edit-config-file updmap

此時會出現一個筆記本,我們就要輸入要啟用的字型了:

%% T1 font configuration settings. See the MiKTeX manual for help.Map cwmu.mapMap cwku.mapMap cwfsu.mapMap cwhbu.mapMap cwyu.map

儲存,關閉,接著在控制台輸入命令:

initexmf --mkmaps

  然後就又是等待,命令完成後,就大功告成了

2.安裝pandoc

用文字編輯器開啟article.tplx (\Python27\Lib\site-packages\nbconvert\templates\latex\),修改為

\documentclass{ctexart}

 

3.try1.命令列試試

將ipynb編譯為tex

ipython nbconvert --to latex Example.ipynb

編譯tex,產生pdf

xelatex Example.tex

2.Jupyter notebook試試

3.換個姿勢再來一次

ipynb2pdf.py

# coding:utf-8import sysimport osimport reimport shutilnotebook = sys.argv[1]texFile = notebook.replace(‘.ipynb‘,‘.tex‘)# 1.convert .ipynb to latex file .tex# 將ipynb檔案轉為tex檔案print ‘1. convert ‘ + notebook + ‘ to ‘ + texFile  print ‘------ \n‘os.system(r‘jupyter nbconvert --to latex ‘ + notebook)print ‘convert over‘# 2. add Chinese support by adding the string below# 加入引用的包使支援中文(直接轉換中文會丟失)# \usepackage{fontspec, xunicode, xltxtra}# \setmainfont{Microsoft YaHei} # \usepackage{ctex}print ‘2. add Chinese support to .tex file‘ print ‘------‘file = open(texFile,‘r‘)str_file = file.read()strinfo = re.compile(‘(documentclass[\d\D]+\{article\})‘)  #尋找的字元line0m=re.findall(strinfo,str_file)if len(m) == 0:    print r‘can not find documentclass[**pt]{article}‘    sys.exit(1)str_file = strinfo.sub(‘\\1 \n \\usepackage{fontspec, xunicode, xltxtra} \n \\setmainfont{Microsoft YaHei} \r \\usepackage{ctex}‘,str_file)  #  替換的字元line1file.close()file = open(texFile,‘w‘)file.write(str_file)file.close()print ‘add Chinese support successed‘# 3. convert .tex to .pdf by xelatex# 使用xelatex命令編譯.tex檔案得到pdfprint  ‘3. convert tex to pdf‘print ‘------‘os.system(‘xelatex ‘ + texFile)print  ‘convert pdf successed‘# 4. delete the auxiliary files# 清理產生的中間檔案# change there if latex file is neededprint ‘4. delete auxiliary files‘print ‘------‘os.remove(notebook.replace(‘.ipynb‘,‘.aux‘))os.remove(notebook.replace(‘.ipynb‘,‘.log‘))os.remove(notebook.replace(‘.ipynb‘,‘.out‘))# change there if latex file is neededos.remove(notebook.replace(‘.ipynb‘,‘.tex‘))if os.path.isdir(notebook.replace(‘.ipynb‘,‘_files‘)):    shutil.rmtree(notebook.replace(‘.ipynb‘,‘_files‘))print ‘delete auxiliary files successed‘
View Code

 

python ipynb2pdf.py Example.ipynb

也轉換成可執行檔後使用

pip install pyinstaller 

 

pyinstaller -F ipynb2pdf.py 

 

ipynb2pdf.exe Example.ipynb

Windows + Jupyter notebook + miktex + pandoc 輸出 pdf

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.