3 回答

TA貢獻1752條經驗 獲得超4個贊
這是我寫的一個小腳本。它拆分單個*.ipynb
文件并將其轉換為多個*.tex
文件。
用法是:
復制以下腳本并另存為
main.py
執行
python main.py init
。它將創建main.tex
和style_ipython_custom.tplx
在您的 jupyther 筆記本中,向您要提取的每個單元格添加額外的行
#latex:tag_a
,#latex:tag_b
, .. 。相同的標簽將被提取到相同的*.tex
文件。將其保存為
*.ipynb
文件。幸運的是,目前VSCode蟒蛇插件支持出口到*.ipynb
從,或使用jupytext轉換*.py
到*.ipynb
。運行
python main.py path/to/your.ipynb
,它將創建tag_a.tex
和tag_b.tex
編輯
main.tex
和添加\input{tag_a.tex}
或\input{tag_b.tex}
任何你想要的地方。運行
pdflatex main.tex
它會產生main.pdf
這個腳本背后的想法:
使用默認值從 jupyter notebook 轉換為 LaTexnbconvert.LatexExporter
會生成包含宏定義的完整 LaTex 文件。使用它來轉換每個單元格可能會創建大型 LaTex 文件。為避免該問題,腳本首先創建main.tex
只有宏定義的單元格,然后將每個單元格轉換為沒有宏定義的 LaTex 文件。這可以使用自定義模板文件來完成,該文件從style_ipython.tplx
標記或標記單元格可能使用單元格元數據完成,但我找不到如何在 VSCode python 插件(問題)中設置它,因此它使用正則表達式模式掃描每個單元格的源^#latex:(.*)
,并在將其轉換為 LaTex 文件之前將其刪除.
來源:
import sys
import re
import os
from collections import defaultdict
import nbformat
from nbconvert import LatexExporter, exporters
OUTPUT_FILES_DIR = './images'
CUSTOM_TEMPLATE = 'style_ipython_custom.tplx'
MAIN_TEX = 'main.tex'
def create_main():
# creates `main.tex` which only has macro definition
latex_exporter = LatexExporter()
book = nbformat.v4.new_notebook()
book.cells.append(
nbformat.v4.new_raw_cell(r'\input{__your_input__here.tex}'))
(body, _) = latex_exporter.from_notebook_node(book)
with open(MAIN_TEX, 'x') as fout:
fout.write(body)
print("created:", MAIN_TEX)
def init():
create_main()
latex_exporter = LatexExporter()
# copy `style_ipython.tplx` in `nbconvert.exporters` module to current directory,
# and modify it so that it does not contain macro definition
tmpl_path = os.path.join(
os.path.dirname(exporters.__file__),
latex_exporter.default_template_path)
src = os.path.join(tmpl_path, 'style_ipython.tplx')
target = CUSTOM_TEMPLATE
with open(src) as fsrc:
with open(target, 'w') as ftarget:
for line in fsrc:
# replace the line so than it does not contain macro definition
if line == "((*- extends 'base.tplx' -*))\n":
line = "((*- extends 'document_contents.tplx' -*))\n"
ftarget.write(line)
print("created:", CUSTOM_TEMPLATE)
def group_cells(note):
# scan the cell source for tag with regexp `^#latex:(.*)`
# if sames tags are found group it to same list
pattern = re.compile(r'^#latex:(.*?)$(\n?)', re.M)
group = defaultdict(list)
for num, cell in enumerate(note.cells):
m = pattern.search(cell.source)
if m:
tag = m.group(1).strip()
# remove the line which contains tag
cell.source = cell.source[:m.start(0)] + cell.source[m.end(0):]
group[tag].append(cell)
else:
print("tag not found in cell number {}. ignore".format(num + 1))
return group
def doit():
with open(sys.argv[1]) as f:
note = nbformat.read(f, as_version=4)
group = group_cells(note)
latex_exporter = LatexExporter()
# use the template which does not contain LaTex macro definition
latex_exporter.template_file = CUSTOM_TEMPLATE
try:
os.mkdir(OUTPUT_FILES_DIR)
except FileExistsError:
pass
for (tag, g) in group.items():
book = nbformat.v4.new_notebook()
book.cells.extend(g)
# unique_key will be prefix of image
(body, resources) = latex_exporter.from_notebook_node(
book,
resources={
'output_files_dir': OUTPUT_FILES_DIR,
'unique_key': tag
})
ofile = tag + '.tex'
with open(ofile, 'w') as fout:
fout.write(body)
print("created:", ofile)
# the image data which is embedded as base64 in notebook
# will be decoded and returned in `resources`, so write it to file
for filename, data in resources.get('outputs', {}).items():
with open(filename, 'wb') as fres:
fres.write(data)
print("created:", filename)
if len(sys.argv) <= 1:
print("USAGE: this_script [init|yourfile.ipynb]")
elif sys.argv[1] == "init":
init()
else:
doit()

TA貢獻1946條經驗 獲得超3個贊
我會使用bookdown在同一個文檔中同時包含測試和源代碼(為了方便起見,分成幾個文件)。這個包起源于 R 世界,但也可以與其他語言一起使用。這是一個非常簡單的例子:
---
output: bookdown::pdf_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Setup data
First we define some varialbes with data.
```{python data}
bob = ['Bob Smith', 42, 30000, 'software']
sue = ['Sue Jones', 45, 40000, 'music']
```
# Output data
then we output some of the data.
```{python output}
bob[0], sue[2]
```
# Reference code block
Finally lets repeate the code block without evaluating it.
```{python, ref.label="output", eval = FALSE}
```
輸出:

TA貢獻1828條經驗 獲得超4個贊
Jupyter 不允許從筆記本導出特定單元格——它只允許您導出整個筆記本。為了盡可能接近您的理想場景,您需要一個模塊化的 Jupyter 設置:
將您的單個 Jupyter 筆記本拆分為更小的筆記本。
然后可以通過文件 > 下載為 > LaTeX (.tex) 將每個筆記本導出到 LaTeX
在 LaTeX 中,您可以通過以下方式導入生成的 .tex 文件
\input{文件名.tex}
如果您想將較小的筆記本導入主筆記本的單元格中,您可以通過(請參閱魔術命令運行)
%run my_other_notebook.ipynb #or %run 'my notebook with spaces.ipynb'
您還可以通過(請參閱magic command load)插入python文件
%load python_file.py
它加載 Python 文件并允許您在主筆記本中執行它。
您還可以擁有小的 .py 片段,將它們加載到您的小型 Jupyter 筆記本中,然后在較大的筆記本中運行該小型筆記本。
你對 VS Code 的使用很好,不過,瀏覽器中的 Jupyter 可能會讓你編輯得更快。
添加回答
舉報