字符串占位符不支持的格式

2024-04-28 20:55:06 发布

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

我想用python生成latex文档。所以我创建了一个模板并使用字符串占位符来改变值。你知道吗

template = r'''

\documentclass{article}

\usepackage[left=2.54cm, right=2.54cm, top=2.54cm]{geometry} \usepackage[english]{babel} %%% 'french', 'german', 'spanish', 'danish', etc. \usepackage{amssymb} \usepackage{txfonts} \usepackage{mathdots} \usepackage{pifont} \usepackage{wasysym} \usepackage{amssymb} \usepackage{tabularx,pbox}

\begin{document}

\noindent Form A

\noindent \newline

\noindent \textbf{Expansion of Hong Kong International Airport into a Three-Runway System}

\noindent Marine Travel Routes and Management Plan for High Speed Ferries of SkyPier

\noindent \textbf{\underbar{}}

\noindent \textbf{\underbar{Environmental Audit Checking Record}}

\noindent

\begin{tabularx}{\linewidth}{|l|X|} \hline Reference Plan: & Marine Travel Routes and Management Plan for High Speed Ferries of SkyPier (EP Condition 2.10) \ \hline Monitoring Data: & Ferry movement data collected in the period between \textbf{\underbar{"%(start_date)s" to "%(end_date)s"}} \ \hline Information and Data Checked: &\CheckedBox Automatic Identification System (AIS) Data \newline \CheckedBox Daily SkyPier HSF movements \newline \CheckedBox Record of potential deviations \newline \CheckedBox Response provided by the ferry operators\ \hline Comments and Observations: & The deviation of implementation of SkyPier HSF plan was checked. Eight notices were issued by AAHK to ferry operators related to potential speeding across the SCZ, not travelling through the gate access points and \ \hline

\end{tabularx}

\noindent \newline \newline \newline \newline \newline \newline

\begin{tabularx}{\linewidth}{|l|X|X|X|} \hline & ET Leader \newline ET's Representative & IEC \newline IEC's Representative & PM \newline PM's Representative\ \hline Signature&&&\ \hline Name & Terence Kong&&\ \hline \end{tabularx}

\noindent

\end{document}

'''

但是,当我试着跑的时候

page = template % {'start_date' : 'a', 'end_date' : 'b' }

它回来了

ValueError: unsupported format character ''' (0x27) at index 120

Tags: andofthedatenewlinecmendbegin
2条回答

问题是行中的%%%

\usepackage[left=2.54cm, right=2.54cm, top=2.54cm]{geometry} \usepackage[english]{babel} %%% 'french', 'german', 'spanish', 'danish', etc. \usepackage{amssymb} \usepackage{txfonts} \usepackage{mathdots} \usepackage{pifont} \usepackage{wasysym} \usepackage{amssymb} \usepackage{tabularx,pbox}

当使用%格式化字符串时,可以通过将其转义为%%来编写一个actual%,因此%%%被解释为一个actual%,然后另一个%被用作占位符,下面的"'"被解释为(无法识别的)格式字符。你知道吗

只需将%%%替换为偶数%,例如%%%%%%,如果您希望在生成的Latex代码中正好有3 %,但是任何其他偶数也可以。你知道吗

相关问题 更多 >