1

I'm wondering to know how to draw some lines in a figure or and add some linewidth on an image but I've trying I have not get any solution.

This is the code I'm trying:

from pylatex import Document, PageStyle, Head, MiniPage, Foot, LargeText, \
MediumText, LineBreak, simple_page_number, Figure, NoEscape,\
StandAloneGraphic, Package, Command, VerticalSpace,\
HugeText, Section, NewPage, Center, TikZDraw,\
TikZCoordinate, TikZOptions, TikZ, TikZNode
from pylatex.utils import bold 
import os

def generate_header(): #Imagenes Portada image = os.path.join(os.path.dirname(file), 'logo.png')

with doc.create(Center()) as center:
center.append(VerticalSpace("20pt"))
     with center.create(Section('Screen 1', numbering=True)):
          center.append(VerticalSpace("20pt"))
          with center.create(Figure(position='h!')) as image1:
              image1.add_image(image, width='317px')
              image1.add_caption('Description')

              with image1.create(TikZ()) as pic:
                  # pic.append(TikZDraw([TikZCoordinate(-5, 0),
                  #             'rectangle',
                  #             TikZCoordinate(7, -2)],
                  #             ))
                   pic.append(TikZDraw([TikZCoordinate(0,0),
                        'circle'],
                        options=TikZOptions(radius='5pt')
                        ))

What I get is the circle below the image and not over the figure.

  • 1
    Hi, welcome. First, your Python-code has the wrong indentation. And please also make a complete example, so that we can try what you've done without modifications. What you've done so far is to add the tikzpicture after the diagram. They are two separate boxes, and are placed one after the other. You should rather generate code along the lines of this example https://tex.stackexchange.com/a/9561/ – Torbjørn T. May 26 '21 at 16:17
  • I just corrected the indentation but I do not how to apply the code you just mention on pylatex. Any idea? @TorbjørnT. – Richard21 May 28 '21 at 22:57

1 Answers1

0

This might be horrible code, but it works I suppose. I didn't use exactly the same strategy as in the post I linked to in the comment, as I'm too daft/lazy to figure out how to do it. Specifically, I dropped the scope, which means that the coordinates are not relative to the image size. Hence, if you change the size of the image, you have to change all the coordinates of the paths. The way this is set up, the lower left corner of the image is at the coordinate (0,0).

enter image description here

from pylatex import Document, PageStyle, Head, MiniPage, Foot, LargeText, \
MediumText, LineBreak, simple_page_number, Figure, NoEscape,\
StandAloneGraphic, Package, Command, VerticalSpace,\
HugeText, Section, NewPage, Center, TikZDraw,\
TikZCoordinate, TikZOptions, TikZ, TikZNode
from pylatex.utils import bold 
import os

def generate_header(): #Imagenes Portada image = 'example-image'

with doc.create(Center()) as center:
    center.append(VerticalSpace("20pt"))
    with center.create(Section('Screen 1', numbering=True)):
        center.append(VerticalSpace("20pt"))
        with center.create(Figure(position='h!')) as image1:
            with image1.create(TikZ()) as pic:
                pic.append(TikZNode(text=StandAloneGraphic(image,image_options='width=7cm').dumps(),
                            options=TikZOptions('inner sep=0pt,name=img', 'anchor=south west')
                            ))
                pic.append(TikZDraw([TikZCoordinate(0.1, 3), '--', TikZCoordinate(6, 4)], options=TikZOptions('draw', 'red', 'ultra thick', '-stealth')))
                pic.append(TikZDraw([TikZCoordinate(3.5,0.5), 'rectangle', TikZCoordinate(6, 2)],
                                     options=TikZOptions('draw','blue','thick')))

doc = Document()

generate_header()

doc.generate_tex('my_solution')

Which generates this code:

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{ragged2e}%
\usepackage{tikz}%
%
%
%
\begin{document}%
\normalsize%
\begin{center}%
\vspace*{20pt}%
\section{Screen 1}%
\label{sec:Screen1}%
\vspace*{20pt}%

\begin{figure}[h!]% \begin{tikzpicture}% \node[inner sep=0pt,name=img,anchor=south west] {\includegraphics[width=7cm]{example-image}};% \path[draw,red,ultra thick,-stealth,draw] (0.1,3.0) -- (6.0,4.0);% \path[draw,blue,thick,draw] (3.5,0.5) rectangle (6.0,2.0);% \end{tikzpicture}% \end{figure}

% \end{center}% \end{document}

Torbjørn T.
  • 206,688
  • Thanks a lot. Now I know how to do it but I have a remainding question, how can I use here the add_caption command if now I'm using TikZ? @TorbjørnT. – Richard21 May 30 '21 at 05:39
  • @Richard21 The caption doesn't have anything to do with the TikZ part, do the same thing you did in your original code: image1.add_caption('foo') – Torbjørn T. May 30 '21 at 06:59