How to format a LaTeX string in python?

后端 未结 2 1089
南笙
南笙 2021-02-06 00:46

I\'m writing an application, part of whose functionality is to generate LaTeX CVs, so I find myself in a situation where I have strings like

\\begin{document}
\\         


        
2条回答
  •  孤街浪徒
    2021-02-06 01:12

    You may not use the new format syntax to avoid escaping the { and }.

    That should work:

    >>> a = r'''
    \title{%(title)s}
    \author{%(author)s}
    \begin{document}'''
    
    >>> b = a % {'title': 'My Title', 'author': 'Me, Of course'}
    >>> print(b)
    
    \title{My Title}
    \author{Me, Of course}
    \begin{document}
    

    You should use raw strings r'something' to avoid escaping \ as \\.

    PS: You should take a look on txt2tags, a Python script to convert t2t formatted text into html, latex, markdown etc. Check the source code to see how these conversions are done.

提交回复
热议问题