文本输入。写入

2024-09-29 19:29:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使一个程序,将创建一个乳胶文件(.tex),其中有一个前导,以及更快的一些部分。我已经定义了我的函数流(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): ")

如您所见,我将序言定义为包含unicode字符的文本。当我运行这个程序时,它几乎写下了tex文档中所有的前导字符串,但是它切断了部分前导字符串并创建了奇怪的符号,比如: tex document created

我将标题命名为“stackoverflow”并将主题命名为“python问题”,这很好。但是应该是“\renewcommand”的内容在文档“enewcommand”中。我不知道怎么解决这个问题。我只想知道我的前导所说的。你知道吗


Tags: ininputtitletaskstexsubjectprintstr
1条回答
网友
1楼 · 发布于 2024-09-29 19:29:21

当你把你的标题和主题合并到字符串中时,你必须把第二个片段重新生出来

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

第二个“r”丢失了,在您的示例中丢失了两次。你知道吗

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

相关问题 更多 >

    热门问题