.write 中的文本被截断



我正在尝试制作一个程序,该程序将创建一个带有序言的LaTex文件(.tex(,并且在某些部分更快。我已经定义了我的函数thepreamble(title,subject(,以便输入将在字符串中创建,这在我的代码下面可以看到。

# -*- coding: utf-8 -*-
import io
def thepreamble(title, subject):
global preamble
preamble = r'''documentclass[a4paper, 12pt]{extarticle}
usepackage[T1]{fontenc}
usepackage[utf8x]{inputenc}
usepackage[english, danish]{babel}
usepackage{fancyhdr}
usepackage[dvipsnames]{xcolor}
usepackage{mathtools}
usepackage{graphicx}
usepackage{amssymb}
usepackage{titlesec}
usepackage[left=0.5in, right=0.5in, top=0.8in, bottom=0.8in]{geometry}
usepackage{lipsum}
usepackage[breaklinks, colorlinks=true,linkcolor=NavyBlue, citecolor=blue, urlcolor=Blue, linktoc=all]{hyperref}
usepackage[utf8x]{inputenc}
usepackage{titlesec}
usepackage{fix-cm}
usepackage{titletoc}
usepackage{tocloft}
usepackage{setspace}
usepackage[all]{hypcap}
usepackage{tikz, pgfplots}
usetikzlibrary{calc}
usepackage{tkz-euclide}
usetkzobj{all}
usetikzlibrary{positioning}
usepackage{tikzrput}
usetikzlibrary{arrows.meta}
usepackage[labelfont=bf]{caption}
usepackage[hang, flushmargin]{footmisc}
usepackage{footnotebackref}

pagestyle{fancy}

fancyhf{}
fancyfoot[R]{textbf thepage}
renewcommand{headrulewidth}{0pt}
renewcommand{footrulewidth}{2pt}
renewcommand{footrule}{hbox toheadwidth{color{NavyBlue}leadershrule height footrulewidthhfill}}
newcommand{dl}[1]{underline{underline{#1}}}


setcounter{tocdepth}{2}
setcounter{secnumdepth}{0}
begin{document}
begin{titlepage}
begin{center}
vspace*{30ex}
{fontsize{38}{0}selectfont bfseries  fontfamily{put}selectfont color{NavyBlue} '''+ str(title)+'''} \
[3ex]
{fontsize{18}{0}selectfont bfseries  fontfamily{put}selectfont color{NavyBlue}  ('''+str(subject)+ ''')}\
[14ex]
{ fontsize{15}{0}selectfont Casper Juul Lorentzen} \
[3ex]
{large scshape 1.z} \
[2ex]
{large scshape 2018}\
vspace{fill}
includegraphics[scale=0.45]{C:/LaTeX/Next.png} \
[4mm]
small{bfseries Albertslund Gymnasium & HF} \
end{center}
end{titlepage}
renewcommandcontentsname{Indhold vspace{3ex}}
tableofcontents
thispagestyle{empty}
newpage
setcounter{page}{1}
'''
return preamble


def sections(numsec, numsubsec):
numbers = []
numbers.extend(numsubsec)
global tasks
tasks = []
print("") 
#Brug præfikset 'r' foran unicodes 
print("")
for n,i in zip(range(1, numsec+1),range(0,numsec)):
print("")
opgaver = "section{Opgave "+str(n)+"}"
print(opgaver)
print("")
tasks.append(opgaver)
for x in range(int(numsubsec[i])):
print("subsection{}")
print("")
return tasks
def runprogram():
encoding ='utf8'
titlefile = input("Title (file): ")
title = input("Title of document: ")
subject = input("Subject: ")
numsec = int(input("How many sections? "))
filename = "C:\Users\Casper\Documents\LaTeX\fire.tex"
while True:
numsubsec = input("How many subsections?")
while len(numsubsec) !=numsec:
print("")
numsubsec =input("Error; input must be of "+ str(numsec) + " digits ")
try:
with io.open(filename.replace('fire.tex',titlefile+".tex"), 'w', encoding=encoding) as f:
f.write(unicode_thepreamble(title, subject))
f.close()
#sections(numsec, numsubsec)
break
except:
print("Error")

runprogram()

每当我运行该程序时,它都会创建一个.tex名为

titlefile = input("Title (file): ")

如您所见,我将序言定义为包含 unicode 字符的文本。当我运行程序时,它几乎将前导码字符串的所有内容写入 tex 文档中,但它切断了其中的一些并创建了奇怪的符号,如下所示: 已创建 tex 文档

我将标题命名为"stackoverflow",主题为"python问题",这很好用。但是应该是"\renewcommand"的东西在文档"enewcommand"中。我不知道如何解决这个问题。我只想要我的序言字符串所说的。

当你将标题和主题合并到字符串中时,你必须再次使第二件作品成为原始的

r''' bla bla '''+ str(title) + r''' bla bla'''

第二个"r"丢失了,在您的示例中丢失了两次。

您应该考虑使用str.format()进行合并。

最新更新