Pythontex:尝试编译我在 TeX 上找到的一个示例

Pythontex:尝试编译我在 TeX 上找到的一个示例

我找到了一个简单的例子这里G. Poore 回答了这个问题,但我无法让它工作。pdf 的输出是

?? PythonTeX ??
?? PythonTeX ??

我遗漏了什么?下面您将找到.tex、、和我的.py文件。.pytxcode.latexmkrc

test.tex文件是:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[makestderr]{pythontex}

\newenvironment{pyconcodeblock}%
{\VerbatimEnvironment
  \begin{VerbatimOut}{test.py}}%
  {\end{VerbatimOut}%
  \pyconc{exec(compile(open('test.py', 'rb').read(), 'test.py', 'exec'))}%
  \inputpygments{python}{test.py}}

\begin{document}

\begin{pyconcodeblock}
import os
import sys
sys.path.append(os.getcwd())

def foo(x):
  return 2*x
\end{pyconcodeblock}

\begin{pyconsole}
x = 10
foo(x)
\end{pyconsole}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:

哈维·穆德,我找到了以下条目并Latexmk添加了它:

$pdflatex='pdflatex --shell-escape -interaction=nonstopmode %O %S 
-file-line-error -synctex=1';

# This shows how to use the pythontex package with latexmk

#  This version has a fudge on the latex and pdflatex commands that
#  allows the pythontex custom dependency to work even when $out_dir
#  is used to set the output directory.  Without the fudge (done by
#  trickery symbolic links) the custom dependency for using pythontex
#  will not be detected.

add_cus_dep('pytxcode', 'tex', 0, 'pythontex');
sub pythontex {
# This subroutine is a fudge, because it from latexmk's point of
# view, it makes the main .tex file depend on the .pytxcode file.
# But it doesn't actually make the .tex file, but is used for its
# side effects in creating other files.  The dependence is a way
# of triggering the rule to be run whenever the .pytxcode file
# changes, and to do this before running latex/pdflatex again.
return system("pythontex.py \"$_[0]\"") ;
}


$pdflatex = 'internal mylatex %R %Z pdflatex %O %S';
$latex = 'internal mylatex %R %Z latex %O %S';
sub mylatex {
my $root = shift;
my $dir_string = shift;
my $code = "$root.pytxcode";
my $result = "pythontex-files-$root";
if ($dir_string) {
warn "mylatex: Making symlinks to fool cus_dep creation\n";
unlink $code;
if (-l $result) {
unlink $result;
}
elsif (-d $result) {
unlink glob "$result/*";
rmdir $result;
}
symlink $dir_string.$code, $code;
if ( ! -e $dir_string.$result ) { mkdir $dir_string.$result; }
symlink $dir_string.$result, $result;
}
else {
foreach ($code, $result) { if (-l) { unlink; } }
}
return system @_;
}

test.py文件包含:

import os
import sys
sys.path.append(os.getcwd())

def foo(x):                                                                     
  return 2*x  

我也尝试过不使用import osimport syssys.path.append(os.getcwd())。然后test.pytxcode包含:

=>PYTHONTEX#pycon#default#default#0#c#####22#
exec(compile(open('test.py', 'rb').read(), 'test.py', 'exec'))
=>PYTHONTEX#PYGpython#EXT:test.py#defaultverb#0#verbatim#####22#
=>PYTHONTEX#pycon#default#default#1#console#####24#
x = 10
foo(x)
=>PYTHONTEX:SETTINGS#
version=0.14
outputdir=pythontex-files-test
workingdir=.
workingdirset=false
gobble=none
rerun=default
hashdependencies=default
makestderr=true
stderrfilename=full
keeptemps=none
pyfuture=default
pyconfuture=none
pygments=true
pygglobal=:GLOBAL||
fvextfile=-1
pyconbanner=none
pyconfilename=stdin
depythontex=false
pygfamily=py|python|
pygfamily=pycon|pycon|
pygfamily=sympy|python|
pygfamily=sympycon|pycon|
pygfamily=pylab|python|
pygfamily=pylabcon|pycon|
pygfamily=PYGpython|python|

答案1

如果 PythonTeX 给出?? PythonTeX ????,则表明它尚未运行,因此没有生成任何内容。

在 Windows 下,您的.latexmkrc方法对我不起作用。我不得不更改该行return system("pythontex.py \"$_[0]\"") ;return system("pythontex \"$_[0]\"") ;提供我的系统配置。之后,它就起作用了。

如果您使用的是 Windows,那么这可能会修复此问题。否则,您需要阅读输出latexmk,并查看 PythonTeX 运行失败的原因。无论哪种方式,都存在一些问题.latexmkrc

相关内容