Home > Software engineering >  Python Latex package not inserting graphic
Python Latex package not inserting graphic

Time:11-27

I have a python program using the pdflatex PyPDF2 package to generate a LaTeX .tex file and then convert that to a .pdf file.

My problem is that the pdf file needs to include an image, and that image is not being inserted into the document.

The LaTeX file is generated by the following python code:

# compose a LaTex file
content = r'''\documentclass{article}
\begin{document}
\usepackage{graphicx}
\graphicspath{./}
\includegraphics{Image.jpg}
\textbf{\huge DSC 501\\}
\textit{Programming for Data Science\\}
\vspace{1cm}
\textbf{Andrew Coleman\\}
19 November 2022\\
\textbf{\Large Southern Connecticut State University \\}
\section{IMDb movie analysis}
Here we are...\\

\end{document}

# specify the file name without an extension so we can reuse it

from datetime import datetime
outfile = 'scsu-dsc501-pdflatex-demo'
timestamped_outfile = data_path   outfile   '_'   datetime.now().strftime("%Y-%m-%d_at_%H-%M-%S")

# store a LaTex file
with open(timestamped_outfile '.tex','w') as f:
    f.write(content)

When this is converted to a pdf file, instead of the image (Image.jpg, which is in the same directory as the .tex file), the pdf file contains "graphicx ./Image.jpg"

How do I get the image itself to appear in the pdf?

This is running via Colab on a Mac laptop.

CodePudding user response:

Packages must not be loaded after \begin{document}.

Some other comments:

  • Adding the current directory to the graphic path is also not necessary, the current directory is searched by default.

  • you shouldn't abuse \\ for line breaks, just leave an empty line to start a new paragraph

  • if you make font size changes and switch back to normal font before the end of the paragraph, your line spacing will be wrong.

\documentclass{article}
\usepackage{graphicx}
%\graphicspath{./}
\begin{document}
\includegraphics{example-image-duck}

{\huge\textbf{DSC 501}\par}
\textit{Programming for Data Science}

\vspace{1cm}
\textbf{Andrew Coleman}

19 November 2022

{\Large\textbf{Southern Connecticut State University}\par}
\section{IMDb movie analysis}
Here we are...

\end{document}

enter image description here

  • Related