填写页面其余部分 pylatex

填写页面其余部分 pylatex

我有以下 pylatex 代码:

import pylatex as pl
from pylatex.utils import NoEscape
from pylatex import Command
from pylatex import Package
from pylatex import Figure
import os

videoNumber = 1
slideNumber = 5

class Form(pl.base_classes.Environment):
    """A class to wrap hyperref's form environment."""

    _latex_name = 'Form'

    packages = [pl.Package('hyperref')]
    escape = False
    content_separator = "\n"


geometry_options = {
    "head": "1pt",
    "margin": "0.2in",
    "bottom": "0.2in",
    "includeheadfoot": False}
doc = pl.Document(geometry_options=geometry_options)


with doc.create(Figure(position='h!')) as slide_pic:
                slide_pic.add_image("1.jpg",width=NoEscape("\linewidth"))


dirname = os.getcwd()
filename = os.path.join(dirname, 'Text And Slides\\' + str(videoNumber) + '\\Text\\' + str(slideNumber))
with open(filename+".txt", 'r',encoding="utf8") as myfile:
    text=myfile.read().replace('\n', '')
doc.append(text)
doc.append(NoEscape(r"\newcommand\measurepage{\the\dimexpr\pagegoal-\pagetotal-\baselineskip\relax}"))

with doc.create(Form()):
    doc.append(Command('noindent'))
    doc.append(Command('TextField',
               options=["name=multilinetextbox", "multiline=true",
                        NoEscape("width=\linewidth"),NoEscape("height=\measurepage")],
               arguments=''))

doc.generate_tex(str(slideNumber))
doc.generate_pdf(str(slideNumber))

这将创建这个 tex 文件:

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage[head=1pt,margin=0.2in,bottom=0.2in,includeheadfoot=False]{geometry}%
\usepackage{graphicx}%
\usepackage{hyperref}%
%
%
%
\begin{document}%
\normalsize%


\begin{figure}[h!]%
\centering%
\includegraphics[width=\linewidth]{1.jpg}%
\end{figure}

%
Now in this particular case where we have equal populations we can distinguish different regimes. This picture is on the title page of the first edition of Peter Hore's book. First of all it shows the spectra that we see as a function of the exchange rate. You will note that this is a logarithmic scale for example this corresponds to one jump per second while this corresponds to a thousand jumps per second and of course we can go all the way to 100000 per second. This corresponds generally speaking to an increasing temperature. There are other ways of course of changing the speed of a chemical reaction but temperature is certainly one way of doing it. At low temperatures we have slow rates and at slow rates we can actually distinguish the chemical shifts of site A and site B as we have in previous examples. In these simulations they happen to be separated by a difference in chemical shifts that is 50 Hz and which elsewhere is called delta nu with a lower case delta. So that's the abstract notation for the difference in chemical shifts delta nu is equal to nu zero of site B minus nu zero of site A. Now what is interesting is that if we increase the temperature or otherwise increase the interchange rate then the lines begin to broaden and more surprisingly they tend to move closer together. It turns out that the distance between these broad humps is smaller than the distance between the two narrow peaks at low temperature. They get broadened to the point that we see almost no signal and then they turn up as a single hump the two lines appear merged together into a single hump a phenomenon that we call âcoalescenceâ in our trade. More remarkably the single line which is initially rather broad will become narrower and narrower as the exchange rate increases while at the same time we end up with an average chemical shift which is simply half{-}way between the two chemical shifts of the separate sites.%
\newcommand\measurepage{\the\dimexpr\pagegoal-\pagetotal-\baselineskip\relax}%
\begin{Form}
\noindent
\TextField[name=multilinetextbox,multiline=true,width=\linewidth,height=\measurepage]{}
\end{Form}%
\end{document}

但是当我尝试将其转换为 pdf 时,出现以下错误:

CalledProcessError Traceback(最近一次调用最后一次)在()46 47 doc.generate_tex(str(slideNumber))

---> 48 doc.generate_pdf(str(slideNumber))

~\Anaconda3\lib\site-packages\pylatex\document.py 在 generate_pdf(self、filepath、clean、clean_tex、compiler、compiler_args、silent) 中 231 尝试:232 output = subprocess.check_output(command, --> 233 stderr=subprocess.STDOUT) 234 除外 (OSError, IOError) 作为 e:235 # 当 python 2 被删除时使用 FileNotFoundError

~\Anaconda3\lib\subprocess.py 在 check_output(timeout、*popenargs、**kwargs) 中 334 335 返回 run(*popenargs、stdout=PIPE、timeout=timeout、check=True、--> 336 **kwargs).stdout 337 338

~\Anaconda3\lib\subprocess.py 在运行(输入、超时、检查、*popenargs、**kwargs)416 中,如果检查和 retcode:417 引发 CalledProcessError(retcode、process.args、--> 418 输出=stdout,stderr=stderr)419 返回 CompletedProcess(process.args、retcode、stdout、stderr)420

CalledProcessError:命令‘[‘latexmk’、‘--pdf’、‘--interaction=nonstopmode’、‘5.tex’]’返回非零退出状态 12。

答案1

\measurepage无法在段落中产生正确的输出。要获得正确的输出,您应该在环境\par前面放置一个(或一个空白行)\begin{Form}。以下 LaTeX 文档产生正确的输出:

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage[head=1pt,margin=0.2in,bottom=0.2in,includeheadfoot=False]{geometry}%
\usepackage{graphicx}%
\usepackage{hyperref}%
%
%
%
\begin{document}%
\normalsize%


\begin{figure}[h!]%
\centering%
\includegraphics[width=\linewidth]{example-image-duck}%
\end{figure}

%
Now in this particular case where we have equal populations we can distinguish different regimes. This picture is on the title page of the first edition of Peter Hore's book. First of all it shows the spectra that we see as a function of the exchange rate. You will note that this is a logarithmic scale for example this corresponds to one jump per second while this corresponds to a thousand jumps per second and of course we can go all the way to 100000 per second. This corresponds generally speaking to an increasing temperature. There are other ways of course of changing the speed of a chemical reaction but temperature is certainly one way of doing it. At low temperatures we have slow rates and at slow rates we can actually distinguish the chemical shifts of site A and site B as we have in previous examples. In these simulations they happen to be separated by a difference in chemical shifts that is 50 Hz and which elsewhere is called delta nu with a lower case delta. So that's the abstract notation for the difference in chemical shifts delta nu is equal to nu zero of site B minus nu zero of site A. Now what is interesting is that if we increase the temperature or otherwise increase the interchange rate then the lines begin to broaden and more surprisingly they tend to move closer together. It turns out that the distance between these broad humps is smaller than the distance between the two narrow peaks at low temperature. They get broadened to the point that we see almost no signal and then they turn up as a single hump the two lines appear merged together into a single hump a phenomenon that we call âcoalescenceâ in our trade. More remarkably the single line which is initially rather broad will become narrower and narrower as the exchange rate increases while at the same time we end up with an average chemical shift which is simply half{-}way between the two chemical shifts of the separate sites.%
\newcommand\measurepage{\the\dimexpr\pagegoal-\pagetotal-\baselineskip\relax}%
\par % <---- This line added
\begin{Form}
\noindent
\TextField[name=multilinetextbox,multiline=true,width=\linewidth,height=\measurepage]{}
\end{Form}%
\end{document}

在此处输入图片描述

因此,你的 Python 代码应该\par在文档中包含以下内容:

import pylatex as pl
from pylatex.utils import NoEscape
from pylatex import Command
from pylatex import Package
from pylatex import Figure
import os

videoNumber = 1
slideNumber = 5

class Form(pl.base_classes.Environment):
    """A class to wrap hyperref's form environment."""

    _latex_name = 'Form'

    packages = [pl.Package('hyperref')]
    escape = False
    content_separator = "\n"


geometry_options = {
    "head": "1pt",
    "margin": "0.2in",
    "bottom": "0.2in",
    "includeheadfoot": False}
doc = pl.Document(geometry_options=geometry_options)


with doc.create(Figure(position='h!')) as slide_pic:
                slide_pic.add_image("1.jpg",width=NoEscape("\linewidth"))


dirname = os.getcwd()
filename = os.path.join(dirname, 'Text And Slides\\' + str(videoNumber) + '\\Text\\' + str(slideNumber))
with open(filename+".txt", 'r',encoding="utf8") as myfile:
    text=myfile.read().replace('\n', '')
doc.append(text)
doc.append(NoEscape(r"\newcommand\measurepage{\the\dimexpr\pagegoal-\pagetotal-\baselineskip\relax}"))
doc.append(NoEscape(r"\par")) # <---- This line added

with doc.create(Form()):
    doc.append(Command('noindent'))
    doc.append(Command('TextField',
               options=["name=multilinetextbox", "multiline=true",
                        NoEscape("width=\linewidth"),NoEscape("height=\measurepage")],
               arguments=''))

doc.generate_tex(str(slideNumber))
doc.generate_pdf(str(slideNumber))

相关内容