亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

編寫充滿 Python 代碼的 Latex 書的困難工作流程

編寫充滿 Python 代碼的 Latex 書的困難工作流程

DIEA 2021-11-30 10:39:51
我正在寫一本關于使用 Latex 在 python 中編碼的書。我計劃將大量帶有 Python 代碼的文本及其輸出散布在整個文本中。真正給我帶來麻煩的是,當我需要返回并編輯我的 python 代碼時,將它很好地恢復到我的最新文檔中是一種巨大的痛苦。我做了大量的研究,似乎找不到一個好的解決方案。這個包括完整的文件,不能解決我的問題 https://tex.stackexchange.com/questions/289385/workflow-for-include-jupyter-aka-ipython-notebooks-as-pages-in-a-乳膠文檔和這個一樣。 http://blog.juliusschulz.de/blog/ultimate-ipython-notebook找到解決方案1(糟糕)我可以使用列表乳膠包將 python 代碼復制并粘貼到乳膠中。優點:易于更新僅一小部分代碼。缺點:對于需要在python中運行的輸出,分別復制、粘貼。初寫SLOW,每章需要做幾百遍這個過程。找到解決方案 2(壞)使用帶有 markdown 的 jupyter notebook,導出到 Latex,\include 文件到主 Latex 文檔中。優點:流線型包含輸出。缺點:要進行小的更改,需要重新導入整個文檔,Latex 編輯器中對 Markdown 文本所做的任何更改都不會保存在 jupyter notebook 之后重命名 python 中的單個變量可能需要幾個小時。編輯似乎是一項艱巨的任務。理想的解決方案在 Latex 中寫入文本在jupyter notebook中寫python,導出為latex。以某種方式將代碼片段(導出文件的小部分)包含到主要乳膠書的不同部分中。這是我想不通的部分當需要更改python時,在jupyter中進行更改,然后重新導出為同名的latex文件Latex 書自動從包含更新。這里的關鍵是導出的 python notebook 被拆分并發送到文檔的不同部分。為了讓它起作用,它需要以某種方式在筆記本的降價或代碼中進行標記或標記,因此當我重新導出它時,這些相同的部分會被發送到書中的相同位置。優點:Python 編輯容易,易于傳播回書。用乳膠書寫的文字,可以使用乳膠的力量任何幫助提出更接近我的理想解決方案的解決方案將不勝感激。這太痛苦了??赡軣o關緊要,但我在 VS Code 中同時編寫了 Latex 和 jupyter 筆記本。如果這意味著解決這些問題,我愿意改變工具。
查看完整描述

3 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

這是我寫的一個小腳本。它拆分單個*.ipynb文件并將其轉換為多個*.tex文件。

用法是:

  1. 復制以下腳本并另存為 main.py

  2. 執行python main.py init。它將創建main.texstyle_ipython_custom.tplx

  3. 在您的 jupyther 筆記本中,向您要提取的每個單元格添加額外的行#latex:tag_a#latex:tag_b, .. 。相同的標簽將被提取到相同的*.tex文件。

  4. 將其保存為*.ipynb文件。幸運的是,目前VSCode蟒蛇插件支持出口到*.ipynb從,或使用jupytext轉換*.py*.ipynb。

  5. 運行python main.py path/to/your.ipynb,它將創建tag_a.textag_b.tex

  6. 編輯main.tex和添加\input{tag_a.tex}\input{tag_b.tex}任何你想要的地方。

  7. 運行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()


查看完整回答
反對 回復 2021-11-30
?
智慧大石

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}

```

輸出:

http://img1.sycdn.imooc.com//61a58f200001f8b505920363.jpg

查看完整回答
反對 回復 2021-11-30
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

Jupyter 不允許從筆記本導出特定單元格——它只允許您導出整個筆記本。為了盡可能接近您的理想場景,您需要一個模塊化的 Jupyter 設置:

  1. 將您的單個 Jupyter 筆記本拆分為更小的筆記本。

  2. 然后可以通過文件 > 下載為 > LaTeX (.tex) 將每個筆記本導出到 LaTeX

  3. 在 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 可能會讓你編輯得更快。


查看完整回答
反對 回復 2021-11-30
  • 3 回答
  • 0 關注
  • 292 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號