# Form Fill Rest of the Page pylatex

I have the following pylatex cod:

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 = {
"margin": "0.2in",
"bottom": "0.2in",
doc = pl.Document(geometry_options=geometry_options)

with doc.create(Figure(position='h!')) as slide_pic:

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:
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))


Which creates this tex file:

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\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}


However when I try to convert it to pdf, I get the following error:

CalledProcessError Traceback (most recent call last) in () 46 47 doc.generate_tex(str(slideNumber))

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

~\Anaconda3\lib\site-packages\pylatex\document.py in generate_pdf(self, filepath, clean, clean_tex, compiler, compiler_args, silent) 231 try: 232 output = subprocess.check_output(command, --> 233 stderr=subprocess.STDOUT) 234 except (OSError, IOError) as e: 235 # Use FileNotFoundError when python 2 is dropped

~\Anaconda3\lib\subprocess.py in check_output(timeout, *popenargs, **kwargs) 334 335 return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, --> 336 **kwargs).stdout 337 338

~\Anaconda3\lib\subprocess.py in run(input, timeout, check, *popenargs, **kwargs) 416 if check and retcode: 417 raise CalledProcessError(retcode, process.args, --> 418 output=stdout, stderr=stderr) 419 return CompletedProcess(process.args, retcode, stdout, stderr) 420

CalledProcessError: Command '['latexmk', '--pdf', '--interaction=nonstopmode', '5.tex']' returned non-zero exit status 12.

• You seem to set the height with the height key. Change this to get a bigger text field. I don't know whether it is possible to do calculations of the already taken place in python or whether you can set the height option using TeX code at the TeX level. If the latter is possible you might be able to calculate the available height with \pagegoal and \pagetotal. Try searching for those two and see whether this can help you in pylatex. – Skillmon Sep 23 '18 at 15:23
• @Skillmon Please have a look at my edit. Maybe you can spot the mistake. – james Sep 23 '18 at 16:34
• Have you tried using a raw string (so NoEscape(r"height=\measure{page}"))? The thing with backslashes in strings is that Python might interpret them and make different stuff out of them. – Skillmon Sep 23 '18 at 17:16
• Also it might help to look at the generated TeX code. Can you show us what TeX code pylatex produces in the above example? – Skillmon Sep 23 '18 at 17:17
• @Skillmon See my edit. – james Sep 23 '18 at 21:19

The \measurepage macro doesn't produce correct output amidst a paragraph. To get the correct output you should put a \par (or a blank line) right before the \begin{Form} environment. The following LaTeX document produces correct output:

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\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}


Your python code should therefore include this \par in the document:

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 = {
"margin": "0.2in",
"bottom": "0.2in",
doc = pl.Document(geometry_options=geometry_options)

with doc.create(Figure(position='h!')) as slide_pic:

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:
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))

• Thank you very much !! For one page it works now, but it still does not generate a pdf if I do it for multiple pages. If you have the time, I would be very grateful, if you could have a look at my second problem. Thank you so much ! – james Sep 24 '18 at 6:17
• I have created a new question for that. Please have a look here: tex.stackexchange.com/questions/452218/… – james Sep 24 '18 at 11:08