在 PyLaTex 中如何使用\usepackage{multicol}
?在 Python 中该写些什么?
\begin{multicols}{2}
Column 1
\columnbreak
Column 2
\end{multicols}
我已经找到了一种使用 PyLaTex 以双列显示内容的方法,但我不确定这是否是最好的编程方法。
PyLaTex 以双列显示内容的最佳方式是什么?
PyLaTeX 代码如下:
from pylatex import Document, Section, Subsection, Command
from pylatex.utils import italic, NoEscape
from pylatex.package import Package
from pylatex import PageStyle, Head, MiniPage, Foot, LargeText, \
MediumText, LineBreak, simple_page_number
from pylatex.utils import bold
lorem = '''
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere
cubilia Curae; Phasellus facilisis tortor vel imperdiet vestibulum. Vivamus et
mollis risus. Proin ut enim eu leo volutpat tristique. Vivamus quam enim,
efficitur quis turpis ac, condimentum tincidunt tellus. Praesent non tellus in
quam tempor dignissim. Sed feugiat ante id mauris vehicula, quis elementum nunc
molestie. Pellentesque a vulputate nisi, ut vulputate ex. Morbi erat eros,
aliquam in justo sed, placerat tempor mauris. In vitae velit eu lorem dapibus
consequat. Integer posuere ornare laoreet.
Donec pellentesque libero id tempor aliquam. Maecenas a diam at metus varius
rutrum vel in nisl. Maecenas a est lorem. Vivamus tristique nec eros ac
hendrerit. Vivamus imperdiet justo id lobortis luctus. Sed facilisis ipsum ut
tellus pellentesque tincidunt. Mauris libero lectus, maximus at mattis ut,
venenatis eget diam. Fusce in leo at erat varius laoreet. Mauris non ipsum
pretium, convallis purus vel, pulvinar leo. Aliquam lacinia lorem dapibus
tortor imperdiet, quis consequat diam mollis.
Praesent accumsan ultrices diam a eleifend. Vestibulum ante ipsum primis in
faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse accumsan
orci ut sodales ullamcorper. Integer bibendum elementum convallis. Praesent
accumsan at leo eget ullamcorper. Maecenas eget tempor enim. Quisque et nisl
eros.
'''
if __name__ == '__main__':
geometry_options = {"top":"3.5cm","bottom":"3.5cm", "left":"3.7cm",\
"right":"4.7cm", "columnsep":"30pt"}
doc = Document('test', geometry_options=geometry_options)
doc.preamble.append(Command('usepackage', 'multicol'))
doc.append(NoEscape(r'\begin{multicols}{2}'))
doc.append(NoEscape(lorem))
doc.append(NoEscape(r'\end{multicols}'))
doc.generate_pdf(clean_tex=False)
doc.generate_tex()
tex = doc.dumps()
答案1
你可以使用以下命令定义新环境Environment
。
from pylatex import UnsafeCommand
from pylatex.base_classes import Environment
from pylatex.utils import NoEscape
class MultiColumnEnvironment(Environment):
_latex_name = 'MultiColumn'
packages = [Package('multicol')]
def main():
geometry_options = {"top":"3.5cm","bottom":"3.5cm", "left":"3.7cm",\
"right":"4.7cm", "columnsep":"30pt"}
doc = Document('test', geometry_options=geometry_options)
...
new_env = UnsafeCommand('newenvironment', 'MultiColumn',
extra_arguments=[r'\begin{multicols}{2}', r'\end{multicols}'])
doc.append(new_env)
with doc.create(MultiColumnEnvironment()) as environment:
environment.append(NoEscape(lorem))
if __name__ == "__main__":
main()