pythonlatextikzpylatex

Failing to place an image with pylatex


I am using pylatex to create a pdf with an image in it at the coordinates that I specify. I have used the code below but no matter what coordinates I enter, the image is always in the upper left corner. Please help me.

from pylatex import (Document, TikZ, 
                 TikZNode, TikZCoordinate, 
                 TikZOptions, StandAloneGraphic,  NoEscape)
       
geometry_options = {"margin": "0cm"}
doc = Document(documentclass='standalone',document_options=('tikz'), geometry_options=geometry_options)

doc.append(NoEscape(r'\noindent'))
    
with doc.create(TikZ()) as pic:
    
# img = TikZNode(text='\includegraphics[width=0.8\\textwidth]{example-image-a}',
#                         at = TikZCoordinate(3,4),
#                         options=TikZOptions('inner sep=0pt,anchor=south west'))

img = TikZNode(text=StandAloneGraphic('example-image-a').dumps(),
                at = TikZCoordinate(1,2),
                options=TikZOptions('inner sep=0pt')
                )

pic.append(img)

tex = doc.dumps()

doc.generate_pdf('basic',compiler='lualatex', clean_tex=False)
doc.generate_tex()

Tex-Code:

\documentclass[tikz]{standalone}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{geometry}%
\geometry{margin=0cm}%
\usepackage{tikz}%
%
%
%
\begin{document}%
\normalsize%
\noindent%
\begin{tikzpicture}%
\node[inner sep=0pt] at (1.0,2.0) {\includegraphics[width=0.8\textwidth]{example-image-a}};%
\end{tikzpicture}%
\end{document}

This looks pretty similar to the code in this post to me: https://tex.stackexchange.com/questions/9559/drawing-on-an-image-with-tikz


Solution

  • The size of the tikz picture will adapt to the content and it is normally placed wherever you use it on the page, just as if you would write a normal letter.

    You can see this if you add another, bigger element, to your picture. If you now play around with the coordinates of your node, you will see the image moving around.

    Normally you could also position your nodes relative to the paper with the [remember picture, overlay] option, but you are using the standalone class, which will automatically adapt the paper size to the content, so in your case this does not help.

    \documentclass[tikz]{standalone}%
    \usepackage[T1]{fontenc}%
    \usepackage[utf8]{inputenc}%
    \usepackage{lmodern}%
    \usepackage{textcomp}%
    \usepackage{lastpage}%
    %\usepackage{geometry}%
    %\geometry{margin=0cm}%
    \usepackage{tikz}%
    %
    %
    %
    \begin{document}%
    \normalsize%
    \noindent%
    \begin{tikzpicture}%
    \path (-20,-20) rectangle (20,20);
    \node[inner sep=0pt] at (3.0,2.0) {\includegraphics[width=0.8\textwidth]{example-image-a}};%
    \end{tikzpicture}%
    \end{document}