【问题标题】:Python Pweave to LaTeXPython Pweave 到 LaTeX
【发布时间】:2017-04-26 18:34:20
【问题描述】:

我正在尝试让Pweave 以逐字以外的形式生成 LaTeX 文件,以便我可以在文档中添加一些功能(徽标、脚注等...)。
就我喜欢 Pweave 的易用性和便利性而言,我一直无法做到。

Python 3.5.2(v3.5.2:4def2a2901a5,2016 年 6 月 26 日,10:47:25) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] 在 darwin pweave.版本是'0.25'

请问有没有人可以提供线索?

代码示例(全部在 Python Pweave 中,用于说明目的):

#' let's print
[print (i) for i in range(10)]

#' let's plot
#' we import the modules
import matplotlib.pyplot as plt
import numpy as np

#' we set the var x and y
x = np.arange(1,10,1)
y = x**2

#' we plot!
plt.plot(x,y, color= 'red')
plt.show()

谢谢!

【问题讨论】:

    标签: python python-3.x latex tex pweave


    【解决方案1】:

    我从您的代码库创建了示例源文件:一个是基于逐字的输出,另一个是使用 Minted 包进行语法突出显示,以便您看到不同之处。唯一的区别仅在于在源代码中的其他包中添加了\usepackage{minted}。

    逐字逐句:test_pweave_verbatim.texw

    \documentclass[a4paper,11pt,final]{article}
    \usepackage{fancyvrb, color, graphicx, hyperref, amsmath, url}
    \usepackage{palatino}
    \usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry}
    
    \hypersetup
    {   pdfauthor = {Name Surname},
      pdftitle={Simple test with Python and Matplotlib},
      colorlinks=TRUE,
      linkcolor=black,
      citecolor=blue,
      urlcolor=blue
    }
    
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{1.2ex}
    
    
    
    \title{Simple test with Python and Matplotlib}
    \author{Name Surname}
    \date{12nd December 2016}
    
    \begin{document}
    \maketitle
    
    \section{Introduction}
    
    Just a simple example!
    
    
    Plot stuff.
    
    <<caption="Test!">>=
    #' let's print
    [print (i) for i in range(10)]
    
    #' let's plot
    #' we import the modules
    import matplotlib.pyplot as plt
    import numpy as np
    #' we set the var x and y
    x = np.arange(1,10,1)
    y = x**2
    
    #' we plot!
    plt.plot(x,y, color= 'red')
    plt.show()
    @
    
    \section{End}
    
    A simple end.
    
    \end{document}
    

    使用 Minted 进行语法高亮:test_pweave_minted.texw

    \documentclass[a4paper,11pt,final]{article}
    \usepackage{fancyvrb, color, graphicx, hyperref, amsmath, url}
    \usepackage{minted}
    \usepackage{palatino}
    \usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry}
    
    \hypersetup
    {   pdfauthor = {Name Surname},
      pdftitle={Simple test with Python and Matplotlib},
      colorlinks=TRUE,
      linkcolor=black,
      citecolor=blue,
      urlcolor=blue
    }
    
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{1.2ex}
    
    
    
    \title{Simple test with Python and Matplotlib}
    \author{Name Surname}
    \date{12nd December 2016}
    
    \begin{document}
    \maketitle
    
    \section{Introduction}
    
    Just a simple example!
    
    
    Plot stuff.
    
    <<caption="Test!">>=
    #' let's print
    [print (i) for i in range(10)]
    
    #' let's plot
    #' we import the modules
    import matplotlib.pyplot as plt
    import numpy as np
    #' we set the var x and y
    x = np.arange(1,10,1)
    y = x**2
    
    #' we plot!
    plt.plot(x,y, color= 'red')
    plt.show()
    @
    
    \section{End}
    
    A simple end.
    
    \end{document}
    

    现在,使用以下命令生成 pdf 文件:

    1. 逐字逐句

      • pweave -f tex test_pweave_verbatim.texw
      • pdflatex test_pweave_verbatim.tex
    2. 铸造

      • pweave -f texminted test_pweave_minted.texw
      • pdflatex -shell-escape test_pweave_minted.tex

    使用 Python 2.7.10 和 Pweave 0.25 在 OSX 10.11.4 中测试。

    【讨论】:

    • 谢谢,不幸的是我无法让它与结果一起工作。我的主要问题是使用 weave('file path') 运行我的文件会给出一个 html,但我需要一个 tex 文件,以便我可以进一步处理它,例如添加其他文件、部分和子部分编号、徽标等。作为旁注,我也尝试使用 emacs 从 org-mod 生成,但我发现它的可预测性不够。另外,我喜欢能够直接从 IDE 使用 python,而不是处理会话和长文件。据我所知,似乎 org-mode 不会读取 weave。
    • 您的文件是否有 .texw 扩展名?
    • 您是否尝试运行以下命令来生成 tex 文件:pweave -f tex yourfile.texw
    • 现在我想我明白我的问题是什么了,感谢您和我在一起:我需要从我的 python 代码创建 .texw 文件。我怎么去那里?
    • 正如您在我的示例中看到的,.texw 文件只是一个 .tex 文件,您可以在其中插入一些代码。
    猜你喜欢
    • 2023-04-08
    • 2017-08-14
    • 2012-04-18
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2017-03-08
    • 2019-08-08
    相关资源
    最近更新 更多