JelteF / PyLaTeX

A Python library for creating LaTeX files
https://jeltef.github.io/PyLaTeX/
MIT License
2.24k stars 287 forks source link

Research #384

Open Maria2000haha opened 1 month ago

Maria2000haha commented 1 month ago

paper_json = json.dumps({ "title": "Outcome of Non-Operative Management of Splenic Blunt Trauma", "author_list": ["Dhurgham Jabbar Hassoon"], "keyword_list": ["Splenic trauma", "Non-operative management", "Blunt trauma", "Spleen"], "abstract": "This study explores the thrilling roller-coaster ride of treating splenic blunt trauma without surgical intervention. We delve into the fates of 78 brave souls whose spleens were put to the test, challenging the norms of 'cut first, ask questions later.'", "paragraphs": [ "The spleen, often overlooked in favor of more glamorous organs like the heart and brain, plays a crucial role in immune function and blood filtration. Yet, it is not immune to trauma, especially blunt trauma, which is as common as a plot twist in a daytime soap opera. Non-operative management (NOM) of splenic blunt trauma has emerged as a heroic alternative to the scalpel, offering patients a chance to keep their spleen and live to tell the tale.", "Our study involved 78 patients who presented with splenic blunt trauma. Instead of whisking them off to the operating room, we opted for a more conservative approach, involving close monitoring, bed rest, and a hefty dose of optimism. The results were surprisingly good – like finding out your favorite TV show isn't getting canceled after all. Only a handful of patients required surgery after initial NOM, and the overall success rate was a resounding 'spleendid' (pun very much intended)." ] })

import json from pylatex import Document, Section, Subsection, Command, Package from pylatex.utils import italic, NoEscape

def create_document(): paper = json.loads(paper_json)

title = paper.get('title', 'No Title')
authors = paper.get('authors', [])
keywords = paper.get('keywords', [])
abstract = paper.get('abstract', 'No Abstract')
paragraphs = paper.get('paragraphs', [])

doc = Document(document_options=['12pt'])
doc.packages.append(Package('geometry', options=['top=2cm', 'bottom=2cm', 'left=2cm', 'right=2cm']))
doc.packages.append(Package('tikz'))

doc.preamble.append(Command('title', NoEscape(r'\vspace{-2cm}\hrule height 2pt \vspace{0.5cm} \textbf{' + title + r'}\vspace{0.25cm}\hrule\vspace{1cm}')))
doc.preamble.append(Command('author', NoEscape(r'\textbf{' + ' and '.join(authors) + r'}')))
doc.preamble.append(Command('date', NoEscape(r'\today')))
doc.append(NoEscape(r'\maketitle'))

doc.append(NoEscape(r'\begin{center}'))
with doc.create(Section(NoEscape(r'\textbf{Abstract}'), numbering=False)):
    doc.append(NoEscape(r'\begin{minipage}{0.8\textwidth}'))
    doc.append(abstract)
    doc.append(NoEscape(r'\end{minipage}'))
doc.append(NoEscape(r'\end{center}'))

doc.append(NoEscape(r'\vspace{1cm}'))
doc.append(NoEscape(r'\noindent'))

doc.append(NoEscape(r'\begin{minipage}[t]{0.46\textwidth}'))
doc.append(paragraphs[0])
doc.append(NoEscape(r'\end{minipage}\hfill'))
doc.append(NoEscape(r'\hspace{0.08\textwidth}'))
doc.append(NoEscape(r'\begin{minipage}[t]{0.46\textwidth}'))
doc.append(paragraphs[1])
doc.append(NoEscape(r'\end{minipage}'))

doc.append(NoEscape(r'''
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=west, rotate=90, gray, scale=2.2] at ([xshift=0.75cm, yshift=-19cm]current page.north west) {arXiv:2311.17421v0 [http://cs.CV] 09 Nov 2023};
\end{tikzpicture}
'''))

pdf_filename = 'paper'
doc.generate_pdf(pdf_filename)

return pdf_filename + ".pdf"

tex_filename = create_document() print('Created LaTeX document: {}'.format(tex_filename))