.write中的文本被截断

时间:2018-08-25 12:15:10

标签: python unicode

我正在尝试制作一个程序,该程序将创建一个带有序言的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 to\headwidth{\color{NavyBlue}\leaders\hrule height \footrulewidth\hfill}}

\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}
\renewcommand\contentsname{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): ")

如您所见,我已经将 preamble 定义为带有unicode字符的文本。而且,当我运行该程序时,它几乎将所有前导字符串都写入了tex文档,但它切断了其中的一部分并创建了奇怪的符号,如下所示: tex document created

我将标题命名为“ stackoverflow”,主题命名为“ python problem”,并且工作正常。但是应该是'\ renewcommand'在文档'enewcommand'中。我不知道该如何解决。我只想要我的前言字符串说什么。

1 个答案:

答案 0 :(得分:1)

当您将标题和主题合并到字符串中时,您必须再次使第二部分原始

    import pandas as pd
    data={"students":["aj","mark","bob","rachel","steven"],
    "math":[98,50,23,72,87],
    "science":[96,45,76,54,1],
    "sports":["base","swim","tt","bad","tae"]}
    students=pd.DataFrame(data,columns= 
    ["students","math","science","sports"])
    print(students)

第二个“ r”缺失,在您的示例中两次。

您应该考虑使用 result = Series(result, index=obj.index, name=obj.name, copy=False) TypeError: 'list' object is not callable 进行合并。